blob: 4ade3011bb3c2bd4d270cfb10cdcf5e112402ce9 [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>
40#include <asm/system.h>
41#include <linux/mm.h>
42#include <linux/interrupt.h>
43#include <linux/errno.h>
44#include <linux/sched.h>
45#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/netdevice.h>
47#include <linux/rtnetlink.h>
48#include <linux/poll.h>
49#include <linux/highmem.h>
Herbert Xu3305b802005-12-13 23:16:37 -080050#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070055#include <net/checksum.h>
56#include <net/sock.h>
57#include <net/tcp_states.h>
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 Dumazetbf368e42009-04-28 02:24:21 -070068static int receiver_wake_function(wait_queue_t *wait, unsigned mode, int sync,
69 void *key)
70{
71 unsigned long bits = (unsigned long)key;
72
73 /*
74 * Avoid a wakeup if event not interesting for us
75 */
76 if (bits && !(bits & (POLLIN | POLLERR)))
77 return 0;
78 return autoremove_wake_function(wait, mode, sync, key);
79}
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
88 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
89
90 /* Socket errors? */
91 error = sock_error(sk);
92 if (error)
93 goto out_err;
94
95 if (!skb_queue_empty(&sk->sk_receive_queue))
96 goto out;
97
98 /* Socket shut down? */
99 if (sk->sk_shutdown & RCV_SHUTDOWN)
100 goto out_noerr;
101
102 /* Sequenced packets can come disconnected.
103 * If so we report the problem
104 */
105 error = -ENOTCONN;
106 if (connection_based(sk) &&
107 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
108 goto out_err;
109
110 /* handle signals */
111 if (signal_pending(current))
112 goto interrupted;
113
114 error = 0;
115 *timeo_p = schedule_timeout(*timeo_p);
116out:
117 finish_wait(sk->sk_sleep, &wait);
118 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
Herbert Xua59322b2007-12-05 01:53:40 -0800134 * @peeked: returns non-zero if this packet has been seen before
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700135 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
137 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
138 * and possible races. This replaces identical code in packet, raw and
139 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
140 * the long standing peek and read race for datagram sockets. If you
141 * alter this routine remember it must be re-entrant.
142 *
143 * This function will lock the socket if a skb is returned, so the caller
144 * needs to unlock the socket in that case (usually by calling
145 * skb_free_datagram)
146 *
147 * * It does not lock socket since today. This function is
148 * * free of race conditions. This measure should/can improve
149 * * significantly datagram socket latencies at high loads,
150 * * when data copying to user space takes lots of time.
151 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
152 * * 8) Great win.)
153 * * --ANK (980729)
154 *
155 * The order of the tests when we find no data waiting are specified
156 * quite explicitly by POSIX 1003.1g, don't change them without having
157 * the standard around please.
158 */
Herbert Xua59322b2007-12-05 01:53:40 -0800159struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
160 int *peeked, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct sk_buff *skb;
163 long timeo;
164 /*
165 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
166 */
167 int error = sock_error(sk);
168
169 if (error)
170 goto no_packet;
171
Herbert Xua59322b2007-12-05 01:53:40 -0800172 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 do {
175 /* Again only user level code calls this function, so nothing
176 * interrupt level will suddenly eat the receive_queue.
177 *
178 * Look at current nfs client by the way...
179 * However, this function was corrent in any case. 8)
180 */
Herbert Xua59322b2007-12-05 01:53:40 -0800181 unsigned long cpu_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Herbert Xua59322b2007-12-05 01:53:40 -0800183 spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
184 skb = skb_peek(&sk->sk_receive_queue);
185 if (skb) {
186 *peeked = skb->peeked;
187 if (flags & MSG_PEEK) {
188 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800190 } else
191 __skb_unlink(skb, &sk->sk_receive_queue);
192 }
193 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (skb)
196 return skb;
197
198 /* User doesn't want to wait */
199 error = -EAGAIN;
200 if (!timeo)
201 goto no_packet;
202
203 } while (!wait_for_packet(sk, err, &timeo));
204
205 return NULL;
206
207no_packet:
208 *err = error;
209 return NULL;
210}
Herbert Xua59322b2007-12-05 01:53:40 -0800211EXPORT_SYMBOL(__skb_recv_datagram);
212
213struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
214 int noblock, int *err)
215{
216 int peeked;
217
218 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
219 &peeked, err);
220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
223{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000224 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800225 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Eric Dumazet9d410c72009-10-30 05:03:53 +0000227EXPORT_SYMBOL(skb_free_datagram);
228
229void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
230{
231 lock_sock(sk);
232 skb_free_datagram(sk, skb);
233 release_sock(sk);
234}
235EXPORT_SYMBOL(skb_free_datagram_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237/**
Herbert Xu3305b802005-12-13 23:16:37 -0800238 * skb_kill_datagram - Free a datagram skbuff forcibly
239 * @sk: socket
240 * @skb: datagram skbuff
241 * @flags: MSG_ flags
242 *
243 * This function frees a datagram skbuff that was received by
244 * skb_recv_datagram. The flags argument must match the one
245 * used for skb_recv_datagram.
246 *
247 * If the MSG_PEEK flag is set, and the packet is still on the
248 * receive queue of the socket, it will be taken off the queue
249 * before it is freed.
250 *
251 * This function currently only disables BH when acquiring the
252 * sk_receive_queue lock. Therefore it must not be used in a
253 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800254 *
255 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800256 */
257
Herbert Xu27ab2562007-12-05 01:51:58 -0800258int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800259{
Herbert Xu27ab2562007-12-05 01:51:58 -0800260 int err = 0;
261
Herbert Xu3305b802005-12-13 23:16:37 -0800262 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800263 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800264 spin_lock_bh(&sk->sk_receive_queue.lock);
265 if (skb == skb_peek(&sk->sk_receive_queue)) {
266 __skb_unlink(skb, &sk->sk_receive_queue);
267 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800268 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800269 }
270 spin_unlock_bh(&sk->sk_receive_queue.lock);
271 }
272
John Dykstra61de71c2009-05-08 14:57:01 -0700273 kfree_skb(skb);
274 sk_mem_reclaim_partial(sk);
275
Herbert Xu27ab2562007-12-05 01:51:58 -0800276 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800277}
278
279EXPORT_SYMBOL(skb_kill_datagram);
280
281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700283 * @skb: buffer to copy
284 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700285 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700286 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
288 * Note: the iovec is modified during the copy.
289 */
290int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
291 struct iovec *to, int len)
292{
David S. Miller1a028e52007-04-27 15:21:23 -0700293 int start = skb_headlen(skb);
294 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700295 struct sk_buff *frag_iter;
Herbert Xuc75d7212005-11-02 18:55:00 +1100296
Neil Hormane9b3cc12009-08-13 05:19:44 +0000297 trace_skb_copy_datagram_iovec(skb, len);
298
David S. Millerb4d9eda2006-02-13 16:06:10 -0800299 /* Copy header. */
300 if (copy > 0) {
301 if (copy > len)
302 copy = len;
303 if (memcpy_toiovec(to, skb->data + offset, copy))
304 goto fault;
305 if ((len -= copy) == 0)
306 return 0;
307 offset += copy;
308 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100309
David S. Millerb4d9eda2006-02-13 16:06:10 -0800310 /* Copy paged appendix. Hmm... why does this look so complicated? */
311 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700312 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700314 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700315
316 end = start + skb_shinfo(skb)->frags[i].size;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800317 if ((copy = end - offset) > 0) {
318 int err;
319 u8 *vaddr;
320 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
321 struct page *page = frag->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (copy > len)
324 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800325 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700326 err = memcpy_toiovec(to, vaddr + frag->page_offset +
327 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800328 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (err)
330 goto fault;
331 if (!(len -= copy))
332 return 0;
333 offset += copy;
334 }
David S. Miller1a028e52007-04-27 15:21:23 -0700335 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800337
David S. Miller5b1a0022009-06-09 00:18:15 -0700338 skb_walk_frags(skb, frag_iter) {
339 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800340
David S. Miller5b1a0022009-06-09 00:18:15 -0700341 WARN_ON(start > offset + len);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800342
David S. Miller5b1a0022009-06-09 00:18:15 -0700343 end = start + frag_iter->len;
344 if ((copy = end - offset) > 0) {
345 if (copy > len)
346 copy = len;
347 if (skb_copy_datagram_iovec(frag_iter,
348 offset - start,
349 to, copy))
350 goto fault;
351 if ((len -= copy) == 0)
352 return 0;
353 offset += copy;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800354 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700355 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800357 if (!len)
358 return 0;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360fault:
361 return -EFAULT;
362}
363
Rusty Russelldb543c12008-08-15 15:13:53 -0700364/**
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000365 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
366 * @skb: buffer to copy
367 * @offset: offset in the buffer to start copying from
368 * @to: io vector to copy to
369 * @to_offset: offset in the io vector to start copying to
370 * @len: amount of data to copy from buffer to iovec
371 *
372 * Returns 0 or -EFAULT.
373 * Note: the iovec is not modified during the copy.
374 */
375int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
376 const struct iovec *to, int to_offset,
377 int len)
378{
379 int start = skb_headlen(skb);
380 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700381 struct sk_buff *frag_iter;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000382
383 /* Copy header. */
384 if (copy > 0) {
385 if (copy > len)
386 copy = len;
387 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
388 goto fault;
389 if ((len -= copy) == 0)
390 return 0;
391 offset += copy;
392 to_offset += copy;
393 }
394
395 /* Copy paged appendix. Hmm... why does this look so complicated? */
396 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
397 int end;
398
399 WARN_ON(start > offset + len);
400
401 end = start + skb_shinfo(skb)->frags[i].size;
402 if ((copy = end - offset) > 0) {
403 int err;
404 u8 *vaddr;
405 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
406 struct page *page = frag->page;
407
408 if (copy > len)
409 copy = len;
410 vaddr = kmap(page);
411 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
412 offset - start, to_offset, copy);
413 kunmap(page);
414 if (err)
415 goto fault;
416 if (!(len -= copy))
417 return 0;
418 offset += copy;
419 to_offset += copy;
420 }
421 start = end;
422 }
423
David S. Miller5b1a0022009-06-09 00:18:15 -0700424 skb_walk_frags(skb, frag_iter) {
425 int end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000426
David S. Miller5b1a0022009-06-09 00:18:15 -0700427 WARN_ON(start > offset + len);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000428
David S. Miller5b1a0022009-06-09 00:18:15 -0700429 end = start + frag_iter->len;
430 if ((copy = end - offset) > 0) {
431 if (copy > len)
432 copy = len;
433 if (skb_copy_datagram_const_iovec(frag_iter,
434 offset - start,
435 to, to_offset,
436 copy))
437 goto fault;
438 if ((len -= copy) == 0)
439 return 0;
440 offset += copy;
441 to_offset += copy;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000442 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700443 start = end;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000444 }
445 if (!len)
446 return 0;
447
448fault:
449 return -EFAULT;
450}
451EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
452
453/**
Rusty Russelldb543c12008-08-15 15:13:53 -0700454 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
455 * @skb: buffer to copy
456 * @offset: offset in the buffer to start copying to
457 * @from: io vector to copy to
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000458 * @from_offset: offset in the io vector to start copying from
Rusty Russelldb543c12008-08-15 15:13:53 -0700459 * @len: amount of data to copy to buffer from iovec
460 *
461 * Returns 0 or -EFAULT.
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000462 * Note: the iovec is not modified during the copy.
Rusty Russelldb543c12008-08-15 15:13:53 -0700463 */
464int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000465 const struct iovec *from, int from_offset,
466 int len)
Rusty Russelldb543c12008-08-15 15:13:53 -0700467{
468 int start = skb_headlen(skb);
469 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700470 struct sk_buff *frag_iter;
Rusty Russelldb543c12008-08-15 15:13:53 -0700471
472 /* Copy header. */
473 if (copy > 0) {
474 if (copy > len)
475 copy = len;
Sridhar Samudralad2d27bf2009-06-05 09:35:40 +0000476 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
477 copy))
Rusty Russelldb543c12008-08-15 15:13:53 -0700478 goto fault;
479 if ((len -= copy) == 0)
480 return 0;
481 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000482 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700483 }
484
485 /* Copy paged appendix. Hmm... why does this look so complicated? */
486 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
487 int end;
488
489 WARN_ON(start > offset + len);
490
491 end = start + skb_shinfo(skb)->frags[i].size;
492 if ((copy = end - offset) > 0) {
493 int err;
494 u8 *vaddr;
495 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
496 struct page *page = frag->page;
497
498 if (copy > len)
499 copy = len;
500 vaddr = kmap(page);
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000501 err = memcpy_fromiovecend(vaddr + frag->page_offset +
502 offset - start,
503 from, from_offset, copy);
Rusty Russelldb543c12008-08-15 15:13:53 -0700504 kunmap(page);
505 if (err)
506 goto fault;
507
508 if (!(len -= copy))
509 return 0;
510 offset += copy;
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000511 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700512 }
513 start = end;
514 }
515
David S. Miller5b1a0022009-06-09 00:18:15 -0700516 skb_walk_frags(skb, frag_iter) {
517 int end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700518
David S. Miller5b1a0022009-06-09 00:18:15 -0700519 WARN_ON(start > offset + len);
Rusty Russelldb543c12008-08-15 15:13:53 -0700520
David S. Miller5b1a0022009-06-09 00:18:15 -0700521 end = start + frag_iter->len;
522 if ((copy = end - offset) > 0) {
523 if (copy > len)
524 copy = len;
525 if (skb_copy_datagram_from_iovec(frag_iter,
526 offset - start,
527 from,
528 from_offset,
529 copy))
530 goto fault;
531 if ((len -= copy) == 0)
532 return 0;
533 offset += copy;
534 from_offset += copy;
Rusty Russelldb543c12008-08-15 15:13:53 -0700535 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700536 start = end;
Rusty Russelldb543c12008-08-15 15:13:53 -0700537 }
538 if (!len)
539 return 0;
540
541fault:
542 return -EFAULT;
543}
544EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
547 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800548 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
David S. Miller1a028e52007-04-27 15:21:23 -0700550 int start = skb_headlen(skb);
David S. Miller1a028e52007-04-27 15:21:23 -0700551 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700552 struct sk_buff *frag_iter;
553 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Copy header. */
556 if (copy > 0) {
557 int err = 0;
558 if (copy > len)
559 copy = len;
560 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
561 *csump, &err);
562 if (err)
563 goto fault;
564 if ((len -= copy) == 0)
565 return 0;
566 offset += copy;
567 to += copy;
568 pos = copy;
569 }
570
571 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700572 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700574 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700575
576 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800578 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int err = 0;
580 u8 *vaddr;
581 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
582 struct page *page = frag->page;
583
584 if (copy > len)
585 copy = len;
586 vaddr = kmap(page);
587 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700588 frag->page_offset +
589 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 to, copy, 0, &err);
591 kunmap(page);
592 if (err)
593 goto fault;
594 *csump = csum_block_add(*csump, csum2, pos);
595 if (!(len -= copy))
596 return 0;
597 offset += copy;
598 to += copy;
599 pos += copy;
600 }
David S. Miller1a028e52007-04-27 15:21:23 -0700601 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
David S. Miller5b1a0022009-06-09 00:18:15 -0700604 skb_walk_frags(skb, frag_iter) {
605 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David S. Miller5b1a0022009-06-09 00:18:15 -0700607 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David S. Miller5b1a0022009-06-09 00:18:15 -0700609 end = start + frag_iter->len;
610 if ((copy = end - offset) > 0) {
611 __wsum csum2 = 0;
612 if (copy > len)
613 copy = len;
614 if (skb_copy_and_csum_datagram(frag_iter,
615 offset - start,
616 to, copy,
617 &csum2))
618 goto fault;
619 *csump = csum_block_add(*csump, csum2, pos);
620 if ((len -= copy) == 0)
621 return 0;
622 offset += copy;
623 to += copy;
624 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700626 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 if (!len)
629 return 0;
630
631fault:
632 return -EFAULT;
633}
634
Herbert Xu759e5d02007-03-25 20:10:56 -0700635__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800636{
Al Virod3bc23e2006-11-14 21:24:49 -0800637 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800638
Herbert Xu759e5d02007-03-25 20:10:56 -0700639 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Herbert Xufb286bb2005-11-10 13:01:24 -0800640 if (likely(!sum)) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700641 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800642 netdev_rx_csum_fault(skb->dev);
643 skb->ip_summed = CHECKSUM_UNNECESSARY;
644 }
645 return sum;
646}
Herbert Xu759e5d02007-03-25 20:10:56 -0700647EXPORT_SYMBOL(__skb_checksum_complete_head);
648
649__sum16 __skb_checksum_complete(struct sk_buff *skb)
650{
651 return __skb_checksum_complete_head(skb, skb->len);
652}
Herbert Xufb286bb2005-11-10 13:01:24 -0800653EXPORT_SYMBOL(__skb_checksum_complete);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700657 * @skb: skbuff
658 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700659 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * Caller _must_ check that skb will fit to this iovec.
662 *
663 * Returns: 0 - success.
664 * -EINVAL - checksum failure.
665 * -EFAULT - fault during copy. Beware, in this case iovec
666 * can be modified!
667 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800668int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int hlen, struct iovec *iov)
670{
Al Virod3bc23e2006-11-14 21:24:49 -0800671 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int chunk = skb->len - hlen;
673
Herbert Xuef8aef52007-09-06 14:06:35 +0100674 if (!chunk)
675 return 0;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Skip filled elements.
678 * Pretty silly, look at memcpy_toiovec, though 8)
679 */
680 while (!iov->iov_len)
681 iov++;
682
683 if (iov->iov_len < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800684 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto csum_error;
686 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
687 goto fault;
688 } else {
689 csum = csum_partial(skb->data, hlen, skb->csum);
690 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
691 chunk, &csum))
692 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800693 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700695 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800696 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 iov->iov_len -= chunk;
698 iov->iov_base += chunk;
699 }
700 return 0;
701csum_error:
702 return -EINVAL;
703fault:
704 return -EFAULT;
705}
706
707/**
708 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700709 * @file: file struct
710 * @sock: socket
711 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *
713 * Datagram poll: Again totally generic. This also handles
714 * sequenced packet sockets providing the socket receive queue
715 * is only ever holding data ready to receive.
716 *
717 * Note: when you _don't_ use this routine for this protocol,
718 * and you use a different write policy from sock_writeable()
719 * then please supply your own write_space callback.
720 */
721unsigned int datagram_poll(struct file *file, struct socket *sock,
722 poll_table *wait)
723{
724 struct sock *sk = sock->sk;
725 unsigned int mask;
726
Jiri Olsaa57de0b2009-07-08 12:09:13 +0000727 sock_poll_wait(file, sk->sk_sleep, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mask = 0;
729
730 /* exceptional events? */
731 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
732 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -0800733 if (sk->sk_shutdown & RCV_SHUTDOWN)
734 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (sk->sk_shutdown == SHUTDOWN_MASK)
736 mask |= POLLHUP;
737
738 /* readable? */
739 if (!skb_queue_empty(&sk->sk_receive_queue) ||
740 (sk->sk_shutdown & RCV_SHUTDOWN))
741 mask |= POLLIN | POLLRDNORM;
742
743 /* Connection-based need to check for termination and startup */
744 if (connection_based(sk)) {
745 if (sk->sk_state == TCP_CLOSE)
746 mask |= POLLHUP;
747 /* connection hasn't started yet? */
748 if (sk->sk_state == TCP_SYN_SENT)
749 return mask;
750 }
751
752 /* writable? */
753 if (sock_writeable(sk))
754 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
755 else
756 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
757
758 return mask;
759}
760
761EXPORT_SYMBOL(datagram_poll);
762EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
763EXPORT_SYMBOL(skb_copy_datagram_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764EXPORT_SYMBOL(skb_recv_datagram);