blob: 4dbb05cd572b9be22b1a17aee2dd4bc49be49f06 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * Is a socket 'connection oriented' ?
61 */
62static inline int connection_based(struct sock *sk)
63{
64 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
65}
66
67/*
68 * Wait for a packet..
69 */
70static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
71{
72 int error;
73 DEFINE_WAIT(wait);
74
75 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
76
77 /* Socket errors? */
78 error = sock_error(sk);
79 if (error)
80 goto out_err;
81
82 if (!skb_queue_empty(&sk->sk_receive_queue))
83 goto out;
84
85 /* Socket shut down? */
86 if (sk->sk_shutdown & RCV_SHUTDOWN)
87 goto out_noerr;
88
89 /* Sequenced packets can come disconnected.
90 * If so we report the problem
91 */
92 error = -ENOTCONN;
93 if (connection_based(sk) &&
94 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
95 goto out_err;
96
97 /* handle signals */
98 if (signal_pending(current))
99 goto interrupted;
100
101 error = 0;
102 *timeo_p = schedule_timeout(*timeo_p);
103out:
104 finish_wait(sk->sk_sleep, &wait);
105 return error;
106interrupted:
107 error = sock_intr_errno(*timeo_p);
108out_err:
109 *err = error;
110 goto out;
111out_noerr:
112 *err = 0;
113 error = 1;
114 goto out;
115}
116
117/**
Herbert Xua59322b2007-12-05 01:53:40 -0800118 * __skb_recv_datagram - Receive a datagram skbuff
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700119 * @sk: socket
120 * @flags: MSG_ flags
Herbert Xua59322b2007-12-05 01:53:40 -0800121 * @peeked: returns non-zero if this packet has been seen before
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700122 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
124 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
125 * and possible races. This replaces identical code in packet, raw and
126 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
127 * the long standing peek and read race for datagram sockets. If you
128 * alter this routine remember it must be re-entrant.
129 *
130 * This function will lock the socket if a skb is returned, so the caller
131 * needs to unlock the socket in that case (usually by calling
132 * skb_free_datagram)
133 *
134 * * It does not lock socket since today. This function is
135 * * free of race conditions. This measure should/can improve
136 * * significantly datagram socket latencies at high loads,
137 * * when data copying to user space takes lots of time.
138 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
139 * * 8) Great win.)
140 * * --ANK (980729)
141 *
142 * The order of the tests when we find no data waiting are specified
143 * quite explicitly by POSIX 1003.1g, don't change them without having
144 * the standard around please.
145 */
Herbert Xua59322b2007-12-05 01:53:40 -0800146struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
147 int *peeked, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct sk_buff *skb;
150 long timeo;
151 /*
152 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
153 */
154 int error = sock_error(sk);
155
156 if (error)
157 goto no_packet;
158
Herbert Xua59322b2007-12-05 01:53:40 -0800159 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 do {
162 /* Again only user level code calls this function, so nothing
163 * interrupt level will suddenly eat the receive_queue.
164 *
165 * Look at current nfs client by the way...
166 * However, this function was corrent in any case. 8)
167 */
Herbert Xua59322b2007-12-05 01:53:40 -0800168 unsigned long cpu_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Herbert Xua59322b2007-12-05 01:53:40 -0800170 spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
171 skb = skb_peek(&sk->sk_receive_queue);
172 if (skb) {
173 *peeked = skb->peeked;
174 if (flags & MSG_PEEK) {
175 skb->peeked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800177 } else
178 __skb_unlink(skb, &sk->sk_receive_queue);
179 }
180 spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (skb)
183 return skb;
184
185 /* User doesn't want to wait */
186 error = -EAGAIN;
187 if (!timeo)
188 goto no_packet;
189
190 } while (!wait_for_packet(sk, err, &timeo));
191
192 return NULL;
193
194no_packet:
195 *err = error;
196 return NULL;
197}
Herbert Xua59322b2007-12-05 01:53:40 -0800198EXPORT_SYMBOL(__skb_recv_datagram);
199
200struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
201 int noblock, int *err)
202{
203 int peeked;
204
205 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
206 &peeked, err);
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
210{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000211 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800212 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215/**
Herbert Xu3305b802005-12-13 23:16:37 -0800216 * skb_kill_datagram - Free a datagram skbuff forcibly
217 * @sk: socket
218 * @skb: datagram skbuff
219 * @flags: MSG_ flags
220 *
221 * This function frees a datagram skbuff that was received by
222 * skb_recv_datagram. The flags argument must match the one
223 * used for skb_recv_datagram.
224 *
225 * If the MSG_PEEK flag is set, and the packet is still on the
226 * receive queue of the socket, it will be taken off the queue
227 * before it is freed.
228 *
229 * This function currently only disables BH when acquiring the
230 * sk_receive_queue lock. Therefore it must not be used in a
231 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800232 *
233 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800234 */
235
Herbert Xu27ab2562007-12-05 01:51:58 -0800236int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800237{
Herbert Xu27ab2562007-12-05 01:51:58 -0800238 int err = 0;
239
Herbert Xu3305b802005-12-13 23:16:37 -0800240 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800241 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800242 spin_lock_bh(&sk->sk_receive_queue.lock);
243 if (skb == skb_peek(&sk->sk_receive_queue)) {
244 __skb_unlink(skb, &sk->sk_receive_queue);
245 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800246 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800247 }
248 spin_unlock_bh(&sk->sk_receive_queue.lock);
249 }
250
Eric Dumazet270acef2008-11-05 01:38:06 -0800251 skb_free_datagram(sk, skb);
Herbert Xu27ab2562007-12-05 01:51:58 -0800252 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800253}
254
255EXPORT_SYMBOL(skb_kill_datagram);
256
257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700259 * @skb: buffer to copy
260 * @offset: offset in the buffer to start copying from
Martin Waitz67be2dd2005-05-01 08:59:26 -0700261 * @to: io vector to copy to
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700262 * @len: amount of data to copy from buffer to iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
264 * Note: the iovec is modified during the copy.
265 */
266int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
267 struct iovec *to, int len)
268{
David S. Miller1a028e52007-04-27 15:21:23 -0700269 int start = skb_headlen(skb);
270 int i, copy = start - offset;
Herbert Xuc75d7212005-11-02 18:55:00 +1100271
David S. Millerb4d9eda2006-02-13 16:06:10 -0800272 /* Copy header. */
273 if (copy > 0) {
274 if (copy > len)
275 copy = len;
276 if (memcpy_toiovec(to, skb->data + offset, copy))
277 goto fault;
278 if ((len -= copy) == 0)
279 return 0;
280 offset += copy;
281 }
Herbert Xuc75d7212005-11-02 18:55:00 +1100282
David S. Millerb4d9eda2006-02-13 16:06:10 -0800283 /* Copy paged appendix. Hmm... why does this look so complicated? */
284 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700285 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700287 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700288
289 end = start + skb_shinfo(skb)->frags[i].size;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800290 if ((copy = end - offset) > 0) {
291 int err;
292 u8 *vaddr;
293 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
294 struct page *page = frag->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (copy > len)
297 copy = len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800298 vaddr = kmap(page);
David S. Miller1a028e52007-04-27 15:21:23 -0700299 err = memcpy_toiovec(to, vaddr + frag->page_offset +
300 offset - start, copy);
David S. Millerb4d9eda2006-02-13 16:06:10 -0800301 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (err)
303 goto fault;
304 if (!(len -= copy))
305 return 0;
306 offset += copy;
307 }
David S. Miller1a028e52007-04-27 15:21:23 -0700308 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800310
311 if (skb_shinfo(skb)->frag_list) {
312 struct sk_buff *list = skb_shinfo(skb)->frag_list;
313
314 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -0700315 int end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800316
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700317 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700318
319 end = start + list->len;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800320 if ((copy = end - offset) > 0) {
321 if (copy > len)
322 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -0700323 if (skb_copy_datagram_iovec(list,
324 offset - start,
325 to, copy))
David S. Millerb4d9eda2006-02-13 16:06:10 -0800326 goto fault;
327 if ((len -= copy) == 0)
328 return 0;
329 offset += copy;
330 }
David S. Miller1a028e52007-04-27 15:21:23 -0700331 start = end;
David S. Millerb4d9eda2006-02-13 16:06:10 -0800332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
David S. Millerb4d9eda2006-02-13 16:06:10 -0800334 if (!len)
335 return 0;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337fault:
338 return -EFAULT;
339}
340
Rusty Russelldb543c12008-08-15 15:13:53 -0700341/**
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000342 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
343 * @skb: buffer to copy
344 * @offset: offset in the buffer to start copying from
345 * @to: io vector to copy to
346 * @to_offset: offset in the io vector to start copying to
347 * @len: amount of data to copy from buffer to iovec
348 *
349 * Returns 0 or -EFAULT.
350 * Note: the iovec is not modified during the copy.
351 */
352int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
353 const struct iovec *to, int to_offset,
354 int len)
355{
356 int start = skb_headlen(skb);
357 int i, copy = start - offset;
358
359 /* Copy header. */
360 if (copy > 0) {
361 if (copy > len)
362 copy = len;
363 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
364 goto fault;
365 if ((len -= copy) == 0)
366 return 0;
367 offset += copy;
368 to_offset += copy;
369 }
370
371 /* Copy paged appendix. Hmm... why does this look so complicated? */
372 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
373 int end;
374
375 WARN_ON(start > offset + len);
376
377 end = start + skb_shinfo(skb)->frags[i].size;
378 if ((copy = end - offset) > 0) {
379 int err;
380 u8 *vaddr;
381 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
382 struct page *page = frag->page;
383
384 if (copy > len)
385 copy = len;
386 vaddr = kmap(page);
387 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
388 offset - start, to_offset, copy);
389 kunmap(page);
390 if (err)
391 goto fault;
392 if (!(len -= copy))
393 return 0;
394 offset += copy;
395 to_offset += copy;
396 }
397 start = end;
398 }
399
400 if (skb_shinfo(skb)->frag_list) {
401 struct sk_buff *list = skb_shinfo(skb)->frag_list;
402
403 for (; list; list = list->next) {
404 int end;
405
406 WARN_ON(start > offset + len);
407
408 end = start + list->len;
409 if ((copy = end - offset) > 0) {
410 if (copy > len)
411 copy = len;
412 if (skb_copy_datagram_const_iovec(list,
413 offset - start,
414 to, to_offset,
415 copy))
416 goto fault;
417 if ((len -= copy) == 0)
418 return 0;
419 offset += copy;
420 to_offset += copy;
421 }
422 start = end;
423 }
424 }
425 if (!len)
426 return 0;
427
428fault:
429 return -EFAULT;
430}
431EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
432
433/**
Rusty Russelldb543c12008-08-15 15:13:53 -0700434 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
435 * @skb: buffer to copy
436 * @offset: offset in the buffer to start copying to
437 * @from: io vector to copy to
438 * @len: amount of data to copy to buffer from iovec
439 *
440 * Returns 0 or -EFAULT.
441 * Note: the iovec is modified during the copy.
442 */
443int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
444 struct iovec *from, int len)
445{
446 int start = skb_headlen(skb);
447 int i, copy = start - offset;
448
449 /* Copy header. */
450 if (copy > 0) {
451 if (copy > len)
452 copy = len;
453 if (memcpy_fromiovec(skb->data + offset, from, copy))
454 goto fault;
455 if ((len -= copy) == 0)
456 return 0;
457 offset += copy;
458 }
459
460 /* Copy paged appendix. Hmm... why does this look so complicated? */
461 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
462 int end;
463
464 WARN_ON(start > offset + len);
465
466 end = start + skb_shinfo(skb)->frags[i].size;
467 if ((copy = end - offset) > 0) {
468 int err;
469 u8 *vaddr;
470 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
471 struct page *page = frag->page;
472
473 if (copy > len)
474 copy = len;
475 vaddr = kmap(page);
476 err = memcpy_fromiovec(vaddr + frag->page_offset +
477 offset - start, from, copy);
478 kunmap(page);
479 if (err)
480 goto fault;
481
482 if (!(len -= copy))
483 return 0;
484 offset += copy;
485 }
486 start = end;
487 }
488
489 if (skb_shinfo(skb)->frag_list) {
490 struct sk_buff *list = skb_shinfo(skb)->frag_list;
491
492 for (; list; list = list->next) {
493 int end;
494
495 WARN_ON(start > offset + len);
496
497 end = start + list->len;
498 if ((copy = end - offset) > 0) {
499 if (copy > len)
500 copy = len;
501 if (skb_copy_datagram_from_iovec(list,
502 offset - start,
503 from, copy))
504 goto fault;
505 if ((len -= copy) == 0)
506 return 0;
507 offset += copy;
508 }
509 start = end;
510 }
511 }
512 if (!len)
513 return 0;
514
515fault:
516 return -EFAULT;
517}
518EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
521 u8 __user *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800522 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
David S. Miller1a028e52007-04-27 15:21:23 -0700524 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int pos = 0;
David S. Miller1a028e52007-04-27 15:21:23 -0700526 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* Copy header. */
529 if (copy > 0) {
530 int err = 0;
531 if (copy > len)
532 copy = len;
533 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
534 *csump, &err);
535 if (err)
536 goto fault;
537 if ((len -= copy) == 0)
538 return 0;
539 offset += copy;
540 to += copy;
541 pos = copy;
542 }
543
544 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700545 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700547 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700548
549 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800551 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int err = 0;
553 u8 *vaddr;
554 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
555 struct page *page = frag->page;
556
557 if (copy > len)
558 copy = len;
559 vaddr = kmap(page);
560 csum2 = csum_and_copy_to_user(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -0700561 frag->page_offset +
562 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 to, copy, 0, &err);
564 kunmap(page);
565 if (err)
566 goto fault;
567 *csump = csum_block_add(*csump, csum2, pos);
568 if (!(len -= copy))
569 return 0;
570 offset += copy;
571 to += copy;
572 pos += copy;
573 }
David S. Miller1a028e52007-04-27 15:21:23 -0700574 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 if (skb_shinfo(skb)->frag_list) {
578 struct sk_buff *list = skb_shinfo(skb)->frag_list;
579
580 for (; list; list=list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -0700581 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700583 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700584
585 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -0800587 __wsum csum2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (copy > len)
589 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -0700590 if (skb_copy_and_csum_datagram(list,
591 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 to, copy,
593 &csum2))
594 goto fault;
595 *csump = csum_block_add(*csump, csum2, pos);
596 if ((len -= copy) == 0)
597 return 0;
598 offset += copy;
599 to += copy;
600 pos += copy;
601 }
David S. Miller1a028e52007-04-27 15:21:23 -0700602 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 }
605 if (!len)
606 return 0;
607
608fault:
609 return -EFAULT;
610}
611
Herbert Xu759e5d02007-03-25 20:10:56 -0700612__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800613{
Al Virod3bc23e2006-11-14 21:24:49 -0800614 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800615
Herbert Xu759e5d02007-03-25 20:10:56 -0700616 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Herbert Xufb286bb2005-11-10 13:01:24 -0800617 if (likely(!sum)) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700618 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800619 netdev_rx_csum_fault(skb->dev);
620 skb->ip_summed = CHECKSUM_UNNECESSARY;
621 }
622 return sum;
623}
Herbert Xu759e5d02007-03-25 20:10:56 -0700624EXPORT_SYMBOL(__skb_checksum_complete_head);
625
626__sum16 __skb_checksum_complete(struct sk_buff *skb)
627{
628 return __skb_checksum_complete_head(skb, skb->len);
629}
Herbert Xufb286bb2005-11-10 13:01:24 -0800630EXPORT_SYMBOL(__skb_checksum_complete);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/**
633 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700634 * @skb: skbuff
635 * @hlen: hardware length
Martin Waitz67be2dd2005-05-01 08:59:26 -0700636 * @iov: io vector
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * Caller _must_ check that skb will fit to this iovec.
639 *
640 * Returns: 0 - success.
641 * -EINVAL - checksum failure.
642 * -EFAULT - fault during copy. Beware, in this case iovec
643 * can be modified!
644 */
Herbert Xufb286bb2005-11-10 13:01:24 -0800645int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int hlen, struct iovec *iov)
647{
Al Virod3bc23e2006-11-14 21:24:49 -0800648 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int chunk = skb->len - hlen;
650
Herbert Xuef8aef52007-09-06 14:06:35 +0100651 if (!chunk)
652 return 0;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Skip filled elements.
655 * Pretty silly, look at memcpy_toiovec, though 8)
656 */
657 while (!iov->iov_len)
658 iov++;
659
660 if (iov->iov_len < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800661 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto csum_error;
663 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
664 goto fault;
665 } else {
666 csum = csum_partial(skb->data, hlen, skb->csum);
667 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
668 chunk, &csum))
669 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800670 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700672 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800673 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 iov->iov_len -= chunk;
675 iov->iov_base += chunk;
676 }
677 return 0;
678csum_error:
679 return -EINVAL;
680fault:
681 return -EFAULT;
682}
683
684/**
685 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700686 * @file: file struct
687 * @sock: socket
688 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
690 * Datagram poll: Again totally generic. This also handles
691 * sequenced packet sockets providing the socket receive queue
692 * is only ever holding data ready to receive.
693 *
694 * Note: when you _don't_ use this routine for this protocol,
695 * and you use a different write policy from sock_writeable()
696 * then please supply your own write_space callback.
697 */
698unsigned int datagram_poll(struct file *file, struct socket *sock,
699 poll_table *wait)
700{
701 struct sock *sk = sock->sk;
702 unsigned int mask;
703
704 poll_wait(file, sk->sk_sleep, wait);
705 mask = 0;
706
707 /* exceptional events? */
708 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
709 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -0800710 if (sk->sk_shutdown & RCV_SHUTDOWN)
711 mask |= POLLRDHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (sk->sk_shutdown == SHUTDOWN_MASK)
713 mask |= POLLHUP;
714
715 /* readable? */
716 if (!skb_queue_empty(&sk->sk_receive_queue) ||
717 (sk->sk_shutdown & RCV_SHUTDOWN))
718 mask |= POLLIN | POLLRDNORM;
719
720 /* Connection-based need to check for termination and startup */
721 if (connection_based(sk)) {
722 if (sk->sk_state == TCP_CLOSE)
723 mask |= POLLHUP;
724 /* connection hasn't started yet? */
725 if (sk->sk_state == TCP_SYN_SENT)
726 return mask;
727 }
728
729 /* writable? */
730 if (sock_writeable(sk))
731 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
732 else
733 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
734
735 return mask;
736}
737
738EXPORT_SYMBOL(datagram_poll);
739EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
740EXPORT_SYMBOL(skb_copy_datagram_iovec);
741EXPORT_SYMBOL(skb_free_datagram);
742EXPORT_SYMBOL(skb_recv_datagram);