]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/core/datagram.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[mirror_ubuntu-artful-kernel.git] / net / core / datagram.c
1 /*
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 *
12 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
13 * 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 <linux/uaccess.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/errno.h>
43 #include <linux/sched.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/poll.h>
48 #include <linux/highmem.h>
49 #include <linux/spinlock.h>
50 #include <linux/slab.h>
51 #include <linux/pagemap.h>
52 #include <linux/uio.h>
53
54 #include <net/protocol.h>
55 #include <linux/skbuff.h>
56
57 #include <net/checksum.h>
58 #include <net/sock.h>
59 #include <net/tcp_states.h>
60 #include <trace/events/skb.h>
61 #include <net/busy_poll.h>
62
63 /*
64 * Is a socket 'connection oriented' ?
65 */
66 static inline int connection_based(struct sock *sk)
67 {
68 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69 }
70
71 static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
72 void *key)
73 {
74 unsigned long bits = (unsigned long)key;
75
76 /*
77 * Avoid a wakeup if event not interesting for us
78 */
79 if (bits && !(bits & (POLLIN | POLLERR)))
80 return 0;
81 return autoremove_wake_function(wait, mode, sync, key);
82 }
83 /*
84 * Wait for the last received packet to be different from skb
85 */
86 int __skb_wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
87 const struct sk_buff *skb)
88 {
89 int error;
90 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
91
92 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
93
94 /* Socket errors? */
95 error = sock_error(sk);
96 if (error)
97 goto out_err;
98
99 if (sk->sk_receive_queue.prev != skb)
100 goto out;
101
102 /* Socket shut down? */
103 if (sk->sk_shutdown & RCV_SHUTDOWN)
104 goto out_noerr;
105
106 /* Sequenced packets can come disconnected.
107 * If so we report the problem
108 */
109 error = -ENOTCONN;
110 if (connection_based(sk) &&
111 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
112 goto out_err;
113
114 /* handle signals */
115 if (signal_pending(current))
116 goto interrupted;
117
118 error = 0;
119 *timeo_p = schedule_timeout(*timeo_p);
120 out:
121 finish_wait(sk_sleep(sk), &wait);
122 return error;
123 interrupted:
124 error = sock_intr_errno(*timeo_p);
125 out_err:
126 *err = error;
127 goto out;
128 out_noerr:
129 *err = 0;
130 error = 1;
131 goto out;
132 }
133 EXPORT_SYMBOL(__skb_wait_for_more_packets);
134
135 static struct sk_buff *skb_set_peeked(struct sk_buff *skb)
136 {
137 struct sk_buff *nskb;
138
139 if (skb->peeked)
140 return skb;
141
142 /* We have to unshare an skb before modifying it. */
143 if (!skb_shared(skb))
144 goto done;
145
146 nskb = skb_clone(skb, GFP_ATOMIC);
147 if (!nskb)
148 return ERR_PTR(-ENOMEM);
149
150 skb->prev->next = nskb;
151 skb->next->prev = nskb;
152 nskb->prev = skb->prev;
153 nskb->next = skb->next;
154
155 consume_skb(skb);
156 skb = nskb;
157
158 done:
159 skb->peeked = 1;
160
161 return skb;
162 }
163
164 /**
165 * __skb_try_recv_datagram - Receive a datagram skbuff
166 * @sk: socket
167 * @flags: MSG_ flags
168 * @destructor: invoked under the receive lock on successful dequeue
169 * @peeked: returns non-zero if this packet has been seen before
170 * @off: an offset in bytes to peek skb from. Returns an offset
171 * within an skb where data actually starts
172 * @err: error code returned
173 * @last: set to last peeked message to inform the wait function
174 * what to look for when peeking
175 *
176 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
177 * and possible races. This replaces identical code in packet, raw and
178 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
179 * the long standing peek and read race for datagram sockets. If you
180 * alter this routine remember it must be re-entrant.
181 *
182 * This function will lock the socket if a skb is returned, so
183 * the caller needs to unlock the socket in that case (usually by
184 * calling skb_free_datagram). Returns NULL with *err set to
185 * -EAGAIN if no data was available or to some other value if an
186 * error was detected.
187 *
188 * * It does not lock socket since today. This function is
189 * * free of race conditions. This measure should/can improve
190 * * significantly datagram socket latencies at high loads,
191 * * when data copying to user space takes lots of time.
192 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
193 * * 8) Great win.)
194 * * --ANK (980729)
195 *
196 * The order of the tests when we find no data waiting are specified
197 * quite explicitly by POSIX 1003.1g, don't change them without having
198 * the standard around please.
199 */
200 struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags,
201 void (*destructor)(struct sock *sk,
202 struct sk_buff *skb),
203 int *peeked, int *off, int *err,
204 struct sk_buff **last)
205 {
206 struct sk_buff_head *queue = &sk->sk_receive_queue;
207 struct sk_buff *skb;
208 unsigned long cpu_flags;
209 /*
210 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
211 */
212 int error = sock_error(sk);
213
214 if (error)
215 goto no_packet;
216
217 *peeked = 0;
218 do {
219 /* Again only user level code calls this function, so nothing
220 * interrupt level will suddenly eat the receive_queue.
221 *
222 * Look at current nfs client by the way...
223 * However, this function was correct in any case. 8)
224 */
225 int _off = *off;
226
227 *last = (struct sk_buff *)queue;
228 spin_lock_irqsave(&queue->lock, cpu_flags);
229 skb_queue_walk(queue, skb) {
230 *last = skb;
231 if (flags & MSG_PEEK) {
232 if (_off >= skb->len && (skb->len || _off ||
233 skb->peeked)) {
234 _off -= skb->len;
235 continue;
236 }
237 if (!skb->len) {
238 skb = skb_set_peeked(skb);
239 if (IS_ERR(skb)) {
240 error = PTR_ERR(skb);
241 spin_unlock_irqrestore(&queue->lock,
242 cpu_flags);
243 goto no_packet;
244 }
245 }
246 *peeked = 1;
247 atomic_inc(&skb->users);
248 } else {
249 __skb_unlink(skb, queue);
250 if (destructor)
251 destructor(sk, skb);
252 }
253 spin_unlock_irqrestore(&queue->lock, cpu_flags);
254 *off = _off;
255 return skb;
256 }
257
258 spin_unlock_irqrestore(&queue->lock, cpu_flags);
259
260 if (!sk_can_busy_loop(sk))
261 break;
262
263 sk_busy_loop(sk, flags & MSG_DONTWAIT);
264 } while (!skb_queue_empty(&sk->sk_receive_queue));
265
266 error = -EAGAIN;
267
268 no_packet:
269 *err = error;
270 return NULL;
271 }
272 EXPORT_SYMBOL(__skb_try_recv_datagram);
273
274 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
275 void (*destructor)(struct sock *sk,
276 struct sk_buff *skb),
277 int *peeked, int *off, int *err)
278 {
279 struct sk_buff *skb, *last;
280 long timeo;
281
282 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
283
284 do {
285 skb = __skb_try_recv_datagram(sk, flags, destructor, peeked,
286 off, err, &last);
287 if (skb)
288 return skb;
289
290 if (*err != -EAGAIN)
291 break;
292 } while (timeo &&
293 !__skb_wait_for_more_packets(sk, err, &timeo, last));
294
295 return NULL;
296 }
297 EXPORT_SYMBOL(__skb_recv_datagram);
298
299 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
300 int noblock, int *err)
301 {
302 int peeked, off = 0;
303
304 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
305 NULL, &peeked, &off, err);
306 }
307 EXPORT_SYMBOL(skb_recv_datagram);
308
309 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
310 {
311 consume_skb(skb);
312 sk_mem_reclaim_partial(sk);
313 }
314 EXPORT_SYMBOL(skb_free_datagram);
315
316 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
317 {
318 bool slow;
319
320 if (likely(atomic_read(&skb->users) == 1))
321 smp_rmb();
322 else if (likely(!atomic_dec_and_test(&skb->users))) {
323 sk_peek_offset_bwd(sk, len);
324 return;
325 }
326
327 slow = lock_sock_fast(sk);
328 sk_peek_offset_bwd(sk, len);
329 skb_orphan(skb);
330 sk_mem_reclaim_partial(sk);
331 unlock_sock_fast(sk, slow);
332
333 /* skb is now orphaned, can be freed outside of locked section */
334 __kfree_skb(skb);
335 }
336 EXPORT_SYMBOL(__skb_free_datagram_locked);
337
338 int __sk_queue_drop_skb(struct sock *sk, struct sk_buff *skb,
339 unsigned int flags,
340 void (*destructor)(struct sock *sk,
341 struct sk_buff *skb))
342 {
343 int err = 0;
344
345 if (flags & MSG_PEEK) {
346 err = -ENOENT;
347 spin_lock_bh(&sk->sk_receive_queue.lock);
348 if (skb == skb_peek(&sk->sk_receive_queue)) {
349 __skb_unlink(skb, &sk->sk_receive_queue);
350 atomic_dec(&skb->users);
351 if (destructor)
352 destructor(sk, skb);
353 err = 0;
354 }
355 spin_unlock_bh(&sk->sk_receive_queue.lock);
356 }
357
358 atomic_inc(&sk->sk_drops);
359 return err;
360 }
361 EXPORT_SYMBOL(__sk_queue_drop_skb);
362
363 /**
364 * skb_kill_datagram - Free a datagram skbuff forcibly
365 * @sk: socket
366 * @skb: datagram skbuff
367 * @flags: MSG_ flags
368 *
369 * This function frees a datagram skbuff that was received by
370 * skb_recv_datagram. The flags argument must match the one
371 * used for skb_recv_datagram.
372 *
373 * If the MSG_PEEK flag is set, and the packet is still on the
374 * receive queue of the socket, it will be taken off the queue
375 * before it is freed.
376 *
377 * This function currently only disables BH when acquiring the
378 * sk_receive_queue lock. Therefore it must not be used in a
379 * context where that lock is acquired in an IRQ context.
380 *
381 * It returns 0 if the packet was removed by us.
382 */
383
384 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
385 {
386 int err = __sk_queue_drop_skb(sk, skb, flags, NULL);
387
388 kfree_skb(skb);
389 sk_mem_reclaim_partial(sk);
390 return err;
391 }
392 EXPORT_SYMBOL(skb_kill_datagram);
393
394 /**
395 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
396 * @skb: buffer to copy
397 * @offset: offset in the buffer to start copying from
398 * @to: iovec iterator to copy to
399 * @len: amount of data to copy from buffer to iovec
400 */
401 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
402 struct iov_iter *to, int len)
403 {
404 int start = skb_headlen(skb);
405 int i, copy = start - offset, start_off = offset, n;
406 struct sk_buff *frag_iter;
407
408 trace_skb_copy_datagram_iovec(skb, len);
409
410 /* Copy header. */
411 if (copy > 0) {
412 if (copy > len)
413 copy = len;
414 n = copy_to_iter(skb->data + offset, copy, to);
415 offset += n;
416 if (n != copy)
417 goto short_copy;
418 if ((len -= copy) == 0)
419 return 0;
420 }
421
422 /* Copy paged appendix. Hmm... why does this look so complicated? */
423 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
424 int end;
425 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
426
427 WARN_ON(start > offset + len);
428
429 end = start + skb_frag_size(frag);
430 if ((copy = end - offset) > 0) {
431 if (copy > len)
432 copy = len;
433 n = copy_page_to_iter(skb_frag_page(frag),
434 frag->page_offset + offset -
435 start, copy, to);
436 offset += n;
437 if (n != copy)
438 goto short_copy;
439 if (!(len -= copy))
440 return 0;
441 }
442 start = end;
443 }
444
445 skb_walk_frags(skb, frag_iter) {
446 int end;
447
448 WARN_ON(start > offset + len);
449
450 end = start + frag_iter->len;
451 if ((copy = end - offset) > 0) {
452 if (copy > len)
453 copy = len;
454 if (skb_copy_datagram_iter(frag_iter, offset - start,
455 to, copy))
456 goto fault;
457 if ((len -= copy) == 0)
458 return 0;
459 offset += copy;
460 }
461 start = end;
462 }
463 if (!len)
464 return 0;
465
466 /* This is not really a user copy fault, but rather someone
467 * gave us a bogus length on the skb. We should probably
468 * print a warning here as it may indicate a kernel bug.
469 */
470
471 fault:
472 iov_iter_revert(to, offset - start_off);
473 return -EFAULT;
474
475 short_copy:
476 if (iov_iter_count(to))
477 goto fault;
478
479 return 0;
480 }
481 EXPORT_SYMBOL(skb_copy_datagram_iter);
482
483 /**
484 * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
485 * @skb: buffer to copy
486 * @offset: offset in the buffer to start copying to
487 * @from: the copy source
488 * @len: amount of data to copy to buffer from iovec
489 *
490 * Returns 0 or -EFAULT.
491 */
492 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
493 struct iov_iter *from,
494 int len)
495 {
496 int start = skb_headlen(skb);
497 int i, copy = start - offset;
498 struct sk_buff *frag_iter;
499
500 /* Copy header. */
501 if (copy > 0) {
502 if (copy > len)
503 copy = len;
504 if (copy_from_iter(skb->data + offset, copy, from) != copy)
505 goto fault;
506 if ((len -= copy) == 0)
507 return 0;
508 offset += copy;
509 }
510
511 /* Copy paged appendix. Hmm... why does this look so complicated? */
512 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
513 int end;
514 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
515
516 WARN_ON(start > offset + len);
517
518 end = start + skb_frag_size(frag);
519 if ((copy = end - offset) > 0) {
520 size_t copied;
521
522 if (copy > len)
523 copy = len;
524 copied = copy_page_from_iter(skb_frag_page(frag),
525 frag->page_offset + offset - start,
526 copy, from);
527 if (copied != copy)
528 goto fault;
529
530 if (!(len -= copy))
531 return 0;
532 offset += copy;
533 }
534 start = end;
535 }
536
537 skb_walk_frags(skb, frag_iter) {
538 int end;
539
540 WARN_ON(start > offset + len);
541
542 end = start + frag_iter->len;
543 if ((copy = end - offset) > 0) {
544 if (copy > len)
545 copy = len;
546 if (skb_copy_datagram_from_iter(frag_iter,
547 offset - start,
548 from, copy))
549 goto fault;
550 if ((len -= copy) == 0)
551 return 0;
552 offset += copy;
553 }
554 start = end;
555 }
556 if (!len)
557 return 0;
558
559 fault:
560 return -EFAULT;
561 }
562 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
563
564 /**
565 * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
566 * @skb: buffer to copy
567 * @from: the source to copy from
568 *
569 * The function will first copy up to headlen, and then pin the userspace
570 * pages and build frags through them.
571 *
572 * Returns 0, -EFAULT or -EMSGSIZE.
573 */
574 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
575 {
576 int len = iov_iter_count(from);
577 int copy = min_t(int, skb_headlen(skb), len);
578 int frag = 0;
579
580 /* copy up to skb headlen */
581 if (skb_copy_datagram_from_iter(skb, 0, from, copy))
582 return -EFAULT;
583
584 while (iov_iter_count(from)) {
585 struct page *pages[MAX_SKB_FRAGS];
586 size_t start;
587 ssize_t copied;
588 unsigned long truesize;
589 int n = 0;
590
591 if (frag == MAX_SKB_FRAGS)
592 return -EMSGSIZE;
593
594 copied = iov_iter_get_pages(from, pages, ~0U,
595 MAX_SKB_FRAGS - frag, &start);
596 if (copied < 0)
597 return -EFAULT;
598
599 iov_iter_advance(from, copied);
600
601 truesize = PAGE_ALIGN(copied + start);
602 skb->data_len += copied;
603 skb->len += copied;
604 skb->truesize += truesize;
605 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
606 while (copied) {
607 int size = min_t(int, copied, PAGE_SIZE - start);
608 skb_fill_page_desc(skb, frag++, pages[n], start, size);
609 start = 0;
610 copied -= size;
611 n++;
612 }
613 }
614 return 0;
615 }
616 EXPORT_SYMBOL(zerocopy_sg_from_iter);
617
618 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
619 struct iov_iter *to, int len,
620 __wsum *csump)
621 {
622 int start = skb_headlen(skb);
623 int i, copy = start - offset, start_off = offset;
624 struct sk_buff *frag_iter;
625 int pos = 0;
626 int n;
627
628 /* Copy header. */
629 if (copy > 0) {
630 if (copy > len)
631 copy = len;
632 n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
633 offset += n;
634 if (n != copy)
635 goto fault;
636 if ((len -= copy) == 0)
637 return 0;
638 pos = copy;
639 }
640
641 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
642 int end;
643 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
644
645 WARN_ON(start > offset + len);
646
647 end = start + skb_frag_size(frag);
648 if ((copy = end - offset) > 0) {
649 __wsum csum2 = 0;
650 struct page *page = skb_frag_page(frag);
651 u8 *vaddr = kmap(page);
652
653 if (copy > len)
654 copy = len;
655 n = csum_and_copy_to_iter(vaddr + frag->page_offset +
656 offset - start, copy,
657 &csum2, to);
658 kunmap(page);
659 offset += n;
660 if (n != copy)
661 goto fault;
662 *csump = csum_block_add(*csump, csum2, pos);
663 if (!(len -= copy))
664 return 0;
665 pos += copy;
666 }
667 start = end;
668 }
669
670 skb_walk_frags(skb, frag_iter) {
671 int end;
672
673 WARN_ON(start > offset + len);
674
675 end = start + frag_iter->len;
676 if ((copy = end - offset) > 0) {
677 __wsum csum2 = 0;
678 if (copy > len)
679 copy = len;
680 if (skb_copy_and_csum_datagram(frag_iter,
681 offset - start,
682 to, copy,
683 &csum2))
684 goto fault;
685 *csump = csum_block_add(*csump, csum2, pos);
686 if ((len -= copy) == 0)
687 return 0;
688 offset += copy;
689 pos += copy;
690 }
691 start = end;
692 }
693 if (!len)
694 return 0;
695
696 fault:
697 iov_iter_revert(to, offset - start_off);
698 return -EFAULT;
699 }
700
701 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
702 {
703 __sum16 sum;
704
705 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
706 if (likely(!sum)) {
707 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
708 !skb->csum_complete_sw)
709 netdev_rx_csum_fault(skb->dev);
710 }
711 if (!skb_shared(skb))
712 skb->csum_valid = !sum;
713 return sum;
714 }
715 EXPORT_SYMBOL(__skb_checksum_complete_head);
716
717 __sum16 __skb_checksum_complete(struct sk_buff *skb)
718 {
719 __wsum csum;
720 __sum16 sum;
721
722 csum = skb_checksum(skb, 0, skb->len, 0);
723
724 /* skb->csum holds pseudo checksum */
725 sum = csum_fold(csum_add(skb->csum, csum));
726 if (likely(!sum)) {
727 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
728 !skb->csum_complete_sw)
729 netdev_rx_csum_fault(skb->dev);
730 }
731
732 if (!skb_shared(skb)) {
733 /* Save full packet checksum */
734 skb->csum = csum;
735 skb->ip_summed = CHECKSUM_COMPLETE;
736 skb->csum_complete_sw = 1;
737 skb->csum_valid = !sum;
738 }
739
740 return sum;
741 }
742 EXPORT_SYMBOL(__skb_checksum_complete);
743
744 /**
745 * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
746 * @skb: skbuff
747 * @hlen: hardware length
748 * @msg: destination
749 *
750 * Caller _must_ check that skb will fit to this iovec.
751 *
752 * Returns: 0 - success.
753 * -EINVAL - checksum failure.
754 * -EFAULT - fault during copy.
755 */
756 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
757 int hlen, struct msghdr *msg)
758 {
759 __wsum csum;
760 int chunk = skb->len - hlen;
761
762 if (!chunk)
763 return 0;
764
765 if (msg_data_left(msg) < chunk) {
766 if (__skb_checksum_complete(skb))
767 goto csum_error;
768 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
769 goto fault;
770 } else {
771 csum = csum_partial(skb->data, hlen, skb->csum);
772 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
773 chunk, &csum))
774 goto fault;
775 if (csum_fold(csum))
776 goto csum_error;
777 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
778 netdev_rx_csum_fault(skb->dev);
779 }
780 return 0;
781 csum_error:
782 iov_iter_revert(&msg->msg_iter, chunk);
783 return -EINVAL;
784 fault:
785 return -EFAULT;
786 }
787 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
788
789 /**
790 * datagram_poll - generic datagram poll
791 * @file: file struct
792 * @sock: socket
793 * @wait: poll table
794 *
795 * Datagram poll: Again totally generic. This also handles
796 * sequenced packet sockets providing the socket receive queue
797 * is only ever holding data ready to receive.
798 *
799 * Note: when you _don't_ use this routine for this protocol,
800 * and you use a different write policy from sock_writeable()
801 * then please supply your own write_space callback.
802 */
803 unsigned int datagram_poll(struct file *file, struct socket *sock,
804 poll_table *wait)
805 {
806 struct sock *sk = sock->sk;
807 unsigned int mask;
808
809 sock_poll_wait(file, sk_sleep(sk), wait);
810 mask = 0;
811
812 /* exceptional events? */
813 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
814 mask |= POLLERR |
815 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
816
817 if (sk->sk_shutdown & RCV_SHUTDOWN)
818 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
819 if (sk->sk_shutdown == SHUTDOWN_MASK)
820 mask |= POLLHUP;
821
822 /* readable? */
823 if (!skb_queue_empty(&sk->sk_receive_queue))
824 mask |= POLLIN | POLLRDNORM;
825
826 /* Connection-based need to check for termination and startup */
827 if (connection_based(sk)) {
828 if (sk->sk_state == TCP_CLOSE)
829 mask |= POLLHUP;
830 /* connection hasn't started yet? */
831 if (sk->sk_state == TCP_SYN_SENT)
832 return mask;
833 }
834
835 /* writable? */
836 if (sock_writeable(sk))
837 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
838 else
839 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
840
841 return mask;
842 }
843 EXPORT_SYMBOL(datagram_poll);