blob: 368f9c3f9dc6505e693f56066da6dcb19ec2ebd6 [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>
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>
Neil Hormane9b3cc12009-08-13 05:19:44 +000058#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * Is a socket 'connection oriented' ?
62 */
63static inline int connection_based(struct sock *sk)
64{
65 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
66}
67
Eric Dumazet95c96172012-04-15 05:58:06 +000068static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
Eric Dumazetbf368e42009-04-28 02:24:21 -070069 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * Wait for a packet..
82 */
83static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
84{
85 int error;
Eric Dumazetbf368e42009-04-28 02:24:21 -070086 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Eric Dumazetaa395142010-04-20 13:03:51 +000088 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
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);
116out:
Eric Dumazetaa395142010-04-20 13:03:51 +0000117 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return error;
119interrupted:
120 error = sock_intr_errno(*timeo_p);
121out_err:
122 *err = error;
123 goto out;
124out_noerr:
125 *err = 0;
126 error = 1;
127 goto out;
128}
129
130/**
Herbert Xua59322b2007-12-05 01:53:40 -0800131 * __skb_recv_datagram - Receive a datagram skbuff
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700132 * @sk: socket
133 * @flags: MSG_ flags
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000134 * @off: an offset in bytes to peek skb from. Returns an offset
135 * within an skb where data actually starts
Herbert Xua59322b2007-12-05 01:53:40 -0800136 * @peeked: returns non-zero if this packet has been seen before
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700137 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
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 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000161struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000162 int *peeked, int *off, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
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
Herbert Xua59322b2007-12-05 01:53:40 -0800174 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
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...
David Shwatrz8917a3c2010-12-02 09:01:55 +0000181 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Herbert Xua59322b2007-12-05 01:53:40 -0800183 unsigned long cpu_flags;
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000184 struct sk_buff_head *queue = &sk->sk_receive_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000186 spin_lock_irqsave(&queue->lock, cpu_flags);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000187 skb_queue_walk(queue, skb) {
Herbert Xua59322b2007-12-05 01:53:40 -0800188 *peeked = skb->peeked;
189 if (flags & MSG_PEEK) {
Eric Dumazet77c10902013-02-12 06:16:53 +0000190 if (*off >= skb->len && skb->len) {
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000191 *off -= skb->len;
192 continue;
193 }
Herbert Xua59322b2007-12-05 01:53:40 -0800194 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800196 } else
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000197 __skb_unlink(skb, queue);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000198
199 spin_unlock_irqrestore(&queue->lock, cpu_flags);
200 return skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800201 }
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000202 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* 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
213no_packet:
214 *err = error;
215 return NULL;
216}
Herbert Xua59322b2007-12-05 01:53:40 -0800217EXPORT_SYMBOL(__skb_recv_datagram);
218
Eric Dumazet95c96172012-04-15 05:58:06 +0000219struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
Herbert Xua59322b2007-12-05 01:53:40 -0800220 int noblock, int *err)
221{
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000222 int peeked, off = 0;
Herbert Xua59322b2007-12-05 01:53:40 -0800223
224 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000225 &peeked, &off, err);
Herbert Xua59322b2007-12-05 01:53:40 -0800226}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000227EXPORT_SYMBOL(skb_recv_datagram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
230{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000231 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800232 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Eric Dumazet9d410c72009-10-30 05:03:53 +0000234EXPORT_SYMBOL(skb_free_datagram);
235
236void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
237{
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000238 bool slow;
239
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700240 if (likely(atomic_read(&skb->users) == 1))
241 smp_rmb();
242 else if (likely(!atomic_dec_and_test(&skb->users)))
243 return;
244
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000245 slow = lock_sock_fast(sk);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700246 skb_orphan(skb);
247 sk_mem_reclaim_partial(sk);
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000248 unlock_sock_fast(sk, slow);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700249
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700250 /* skb is now orphaned, can be freed outside of locked section */
251 __kfree_skb(skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000252}
253EXPORT_SYMBOL(skb_free_datagram_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/**
Herbert Xu3305b802005-12-13 23:16:37 -0800256 * skb_kill_datagram - Free a datagram skbuff forcibly
257 * @sk: socket
258 * @skb: datagram skbuff
259 * @flags: MSG_ flags
260 *
261 * This function frees a datagram skbuff that was received by
262 * skb_recv_datagram. The flags argument must match the one
263 * used for skb_recv_datagram.
264 *
265 * If the MSG_PEEK flag is set, and the packet is still on the
266 * receive queue of the socket, it will be taken off the queue
267 * before it is freed.
268 *
269 * This function currently only disables BH when acquiring the
270 * sk_receive_queue lock. Therefore it must not be used in a
271 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800272 *
273 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800274 */
275
Herbert Xu27ab2562007-12-05 01:51:58 -0800276int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800277{
Herbert Xu27ab2562007-12-05 01:51:58 -0800278 int err = 0;
279
Herbert Xu3305b802005-12-13 23:16:37 -0800280 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800281 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800282 spin_lock_bh(&sk->sk_receive_queue.lock);
283 if (skb == skb_peek(&sk->sk_receive_queue)) {
284 __skb_unlink(skb, &sk->sk_receive_queue);
285 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800286 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800287 }
288 spin_unlock_bh(&sk->sk_receive_queue.lock);
289 }
290
John Dykstra61de71c2009-05-08 14:57:01 -0700291 kfree_skb(skb);
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000292 atomic_inc(&sk->sk_drops);
John Dykstra61de71c2009-05-08 14:57:01 -0700293 sk_mem_reclaim_partial(sk);
294
Herbert Xu27ab2562007-12-05 01:51:58 -0800295 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800296}
Herbert Xu3305b802005-12-13 23:16:37 -0800297EXPORT_SYMBOL(skb_kill_datagram);
298
299/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700301 * @skb: buffer to copy
302 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700303 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700304 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
306 * Note: the iovec is modified during the copy.
307 */
308int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
309 struct iovec *to, int len)
310{
David S. Miller1a028e52007-04-27 15:21:23 -0700311 int start = skb_headlen(skb);
312 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700313 struct sk_buff *frag_iter;
Herbert Xuc75d7212005-11-02 18:55:00 +1100314
Neil Hormane9b3cc12009-08-13 05:19:44 +0000315 trace_skb_copy_datagram_iovec(skb, len);
316
David S. Millerb4d9eda2006-02-13 16:06:10 -0800317 /* Copy header. */
318 if (copy > 0) {
319 if (copy > len)
320 copy = len;
321 if (memcpy_toiovec(to, skb->data + offset, copy))
322 goto fault;
323 if ((len -= copy) == 0)
324 return 0;
325 offset += copy;
326 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100327
David S. Millerb4d9eda2006-02-13 16:06:10 -0800328 /* Copy paged appendix. Hmm... why does this look so complicated? */
329 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700330 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000331 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700333 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700334
Eric Dumazet9e903e02011-10-18 21:00:24 +0000335 end = start + skb_frag_size(frag);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800336 if ((copy = end - offset) > 0) {
337 int err;
338 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000339 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (copy > len)
342 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800343 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700344 err = memcpy_toiovec(to, vaddr + frag->page_offset +
345 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800346 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (err)
348 goto fault;
349 if (!(len -= copy))
350 return 0;
351 offset += copy;
352 }
David S. Miller1a028e52007-04-27 15:21:23 -0700353 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800355
David S. Miller5b1a0022009-06-09 00:18:15 -0700356 skb_walk_frags(skb, frag_iter) {
357 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800358
David S. Miller5b1a0022009-06-09 00:18:15 -0700359 WARN_ON(start > offset + len);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800360
David S. Miller5b1a0022009-06-09 00:18:15 -0700361 end = start + frag_iter->len;
362 if ((copy = end - offset) > 0) {
363 if (copy > len)
364 copy = len;
365 if (skb_copy_datagram_iovec(frag_iter,
366 offset - start,
367 to, copy))
368 goto fault;
369 if ((len -= copy) == 0)
370 return 0;
371 offset += copy;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800372 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700373 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800375 if (!len)
376 return 0;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378fault:
379 return -EFAULT;
380}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000381EXPORT_SYMBOL(skb_copy_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Rusty Russelldb543c12008-08-15 15:13:53 -0700383/**
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000384 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
385 * @skb: buffer to copy
386 * @offset: offset in the buffer to start copying from
387 * @to: io vector to copy to
388 * @to_offset: offset in the io vector to start copying to
389 * @len: amount of data to copy from buffer to iovec
390 *
391 * Returns 0 or -EFAULT.
392 * Note: the iovec is not modified during the copy.
393 */
394int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
395 const struct iovec *to, int to_offset,
396 int len)
397{
398 int start = skb_headlen(skb);
399 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700400 struct sk_buff *frag_iter;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000401
402 /* Copy header. */
403 if (copy > 0) {
404 if (copy > len)
405 copy = len;
406 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
407 goto fault;
408 if ((len -= copy) == 0)
409 return 0;
410 offset += copy;
411 to_offset += copy;
412 }
413
414 /* Copy paged appendix. Hmm... why does this look so complicated? */
415 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
416 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000417 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000418
419 WARN_ON(start > offset + len);
420
Eric Dumazet9e903e02011-10-18 21:00:24 +0000421 end = start + skb_frag_size(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000422 if ((copy = end - offset) > 0) {
423 int err;
424 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000425 struct page *page = skb_frag_page(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000426
427 if (copy > len)
428 copy = len;
429 vaddr = kmap(page);
430 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
431 offset - start, to_offset, copy);
432 kunmap(page);
433 if (err)
434 goto fault;
435 if (!(len -= copy))
436 return 0;
437 offset += copy;
438 to_offset += copy;
439 }
440 start = end;
441 }
442
David S. Miller5b1a0022009-06-09 00:18:15 -0700443 skb_walk_frags(skb, frag_iter) {
444 int end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000445
David S. Miller5b1a0022009-06-09 00:18:15 -0700446 WARN_ON(start > offset + len);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000447
David S. Miller5b1a0022009-06-09 00:18:15 -0700448 end = start + frag_iter->len;
449 if ((copy = end - offset) > 0) {
450 if (copy > len)
451 copy = len;
452 if (skb_copy_datagram_const_iovec(frag_iter,
453 offset - start,
454 to, to_offset,
455 copy))
456 goto fault;
457 if ((len -= copy) == 0)
458 return 0;
459 offset += copy;
460 to_offset += copy;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000461 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700462 start = end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000463 }
464 if (!len)
465 return 0;
466
467fault:
468 return -EFAULT;
469}
470EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
471
472/**
Rusty Russelldb543c12008-08-15 15:13:53 -0700473 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
474 * @skb: buffer to copy
475 * @offset: offset in the buffer to start copying to
476 * @from: io vector to copy to
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000477 * @from_offset: offset in the io vector to start copying from
Rusty Russelldb543c12008-08-15 15:13:53 -0700478 * @len: amount of data to copy to buffer from iovec
479 *
480 * Returns 0 or -EFAULT.
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000481 * Note: the iovec is not modified during the copy.
Rusty Russelldb543c12008-08-15 15:13:53 -0700482 */
483int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000484 const struct iovec *from, int from_offset,
485 int len)
Rusty Russelldb543c12008-08-15 15:13:53 -0700486{
487 int start = skb_headlen(skb);
488 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700489 struct sk_buff *frag_iter;
Rusty Russelldb543c12008-08-15 15:13:53 -0700490
491 /* Copy header. */
492 if (copy > 0) {
493 if (copy > len)
494 copy = len;
Sridhar Samudralad2d27bf2009-06-05 09:35:40 +0000495 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
496 copy))
Rusty Russelldb543c12008-08-15 15:13:53 -0700497 goto fault;
498 if ((len -= copy) == 0)
499 return 0;
500 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000501 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700502 }
503
504 /* Copy paged appendix. Hmm... why does this look so complicated? */
505 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
506 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000507 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Rusty Russelldb543c12008-08-15 15:13:53 -0700508
509 WARN_ON(start > offset + len);
510
Eric Dumazet9e903e02011-10-18 21:00:24 +0000511 end = start + skb_frag_size(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700512 if ((copy = end - offset) > 0) {
513 int err;
514 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000515 struct page *page = skb_frag_page(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700516
517 if (copy > len)
518 copy = len;
519 vaddr = kmap(page);
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000520 err = memcpy_fromiovecend(vaddr + frag->page_offset +
521 offset - start,
522 from, from_offset, copy);
Rusty Russelldb543c12008-08-15 15:13:53 -0700523 kunmap(page);
524 if (err)
525 goto fault;
526
527 if (!(len -= copy))
528 return 0;
529 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000530 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700531 }
532 start = end;
533 }
534
David S. Miller5b1a0022009-06-09 00:18:15 -0700535 skb_walk_frags(skb, frag_iter) {
536 int end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700537
David S. Miller5b1a0022009-06-09 00:18:15 -0700538 WARN_ON(start > offset + len);
Rusty Russelldb543c12008-08-15 15:13:53 -0700539
David S. Miller5b1a0022009-06-09 00:18:15 -0700540 end = start + frag_iter->len;
541 if ((copy = end - offset) > 0) {
542 if (copy > len)
543 copy = len;
544 if (skb_copy_datagram_from_iovec(frag_iter,
545 offset - start,
546 from,
547 from_offset,
548 copy))
549 goto fault;
550 if ((len -= copy) == 0)
551 return 0;
552 offset += copy;
553 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700554 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700555 start = end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700556 }
557 if (!len)
558 return 0;
559
560fault:
561 return -EFAULT;
562}
563EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
566 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800567 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
David S. Miller1a028e52007-04-27 15:21:23 -0700569 int start = skb_headlen(skb);
David S. Miller1a028e52007-04-27 15:21:23 -0700570 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700571 struct sk_buff *frag_iter;
572 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Copy header. */
575 if (copy > 0) {
576 int err = 0;
577 if (copy > len)
578 copy = len;
579 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
580 *csump, &err);
581 if (err)
582 goto fault;
583 if ((len -= copy) == 0)
584 return 0;
585 offset += copy;
586 to += copy;
587 pos = copy;
588 }
589
590 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700591 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000592 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700594 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700595
Eric Dumazet9e903e02011-10-18 21:00:24 +0000596 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800598 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int err = 0;
600 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000601 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (copy > len)
604 copy = len;
605 vaddr = kmap(page);
606 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700607 frag->page_offset +
608 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 to, copy, 0, &err);
610 kunmap(page);
611 if (err)
612 goto fault;
613 *csump = csum_block_add(*csump, csum2, pos);
614 if (!(len -= copy))
615 return 0;
616 offset += copy;
617 to += copy;
618 pos += copy;
619 }
David S. Miller1a028e52007-04-27 15:21:23 -0700620 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
David S. Miller5b1a0022009-06-09 00:18:15 -0700623 skb_walk_frags(skb, frag_iter) {
624 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David S. Miller5b1a0022009-06-09 00:18:15 -0700626 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David S. Miller5b1a0022009-06-09 00:18:15 -0700628 end = start + frag_iter->len;
629 if ((copy = end - offset) > 0) {
630 __wsum csum2 = 0;
631 if (copy > len)
632 copy = len;
633 if (skb_copy_and_csum_datagram(frag_iter,
634 offset - start,
635 to, copy,
636 &csum2))
637 goto fault;
638 *csump = csum_block_add(*csump, csum2, pos);
639 if ((len -= copy) == 0)
640 return 0;
641 offset += copy;
642 to += copy;
643 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700645 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 if (!len)
648 return 0;
649
650fault:
651 return -EFAULT;
652}
653
Herbert Xu759e5d02007-03-25 20:10:56 -0700654__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800655{
Al Virod3bc23e2006-11-14 21:24:49 -0800656 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800657
Herbert Xu759e5d02007-03-25 20:10:56 -0700658 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Herbert Xufb286bb2005-11-10 13:01:24 -0800659 if (likely(!sum)) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700660 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800661 netdev_rx_csum_fault(skb->dev);
662 skb->ip_summed = CHECKSUM_UNNECESSARY;
663 }
664 return sum;
665}
Herbert Xu759e5d02007-03-25 20:10:56 -0700666EXPORT_SYMBOL(__skb_checksum_complete_head);
667
668__sum16 __skb_checksum_complete(struct sk_buff *skb)
669{
670 return __skb_checksum_complete_head(skb, skb->len);
671}
Herbert Xufb286bb2005-11-10 13:01:24 -0800672EXPORT_SYMBOL(__skb_checksum_complete);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
675 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700676 * @skb: skbuff
677 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700678 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900679 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * Caller _must_ check that skb will fit to this iovec.
681 *
682 * Returns: 0 - success.
683 * -EINVAL - checksum failure.
684 * -EFAULT - fault during copy. Beware, in this case iovec
685 * can be modified!
686 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800687int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int hlen, struct iovec *iov)
689{
Al Virod3bc23e2006-11-14 21:24:49 -0800690 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int chunk = skb->len - hlen;
692
Herbert Xuef8aef52007-09-06 14:06:35 +0100693 if (!chunk)
694 return 0;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Skip filled elements.
697 * Pretty silly, look at memcpy_toiovec, though 8)
698 */
699 while (!iov->iov_len)
700 iov++;
701
702 if (iov->iov_len < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800703 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto csum_error;
705 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
706 goto fault;
707 } else {
708 csum = csum_partial(skb->data, hlen, skb->csum);
709 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
710 chunk, &csum))
711 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800712 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700714 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800715 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 iov->iov_len -= chunk;
717 iov->iov_base += chunk;
718 }
719 return 0;
720csum_error:
721 return -EINVAL;
722fault:
723 return -EFAULT;
724}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000725EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727/**
728 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700729 * @file: file struct
730 * @sock: socket
731 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
733 * Datagram poll: Again totally generic. This also handles
734 * sequenced packet sockets providing the socket receive queue
735 * is only ever holding data ready to receive.
736 *
737 * Note: when you _don't_ use this routine for this protocol,
738 * and you use a different write policy from sock_writeable()
739 * then please supply your own write_space callback.
740 */
741unsigned int datagram_poll(struct file *file, struct socket *sock,
742 poll_table *wait)
743{
744 struct sock *sk = sock->sk;
745 unsigned int mask;
746
Eric Dumazetaa395142010-04-20 13:03:51 +0000747 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 mask = 0;
749
750 /* exceptional events? */
751 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
752 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -0800753 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +0000754 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (sk->sk_shutdown == SHUTDOWN_MASK)
756 mask |= POLLHUP;
757
758 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +0000759 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 mask |= POLLIN | POLLRDNORM;
761
762 /* Connection-based need to check for termination and startup */
763 if (connection_based(sk)) {
764 if (sk->sk_state == TCP_CLOSE)
765 mask |= POLLHUP;
766 /* connection hasn't started yet? */
767 if (sk->sk_state == TCP_SYN_SENT)
768 return mask;
769 }
770
771 /* writable? */
772 if (sock_writeable(sk))
773 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
774 else
775 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
776
777 return mask;
778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779EXPORT_SYMBOL(datagram_poll);