blob: 84d90d087a3040d615ce79397c95fc90deae19fb [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 *
Alan Cox113aa832008-10-13 19:01:08 -070012 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/errno.h>
43#include <linux/sched.h>
44#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/netdevice.h>
46#include <linux/rtnetlink.h>
47#include <linux/poll.h>
48#include <linux/highmem.h>
Herbert Xu3305b802005-12-13 23:16:37 -080049#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Jason Wang04335472013-08-06 17:45:07 +080051#include <linux/pagemap.h>
Herbert Xua8f820aa2014-11-07 21:22:22 +080052#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <net/protocol.h>
55#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070057#include <net/checksum.h>
58#include <net/sock.h>
59#include <net/tcp_states.h>
Neil Hormane9b3cc12009-08-13 05:19:44 +000060#include <trace/events/skb.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030061#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Is a socket 'connection oriented' ?
65 */
66static inline int connection_based(struct sock *sk)
67{
68 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69}
70
Eric Dumazet95c96172012-04-15 05:58:06 +000071static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
Eric Dumazetbf368e42009-04-28 02:24:21 -070072 void *key)
73{
74 unsigned long bits = (unsigned long)key;
75
76 /*
77 * Avoid a wakeup if event not interesting for us
78 */
79 if (bits && !(bits & (POLLIN | POLLERR)))
80 return 0;
81 return autoremove_wake_function(wait, mode, sync, key);
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Benjamin Poirier39cc8612013-04-29 11:42:13 +000084 * Wait for the last received packet to be different from skb
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
Benjamin Poirier39cc8612013-04-29 11:42:13 +000086static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
87 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int error;
Eric Dumazetbf368e42009-04-28 02:24:21 -070090 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Eric Dumazetaa395142010-04-20 13:03:51 +000092 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Socket errors? */
95 error = sock_error(sk);
96 if (error)
97 goto out_err;
98
Benjamin Poirier39cc8612013-04-29 11:42:13 +000099 if (sk->sk_receive_queue.prev != skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto out;
101
102 /* Socket shut down? */
103 if (sk->sk_shutdown & RCV_SHUTDOWN)
104 goto out_noerr;
105
106 /* Sequenced packets can come disconnected.
107 * If so we report the problem
108 */
109 error = -ENOTCONN;
110 if (connection_based(sk) &&
111 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
112 goto out_err;
113
114 /* handle signals */
115 if (signal_pending(current))
116 goto interrupted;
117
118 error = 0;
119 *timeo_p = schedule_timeout(*timeo_p);
120out:
Eric Dumazetaa395142010-04-20 13:03:51 +0000121 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return error;
123interrupted:
124 error = sock_intr_errno(*timeo_p);
125out_err:
126 *err = error;
127 goto out;
128out_noerr:
129 *err = 0;
130 error = 1;
131 goto out;
132}
133
134/**
Herbert Xua59322b2007-12-05 01:53:40 -0800135 * __skb_recv_datagram - Receive a datagram skbuff
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700136 * @sk: socket
137 * @flags: MSG_ flags
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000138 * @peeked: returns non-zero if this packet has been seen before
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000139 * @off: an offset in bytes to peek skb from. Returns an offset
140 * within an skb where data actually starts
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700141 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
143 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
144 * and possible races. This replaces identical code in packet, raw and
145 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
146 * the long standing peek and read race for datagram sockets. If you
147 * alter this routine remember it must be re-entrant.
148 *
149 * This function will lock the socket if a skb is returned, so the caller
150 * needs to unlock the socket in that case (usually by calling
151 * skb_free_datagram)
152 *
153 * * It does not lock socket since today. This function is
154 * * free of race conditions. This measure should/can improve
155 * * significantly datagram socket latencies at high loads,
156 * * when data copying to user space takes lots of time.
157 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
158 * * 8) Great win.)
159 * * --ANK (980729)
160 *
161 * The order of the tests when we find no data waiting are specified
162 * quite explicitly by POSIX 1003.1g, don't change them without having
163 * the standard around please.
164 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000165struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000166 int *peeked, int *off, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000168 struct sk_buff *skb, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 long timeo;
170 /*
171 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
172 */
173 int error = sock_error(sk);
174
175 if (error)
176 goto no_packet;
177
Herbert Xua59322b2007-12-05 01:53:40 -0800178 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 do {
181 /* Again only user level code calls this function, so nothing
182 * interrupt level will suddenly eat the receive_queue.
183 *
184 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +0000185 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Herbert Xua59322b2007-12-05 01:53:40 -0800187 unsigned long cpu_flags;
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000188 struct sk_buff_head *queue = &sk->sk_receive_queue;
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000189 int _off = *off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000191 last = (struct sk_buff *)queue;
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000192 spin_lock_irqsave(&queue->lock, cpu_flags);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000193 skb_queue_walk(queue, skb) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000194 last = skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800195 *peeked = skb->peeked;
196 if (flags & MSG_PEEK) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000197 if (_off >= skb->len && (skb->len || _off ||
Benjamin Poirieradd05ad2013-04-29 11:42:12 +0000198 skb->peeked)) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000199 _off -= skb->len;
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000200 continue;
201 }
Herbert Xua59322b2007-12-05 01:53:40 -0800202 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800204 } else
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000205 __skb_unlink(skb, queue);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000206
207 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000208 *off = _off;
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000209 return skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800210 }
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000211 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Eliezer Tamircbf55002013-07-08 16:20:34 +0300213 if (sk_can_busy_loop(sk) &&
214 sk_busy_loop(sk, flags & MSG_DONTWAIT))
Eliezer Tamira5b50472013-06-10 11:40:00 +0300215 continue;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* User doesn't want to wait */
218 error = -EAGAIN;
219 if (!timeo)
220 goto no_packet;
221
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000222 } while (!wait_for_more_packets(sk, err, &timeo, last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 return NULL;
225
226no_packet:
227 *err = error;
228 return NULL;
229}
Herbert Xua59322b2007-12-05 01:53:40 -0800230EXPORT_SYMBOL(__skb_recv_datagram);
231
Eric Dumazet95c96172012-04-15 05:58:06 +0000232struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
Herbert Xua59322b2007-12-05 01:53:40 -0800233 int noblock, int *err)
234{
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000235 int peeked, off = 0;
Herbert Xua59322b2007-12-05 01:53:40 -0800236
237 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000238 &peeked, &off, err);
Herbert Xua59322b2007-12-05 01:53:40 -0800239}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000240EXPORT_SYMBOL(skb_recv_datagram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
243{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000244 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800245 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Eric Dumazet9d410c72009-10-30 05:03:53 +0000247EXPORT_SYMBOL(skb_free_datagram);
248
249void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
250{
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000251 bool slow;
252
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700253 if (likely(atomic_read(&skb->users) == 1))
254 smp_rmb();
255 else if (likely(!atomic_dec_and_test(&skb->users)))
256 return;
257
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000258 slow = lock_sock_fast(sk);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700259 skb_orphan(skb);
260 sk_mem_reclaim_partial(sk);
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000261 unlock_sock_fast(sk, slow);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700262
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700263 /* skb is now orphaned, can be freed outside of locked section */
264 __kfree_skb(skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000265}
266EXPORT_SYMBOL(skb_free_datagram_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/**
Herbert Xu3305b802005-12-13 23:16:37 -0800269 * skb_kill_datagram - Free a datagram skbuff forcibly
270 * @sk: socket
271 * @skb: datagram skbuff
272 * @flags: MSG_ flags
273 *
274 * This function frees a datagram skbuff that was received by
275 * skb_recv_datagram. The flags argument must match the one
276 * used for skb_recv_datagram.
277 *
278 * If the MSG_PEEK flag is set, and the packet is still on the
279 * receive queue of the socket, it will be taken off the queue
280 * before it is freed.
281 *
282 * This function currently only disables BH when acquiring the
283 * sk_receive_queue lock. Therefore it must not be used in a
284 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800285 *
286 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800287 */
288
Herbert Xu27ab2562007-12-05 01:51:58 -0800289int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800290{
Herbert Xu27ab2562007-12-05 01:51:58 -0800291 int err = 0;
292
Herbert Xu3305b802005-12-13 23:16:37 -0800293 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800294 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800295 spin_lock_bh(&sk->sk_receive_queue.lock);
296 if (skb == skb_peek(&sk->sk_receive_queue)) {
297 __skb_unlink(skb, &sk->sk_receive_queue);
298 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800299 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800300 }
301 spin_unlock_bh(&sk->sk_receive_queue.lock);
302 }
303
John Dykstra61de71c2009-05-08 14:57:01 -0700304 kfree_skb(skb);
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000305 atomic_inc(&sk->sk_drops);
John Dykstra61de71c2009-05-08 14:57:01 -0700306 sk_mem_reclaim_partial(sk);
307
Herbert Xu27ab2562007-12-05 01:51:58 -0800308 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800309}
Herbert Xu3305b802005-12-13 23:16:37 -0800310EXPORT_SYMBOL(skb_kill_datagram);
311
312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700314 * @skb: buffer to copy
315 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700316 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700317 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
319 * Note: the iovec is modified during the copy.
320 */
321int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
322 struct iovec *to, int len)
323{
David S. Miller1a028e52007-04-27 15:21:23 -0700324 int start = skb_headlen(skb);
325 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700326 struct sk_buff *frag_iter;
Herbert Xuc75d7212005-11-02 18:55:00 +1100327
Neil Hormane9b3cc12009-08-13 05:19:44 +0000328 trace_skb_copy_datagram_iovec(skb, len);
329
David S. Millerb4d9eda2006-02-13 16:06:10 -0800330 /* Copy header. */
331 if (copy > 0) {
332 if (copy > len)
333 copy = len;
334 if (memcpy_toiovec(to, skb->data + offset, copy))
335 goto fault;
336 if ((len -= copy) == 0)
337 return 0;
338 offset += copy;
339 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100340
David S. Millerb4d9eda2006-02-13 16:06:10 -0800341 /* Copy paged appendix. Hmm... why does this look so complicated? */
342 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700343 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000344 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700346 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700347
Eric Dumazet9e903e02011-10-18 21:00:24 +0000348 end = start + skb_frag_size(frag);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800349 if ((copy = end - offset) > 0) {
350 int err;
351 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000352 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (copy > len)
355 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800356 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700357 err = memcpy_toiovec(to, vaddr + frag->page_offset +
358 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800359 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (err)
361 goto fault;
362 if (!(len -= copy))
363 return 0;
364 offset += copy;
365 }
David S. Miller1a028e52007-04-27 15:21:23 -0700366 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800368
David S. Miller5b1a0022009-06-09 00:18:15 -0700369 skb_walk_frags(skb, frag_iter) {
370 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800371
David S. Miller5b1a0022009-06-09 00:18:15 -0700372 WARN_ON(start > offset + len);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800373
David S. Miller5b1a0022009-06-09 00:18:15 -0700374 end = start + frag_iter->len;
375 if ((copy = end - offset) > 0) {
376 if (copy > len)
377 copy = len;
378 if (skb_copy_datagram_iovec(frag_iter,
379 offset - start,
380 to, copy))
381 goto fault;
382 if ((len -= copy) == 0)
383 return 0;
384 offset += copy;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800385 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700386 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800388 if (!len)
389 return 0;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391fault:
392 return -EFAULT;
393}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000394EXPORT_SYMBOL(skb_copy_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Rusty Russelldb543c12008-08-15 15:13:53 -0700396/**
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000397 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
398 * @skb: buffer to copy
399 * @offset: offset in the buffer to start copying from
400 * @to: io vector to copy to
401 * @to_offset: offset in the io vector to start copying to
402 * @len: amount of data to copy from buffer to iovec
403 *
404 * Returns 0 or -EFAULT.
405 * Note: the iovec is not modified during the copy.
406 */
407int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
408 const struct iovec *to, int to_offset,
409 int len)
410{
411 int start = skb_headlen(skb);
412 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700413 struct sk_buff *frag_iter;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000414
415 /* Copy header. */
416 if (copy > 0) {
417 if (copy > len)
418 copy = len;
419 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
420 goto fault;
421 if ((len -= copy) == 0)
422 return 0;
423 offset += copy;
424 to_offset += copy;
425 }
426
427 /* Copy paged appendix. Hmm... why does this look so complicated? */
428 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
429 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000430 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000431
432 WARN_ON(start > offset + len);
433
Eric Dumazet9e903e02011-10-18 21:00:24 +0000434 end = start + skb_frag_size(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000435 if ((copy = end - offset) > 0) {
436 int err;
437 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000438 struct page *page = skb_frag_page(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000439
440 if (copy > len)
441 copy = len;
442 vaddr = kmap(page);
443 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
444 offset - start, to_offset, copy);
445 kunmap(page);
446 if (err)
447 goto fault;
448 if (!(len -= copy))
449 return 0;
450 offset += copy;
451 to_offset += copy;
452 }
453 start = end;
454 }
455
David S. Miller5b1a0022009-06-09 00:18:15 -0700456 skb_walk_frags(skb, frag_iter) {
457 int end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000458
David S. Miller5b1a0022009-06-09 00:18:15 -0700459 WARN_ON(start > offset + len);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000460
David S. Miller5b1a0022009-06-09 00:18:15 -0700461 end = start + frag_iter->len;
462 if ((copy = end - offset) > 0) {
463 if (copy > len)
464 copy = len;
465 if (skb_copy_datagram_const_iovec(frag_iter,
466 offset - start,
467 to, to_offset,
468 copy))
469 goto fault;
470 if ((len -= copy) == 0)
471 return 0;
472 offset += copy;
473 to_offset += copy;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000474 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700475 start = end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000476 }
477 if (!len)
478 return 0;
479
480fault:
481 return -EFAULT;
482}
483EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
484
485/**
Herbert Xua8f820aa2014-11-07 21:22:22 +0800486 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
487 * @skb: buffer to copy
488 * @offset: offset in the buffer to start copying from
489 * @to: iovec iterator to copy to
490 * @len: amount of data to copy from buffer to iovec
491 */
492int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
493 struct iov_iter *to, int len)
494{
495 int start = skb_headlen(skb);
496 int i, copy = start - offset;
497 struct sk_buff *frag_iter;
498
499 trace_skb_copy_datagram_iovec(skb, len);
500
501 /* Copy header. */
502 if (copy > 0) {
503 if (copy > len)
504 copy = len;
505 if (copy_to_iter(skb->data + offset, copy, to) != copy)
506 goto short_copy;
507 if ((len -= copy) == 0)
508 return 0;
509 offset += copy;
510 }
511
512 /* Copy paged appendix. Hmm... why does this look so complicated? */
513 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
514 int end;
515 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
516
517 WARN_ON(start > offset + len);
518
519 end = start + skb_frag_size(frag);
520 if ((copy = end - offset) > 0) {
521 if (copy > len)
522 copy = len;
523 if (copy_page_to_iter(skb_frag_page(frag),
524 frag->page_offset + offset -
525 start, copy, to) != copy)
526 goto short_copy;
527 if (!(len -= copy))
528 return 0;
529 offset += copy;
530 }
531 start = end;
532 }
533
534 skb_walk_frags(skb, frag_iter) {
535 int end;
536
537 WARN_ON(start > offset + len);
538
539 end = start + frag_iter->len;
540 if ((copy = end - offset) > 0) {
541 if (copy > len)
542 copy = len;
543 if (skb_copy_datagram_iter(frag_iter, offset - start,
544 to, copy))
545 goto fault;
546 if ((len -= copy) == 0)
547 return 0;
548 offset += copy;
549 }
550 start = end;
551 }
552 if (!len)
553 return 0;
554
555 /* This is not really a user copy fault, but rather someone
556 * gave us a bogus length on the skb. We should probably
557 * print a warning here as it may indicate a kernel bug.
558 */
559
560fault:
561 return -EFAULT;
562
563short_copy:
564 if (iov_iter_count(to))
565 goto fault;
566
567 return 0;
568}
569EXPORT_SYMBOL(skb_copy_datagram_iter);
570
571/**
Rusty Russelldb543c12008-08-15 15:13:53 -0700572 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
573 * @skb: buffer to copy
574 * @offset: offset in the buffer to start copying to
575 * @from: io vector to copy to
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000576 * @from_offset: offset in the io vector to start copying from
Rusty Russelldb543c12008-08-15 15:13:53 -0700577 * @len: amount of data to copy to buffer from iovec
578 *
579 * Returns 0 or -EFAULT.
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000580 * Note: the iovec is not modified during the copy.
Rusty Russelldb543c12008-08-15 15:13:53 -0700581 */
582int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000583 const struct iovec *from, int from_offset,
584 int len)
Rusty Russelldb543c12008-08-15 15:13:53 -0700585{
586 int start = skb_headlen(skb);
587 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700588 struct sk_buff *frag_iter;
Rusty Russelldb543c12008-08-15 15:13:53 -0700589
590 /* Copy header. */
591 if (copy > 0) {
592 if (copy > len)
593 copy = len;
Sridhar Samudralad2d27bf2009-06-05 09:35:40 +0000594 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
595 copy))
Rusty Russelldb543c12008-08-15 15:13:53 -0700596 goto fault;
597 if ((len -= copy) == 0)
598 return 0;
599 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000600 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700601 }
602
603 /* Copy paged appendix. Hmm... why does this look so complicated? */
604 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
605 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000606 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Rusty Russelldb543c12008-08-15 15:13:53 -0700607
608 WARN_ON(start > offset + len);
609
Eric Dumazet9e903e02011-10-18 21:00:24 +0000610 end = start + skb_frag_size(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700611 if ((copy = end - offset) > 0) {
612 int err;
613 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000614 struct page *page = skb_frag_page(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700615
616 if (copy > len)
617 copy = len;
618 vaddr = kmap(page);
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000619 err = memcpy_fromiovecend(vaddr + frag->page_offset +
620 offset - start,
621 from, from_offset, copy);
Rusty Russelldb543c12008-08-15 15:13:53 -0700622 kunmap(page);
623 if (err)
624 goto fault;
625
626 if (!(len -= copy))
627 return 0;
628 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000629 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700630 }
631 start = end;
632 }
633
David S. Miller5b1a0022009-06-09 00:18:15 -0700634 skb_walk_frags(skb, frag_iter) {
635 int end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700636
David S. Miller5b1a0022009-06-09 00:18:15 -0700637 WARN_ON(start > offset + len);
Rusty Russelldb543c12008-08-15 15:13:53 -0700638
David S. Miller5b1a0022009-06-09 00:18:15 -0700639 end = start + frag_iter->len;
640 if ((copy = end - offset) > 0) {
641 if (copy > len)
642 copy = len;
643 if (skb_copy_datagram_from_iovec(frag_iter,
644 offset - start,
645 from,
646 from_offset,
647 copy))
648 goto fault;
649 if ((len -= copy) == 0)
650 return 0;
651 offset += copy;
652 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700653 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700654 start = end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700655 }
656 if (!len)
657 return 0;
658
659fault:
660 return -EFAULT;
661}
662EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
663
Jason Wangc3bdeb52013-08-06 17:45:04 +0800664/**
665 * zerocopy_sg_from_iovec - Build a zerocopy datagram from an iovec
666 * @skb: buffer to copy
Zhi Yong Wuc4e819d2013-10-28 14:01:49 +0800667 * @from: io vector to copy from
Jason Wangc3bdeb52013-08-06 17:45:04 +0800668 * @offset: offset in the io vector to start copying from
669 * @count: amount of vectors to copy to buffer from
670 *
671 * The function will first copy up to headlen, and then pin the userspace
672 * pages and build frags through them.
673 *
674 * Returns 0, -EFAULT or -EMSGSIZE.
675 * Note: the iovec is not modified during the copy
676 */
677int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
678 int offset, size_t count)
679{
680 int len = iov_length(from, count) - offset;
Jason Wang3d9953a2013-08-06 17:45:08 +0800681 int copy = min_t(int, skb_headlen(skb), len);
682 int size;
Jason Wangc3bdeb52013-08-06 17:45:04 +0800683 int i = 0;
684
Jason Wangc3bdeb52013-08-06 17:45:04 +0800685 /* copy up to skb headlen */
Jason Wang3d9953a2013-08-06 17:45:08 +0800686 if (skb_copy_datagram_from_iovec(skb, 0, from, offset, copy))
687 return -EFAULT;
Jason Wangc3bdeb52013-08-06 17:45:04 +0800688
Jason Wang3d9953a2013-08-06 17:45:08 +0800689 if (len == copy)
Jason Wangc3bdeb52013-08-06 17:45:04 +0800690 return 0;
691
Jason Wang3d9953a2013-08-06 17:45:08 +0800692 offset += copy;
Jason Wangc3bdeb52013-08-06 17:45:04 +0800693 while (count--) {
694 struct page *page[MAX_SKB_FRAGS];
695 int num_pages;
696 unsigned long base;
697 unsigned long truesize;
698
Jason Wang3d9953a2013-08-06 17:45:08 +0800699 /* Skip over from offset and copied */
700 if (offset >= from->iov_len) {
701 offset -= from->iov_len;
Jason Wangc3bdeb52013-08-06 17:45:04 +0800702 ++from;
703 continue;
704 }
Jason Wang3d9953a2013-08-06 17:45:08 +0800705 len = from->iov_len - offset;
Jason Wangc3bdeb52013-08-06 17:45:04 +0800706 base = (unsigned long)from->iov_base + offset;
707 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
708 if (i + size > MAX_SKB_FRAGS)
709 return -EMSGSIZE;
710 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
711 if (num_pages != size) {
Jason Wang04335472013-08-06 17:45:07 +0800712 release_pages(&page[i], num_pages, 0);
Jason Wangc3bdeb52013-08-06 17:45:04 +0800713 return -EFAULT;
714 }
715 truesize = size * PAGE_SIZE;
716 skb->data_len += len;
717 skb->len += len;
718 skb->truesize += truesize;
719 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
720 while (len) {
721 int off = base & ~PAGE_MASK;
722 int size = min_t(int, len, PAGE_SIZE - off);
Jason Wang0a57ec62013-08-06 17:45:05 +0800723 skb_fill_page_desc(skb, i, page[i], off, size);
Jason Wangc3bdeb52013-08-06 17:45:04 +0800724 base += size;
725 len -= size;
726 i++;
727 }
728 offset = 0;
729 ++from;
730 }
731 return 0;
732}
733EXPORT_SYMBOL(zerocopy_sg_from_iovec);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
736 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800737 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
David S. Miller1a028e52007-04-27 15:21:23 -0700739 int start = skb_headlen(skb);
David S. Miller1a028e52007-04-27 15:21:23 -0700740 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700741 struct sk_buff *frag_iter;
742 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* Copy header. */
745 if (copy > 0) {
746 int err = 0;
747 if (copy > len)
748 copy = len;
749 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
750 *csump, &err);
751 if (err)
752 goto fault;
753 if ((len -= copy) == 0)
754 return 0;
755 offset += copy;
756 to += copy;
757 pos = copy;
758 }
759
760 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700761 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000762 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700764 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700765
Eric Dumazet9e903e02011-10-18 21:00:24 +0000766 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800768 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int err = 0;
770 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000771 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (copy > len)
774 copy = len;
775 vaddr = kmap(page);
776 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700777 frag->page_offset +
778 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 to, copy, 0, &err);
780 kunmap(page);
781 if (err)
782 goto fault;
783 *csump = csum_block_add(*csump, csum2, pos);
784 if (!(len -= copy))
785 return 0;
786 offset += copy;
787 to += copy;
788 pos += copy;
789 }
David S. Miller1a028e52007-04-27 15:21:23 -0700790 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
David S. Miller5b1a0022009-06-09 00:18:15 -0700793 skb_walk_frags(skb, frag_iter) {
794 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David S. Miller5b1a0022009-06-09 00:18:15 -0700796 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David S. Miller5b1a0022009-06-09 00:18:15 -0700798 end = start + frag_iter->len;
799 if ((copy = end - offset) > 0) {
800 __wsum csum2 = 0;
801 if (copy > len)
802 copy = len;
803 if (skb_copy_and_csum_datagram(frag_iter,
804 offset - start,
805 to, copy,
806 &csum2))
807 goto fault;
808 *csump = csum_block_add(*csump, csum2, pos);
809 if ((len -= copy) == 0)
810 return 0;
811 offset += copy;
812 to += copy;
813 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700815 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 if (!len)
818 return 0;
819
820fault:
821 return -EFAULT;
822}
823
Herbert Xu759e5d02007-03-25 20:10:56 -0700824__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800825{
Al Virod3bc23e2006-11-14 21:24:49 -0800826 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800827
Herbert Xu759e5d02007-03-25 20:10:56 -0700828 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Tom Herbert46fb51e2014-06-14 23:24:03 -0700829 if (likely(!sum)) {
830 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
831 !skb->csum_complete_sw)
832 netdev_rx_csum_fault(skb->dev);
833 }
834 skb->csum_valid = !sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800835 return sum;
836}
Herbert Xu759e5d02007-03-25 20:10:56 -0700837EXPORT_SYMBOL(__skb_checksum_complete_head);
838
839__sum16 __skb_checksum_complete(struct sk_buff *skb)
840{
Tom Herbert46fb51e2014-06-14 23:24:03 -0700841 __wsum csum;
842 __sum16 sum;
843
844 csum = skb_checksum(skb, 0, skb->len, 0);
845
846 /* skb->csum holds pseudo checksum */
847 sum = csum_fold(csum_add(skb->csum, csum));
848 if (likely(!sum)) {
849 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
850 !skb->csum_complete_sw)
851 netdev_rx_csum_fault(skb->dev);
852 }
853
854 /* Save full packet checksum */
855 skb->csum = csum;
856 skb->ip_summed = CHECKSUM_COMPLETE;
857 skb->csum_complete_sw = 1;
858 skb->csum_valid = !sum;
859
860 return sum;
Herbert Xu759e5d02007-03-25 20:10:56 -0700861}
Herbert Xufb286bb2005-11-10 13:01:24 -0800862EXPORT_SYMBOL(__skb_checksum_complete);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/**
Masanari Iidae793c0f2014-09-04 23:44:36 +0900865 * skb_copy_and_csum_datagram_iovec - Copy and checksum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700866 * @skb: skbuff
867 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700868 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * Caller _must_ check that skb will fit to this iovec.
871 *
872 * Returns: 0 - success.
873 * -EINVAL - checksum failure.
874 * -EFAULT - fault during copy. Beware, in this case iovec
875 * can be modified!
876 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800877int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int hlen, struct iovec *iov)
879{
Al Virod3bc23e2006-11-14 21:24:49 -0800880 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int chunk = skb->len - hlen;
882
Herbert Xuef8aef52007-09-06 14:06:35 +0100883 if (!chunk)
884 return 0;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* Skip filled elements.
887 * Pretty silly, look at memcpy_toiovec, though 8)
888 */
889 while (!iov->iov_len)
890 iov++;
891
892 if (iov->iov_len < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800893 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto csum_error;
895 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
896 goto fault;
897 } else {
898 csum = csum_partial(skb->data, hlen, skb->csum);
899 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
900 chunk, &csum))
901 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800902 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700904 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800905 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 iov->iov_len -= chunk;
907 iov->iov_base += chunk;
908 }
909 return 0;
910csum_error:
911 return -EINVAL;
912fault:
913 return -EFAULT;
914}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000915EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917/**
918 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700919 * @file: file struct
920 * @sock: socket
921 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 *
923 * Datagram poll: Again totally generic. This also handles
924 * sequenced packet sockets providing the socket receive queue
925 * is only ever holding data ready to receive.
926 *
927 * Note: when you _don't_ use this routine for this protocol,
928 * and you use a different write policy from sock_writeable()
929 * then please supply your own write_space callback.
930 */
931unsigned int datagram_poll(struct file *file, struct socket *sock,
932 poll_table *wait)
933{
934 struct sock *sk = sock->sk;
935 unsigned int mask;
936
Eric Dumazetaa395142010-04-20 13:03:51 +0000937 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 mask = 0;
939
940 /* exceptional events? */
941 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000942 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -0700943 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000944
Davide Libenzif348d702006-03-25 03:07:39 -0800945 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +0000946 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (sk->sk_shutdown == SHUTDOWN_MASK)
948 mask |= POLLHUP;
949
950 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +0000951 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 mask |= POLLIN | POLLRDNORM;
953
954 /* Connection-based need to check for termination and startup */
955 if (connection_based(sk)) {
956 if (sk->sk_state == TCP_CLOSE)
957 mask |= POLLHUP;
958 /* connection hasn't started yet? */
959 if (sk->sk_state == TCP_SYN_SENT)
960 return mask;
961 }
962
963 /* writable? */
964 if (sock_writeable(sk))
965 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
966 else
967 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
968
969 return mask;
970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971EXPORT_SYMBOL(datagram_poll);