blob: 99c4f525b1d90e1749333afe4f8062029a837dae [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) {
Benjamin Poirieradd05ad2013-04-29 11:42:12 +0000190 if (*off >= skb->len && (skb->len || *off ||
191 skb->peeked)) {
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000192 *off -= skb->len;
193 continue;
194 }
Herbert Xua59322b2007-12-05 01:53:40 -0800195 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800197 } else
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000198 __skb_unlink(skb, queue);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000199
200 spin_unlock_irqrestore(&queue->lock, cpu_flags);
201 return skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800202 }
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000203 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* User doesn't want to wait */
206 error = -EAGAIN;
207 if (!timeo)
208 goto no_packet;
209
210 } while (!wait_for_packet(sk, err, &timeo));
211
212 return NULL;
213
214no_packet:
215 *err = error;
216 return NULL;
217}
Herbert Xua59322b2007-12-05 01:53:40 -0800218EXPORT_SYMBOL(__skb_recv_datagram);
219
Eric Dumazet95c96172012-04-15 05:58:06 +0000220struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
Herbert Xua59322b2007-12-05 01:53:40 -0800221 int noblock, int *err)
222{
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000223 int peeked, off = 0;
Herbert Xua59322b2007-12-05 01:53:40 -0800224
225 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000226 &peeked, &off, err);
Herbert Xua59322b2007-12-05 01:53:40 -0800227}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000228EXPORT_SYMBOL(skb_recv_datagram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
231{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000232 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800233 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
Eric Dumazet9d410c72009-10-30 05:03:53 +0000235EXPORT_SYMBOL(skb_free_datagram);
236
237void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
238{
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000239 bool slow;
240
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700241 if (likely(atomic_read(&skb->users) == 1))
242 smp_rmb();
243 else if (likely(!atomic_dec_and_test(&skb->users)))
244 return;
245
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000246 slow = lock_sock_fast(sk);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700247 skb_orphan(skb);
248 sk_mem_reclaim_partial(sk);
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000249 unlock_sock_fast(sk, slow);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700250
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700251 /* skb is now orphaned, can be freed outside of locked section */
252 __kfree_skb(skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000253}
254EXPORT_SYMBOL(skb_free_datagram_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256/**
Herbert Xu3305b802005-12-13 23:16:37 -0800257 * 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.
Herbert Xu27ab2562007-12-05 01:51:58 -0800273 *
274 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800275 */
276
Herbert Xu27ab2562007-12-05 01:51:58 -0800277int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800278{
Herbert Xu27ab2562007-12-05 01:51:58 -0800279 int err = 0;
280
Herbert Xu3305b802005-12-13 23:16:37 -0800281 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800282 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800283 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);
Herbert Xu27ab2562007-12-05 01:51:58 -0800287 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800288 }
289 spin_unlock_bh(&sk->sk_receive_queue.lock);
290 }
291
John Dykstra61de71c2009-05-08 14:57:01 -0700292 kfree_skb(skb);
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000293 atomic_inc(&sk->sk_drops);
John Dykstra61de71c2009-05-08 14:57:01 -0700294 sk_mem_reclaim_partial(sk);
295
Herbert Xu27ab2562007-12-05 01:51:58 -0800296 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800297}
Herbert Xu3305b802005-12-13 23:16:37 -0800298EXPORT_SYMBOL(skb_kill_datagram);
299
300/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700302 * @skb: buffer to copy
303 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700304 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700305 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * Note: the iovec is modified during the copy.
308 */
309int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
310 struct iovec *to, int len)
311{
David S. Miller1a028e52007-04-27 15:21:23 -0700312 int start = skb_headlen(skb);
313 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700314 struct sk_buff *frag_iter;
Herbert Xuc75d7212005-11-02 18:55:00 +1100315
Neil Hormane9b3cc12009-08-13 05:19:44 +0000316 trace_skb_copy_datagram_iovec(skb, len);
317
David S. Millerb4d9eda2006-02-13 16:06:10 -0800318 /* 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 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100328
David S. Millerb4d9eda2006-02-13 16:06:10 -0800329 /* Copy paged appendix. Hmm... why does this look so complicated? */
330 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700331 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000332 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700334 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700335
Eric Dumazet9e903e02011-10-18 21:00:24 +0000336 end = start + skb_frag_size(frag);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800337 if ((copy = end - offset) > 0) {
338 int err;
339 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000340 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (copy > len)
343 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800344 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700345 err = memcpy_toiovec(to, vaddr + frag->page_offset +
346 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800347 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (err)
349 goto fault;
350 if (!(len -= copy))
351 return 0;
352 offset += copy;
353 }
David S. Miller1a028e52007-04-27 15:21:23 -0700354 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800356
David S. Miller5b1a0022009-06-09 00:18:15 -0700357 skb_walk_frags(skb, frag_iter) {
358 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800359
David S. Miller5b1a0022009-06-09 00:18:15 -0700360 WARN_ON(start > offset + len);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800361
David S. Miller5b1a0022009-06-09 00:18:15 -0700362 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;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800373 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700374 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800376 if (!len)
377 return 0;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379fault:
380 return -EFAULT;
381}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000382EXPORT_SYMBOL(skb_copy_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Rusty Russelldb543c12008-08-15 15:13:53 -0700384/**
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000385 * 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 */
395int 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;
David S. Miller5b1a0022009-06-09 00:18:15 -0700401 struct sk_buff *frag_iter;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000402
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;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000418 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000419
420 WARN_ON(start > offset + len);
421
Eric Dumazet9e903e02011-10-18 21:00:24 +0000422 end = start + skb_frag_size(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000423 if ((copy = end - offset) > 0) {
424 int err;
425 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000426 struct page *page = skb_frag_page(frag);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000427
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
David S. Miller5b1a0022009-06-09 00:18:15 -0700444 skb_walk_frags(skb, frag_iter) {
445 int end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000446
David S. Miller5b1a0022009-06-09 00:18:15 -0700447 WARN_ON(start > offset + len);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000448
David S. Miller5b1a0022009-06-09 00:18:15 -0700449 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;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000462 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700463 start = end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000464 }
465 if (!len)
466 return 0;
467
468fault:
469 return -EFAULT;
470}
471EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
472
473/**
Rusty Russelldb543c12008-08-15 15:13:53 -0700474 * 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
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000478 * @from_offset: offset in the io vector to start copying from
Rusty Russelldb543c12008-08-15 15:13:53 -0700479 * @len: amount of data to copy to buffer from iovec
480 *
481 * Returns 0 or -EFAULT.
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000482 * Note: the iovec is not modified during the copy.
Rusty Russelldb543c12008-08-15 15:13:53 -0700483 */
484int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000485 const struct iovec *from, int from_offset,
486 int len)
Rusty Russelldb543c12008-08-15 15:13:53 -0700487{
488 int start = skb_headlen(skb);
489 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700490 struct sk_buff *frag_iter;
Rusty Russelldb543c12008-08-15 15:13:53 -0700491
492 /* Copy header. */
493 if (copy > 0) {
494 if (copy > len)
495 copy = len;
Sridhar Samudralad2d27bf2009-06-05 09:35:40 +0000496 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
497 copy))
Rusty Russelldb543c12008-08-15 15:13:53 -0700498 goto fault;
499 if ((len -= copy) == 0)
500 return 0;
501 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000502 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700503 }
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;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000508 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Rusty Russelldb543c12008-08-15 15:13:53 -0700509
510 WARN_ON(start > offset + len);
511
Eric Dumazet9e903e02011-10-18 21:00:24 +0000512 end = start + skb_frag_size(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700513 if ((copy = end - offset) > 0) {
514 int err;
515 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000516 struct page *page = skb_frag_page(frag);
Rusty Russelldb543c12008-08-15 15:13:53 -0700517
518 if (copy > len)
519 copy = len;
520 vaddr = kmap(page);
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000521 err = memcpy_fromiovecend(vaddr + frag->page_offset +
522 offset - start,
523 from, from_offset, copy);
Rusty Russelldb543c12008-08-15 15:13:53 -0700524 kunmap(page);
525 if (err)
526 goto fault;
527
528 if (!(len -= copy))
529 return 0;
530 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000531 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700532 }
533 start = end;
534 }
535
David S. Miller5b1a0022009-06-09 00:18:15 -0700536 skb_walk_frags(skb, frag_iter) {
537 int end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700538
David S. Miller5b1a0022009-06-09 00:18:15 -0700539 WARN_ON(start > offset + len);
Rusty Russelldb543c12008-08-15 15:13:53 -0700540
David S. Miller5b1a0022009-06-09 00:18:15 -0700541 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;
Rusty Russelldb543c12008-08-15 15:13:53 -0700555 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700556 start = end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700557 }
558 if (!len)
559 return 0;
560
561fault:
562 return -EFAULT;
563}
564EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
567 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800568 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
David S. Miller1a028e52007-04-27 15:21:23 -0700570 int start = skb_headlen(skb);
David S. Miller1a028e52007-04-27 15:21:23 -0700571 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700572 struct sk_buff *frag_iter;
573 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
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++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700592 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000593 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700595 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700596
Eric Dumazet9e903e02011-10-18 21:00:24 +0000597 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800599 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int err = 0;
601 u8 *vaddr;
Ian Campbellea2ab692011-08-22 23:44:58 +0000602 struct page *page = skb_frag_page(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (copy > len)
605 copy = len;
606 vaddr = kmap(page);
607 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700608 frag->page_offset +
609 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 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 }
David S. Miller1a028e52007-04-27 15:21:23 -0700621 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
David S. Miller5b1a0022009-06-09 00:18:15 -0700624 skb_walk_frags(skb, frag_iter) {
625 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David S. Miller5b1a0022009-06-09 00:18:15 -0700627 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
David S. Miller5b1a0022009-06-09 00:18:15 -0700629 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700646 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 if (!len)
649 return 0;
650
651fault:
652 return -EFAULT;
653}
654
Herbert Xu759e5d02007-03-25 20:10:56 -0700655__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800656{
Al Virod3bc23e2006-11-14 21:24:49 -0800657 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800658
Herbert Xu759e5d02007-03-25 20:10:56 -0700659 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Herbert Xufb286bb2005-11-10 13:01:24 -0800660 if (likely(!sum)) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700661 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800662 netdev_rx_csum_fault(skb->dev);
663 skb->ip_summed = CHECKSUM_UNNECESSARY;
664 }
665 return sum;
666}
Herbert Xu759e5d02007-03-25 20:10:56 -0700667EXPORT_SYMBOL(__skb_checksum_complete_head);
668
669__sum16 __skb_checksum_complete(struct sk_buff *skb)
670{
671 return __skb_checksum_complete_head(skb, skb->len);
672}
Herbert Xufb286bb2005-11-10 13:01:24 -0800673EXPORT_SYMBOL(__skb_checksum_complete);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
676 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700677 * @skb: skbuff
678 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700679 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * 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 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800688int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int hlen, struct iovec *iov)
690{
Al Virod3bc23e2006-11-14 21:24:49 -0800691 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int chunk = skb->len - hlen;
693
Herbert Xuef8aef52007-09-06 14:06:35 +0100694 if (!chunk)
695 return 0;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* 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) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800704 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 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;
Al Virod3bc23e2006-11-14 21:24:49 -0800713 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700715 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800716 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 iov->iov_len -= chunk;
718 iov->iov_base += chunk;
719 }
720 return 0;
721csum_error:
722 return -EINVAL;
723fault:
724 return -EFAULT;
725}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000726EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728/**
729 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700730 * @file: file struct
731 * @sock: socket
732 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
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 */
742unsigned 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
Eric Dumazetaa395142010-04-20 13:03:51 +0000748 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mask = 0;
750
751 /* exceptional events? */
752 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
753 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -0800754 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +0000755 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (sk->sk_shutdown == SHUTDOWN_MASK)
757 mask |= POLLHUP;
758
759 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +0000760 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780EXPORT_SYMBOL(datagram_poll);