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.
12 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
16 * Alan Cox : NULL return from skb_peek_copy()
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
24 * Alan Cox : Fixed write poll of non IP protocol
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.
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <asm/uaccess.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>
54 #include <net/protocol.h>
55 #include <linux/skbuff.h>
57 #include <net/checksum.h>
59 #include <net/tcp_states.h>
60 #include <trace/events/skb.h>
61 #include <net/busy_poll.h>
64 * Is a socket 'connection oriented' ?
66 static inline int connection_based(struct sock *sk)
68 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
71 static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
74 unsigned long bits = (unsigned long)key;
77 * Avoid a wakeup if event not interesting for us
79 if (bits && !(bits & (POLLIN | POLLERR)))
81 return autoremove_wake_function(wait, mode, sync, key);
84 * Wait for the last received packet to be different from skb
86 static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
87 const struct sk_buff *skb)
90 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
92 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
95 error = sock_error(sk);
99 if (sk->sk_receive_queue.prev != skb)
102 /* Socket shut down? */
103 if (sk->sk_shutdown & RCV_SHUTDOWN)
106 /* Sequenced packets can come disconnected.
107 * If so we report the problem
110 if (connection_based(sk) &&
111 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
115 if (signal_pending(current))
119 *timeo_p = schedule_timeout(*timeo_p);
121 finish_wait(sk_sleep(sk), &wait);
124 error = sock_intr_errno(*timeo_p);
134 static int skb_set_peeked(struct sk_buff *skb)
136 struct sk_buff *nskb;
141 /* We have to unshare an skb before modifying it. */
142 if (!skb_shared(skb))
145 nskb = skb_clone(skb, GFP_ATOMIC);
149 skb->prev->next = nskb;
150 skb->next->prev = nskb;
151 nskb->prev = skb->prev;
152 nskb->next = skb->next;
164 * __skb_recv_datagram - Receive a datagram skbuff
167 * @peeked: returns non-zero if this packet has been seen before
168 * @off: an offset in bytes to peek skb from. Returns an offset
169 * within an skb where data actually starts
170 * @err: error code returned
172 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
173 * and possible races. This replaces identical code in packet, raw and
174 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
175 * the long standing peek and read race for datagram sockets. If you
176 * alter this routine remember it must be re-entrant.
178 * This function will lock the socket if a skb is returned, so the caller
179 * needs to unlock the socket in that case (usually by calling
182 * * It does not lock socket since today. This function is
183 * * free of race conditions. This measure should/can improve
184 * * significantly datagram socket latencies at high loads,
185 * * when data copying to user space takes lots of time.
186 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
190 * The order of the tests when we find no data waiting are specified
191 * quite explicitly by POSIX 1003.1g, don't change them without having
192 * the standard around please.
194 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
195 int *peeked, int *off, int *err)
197 struct sk_buff_head *queue = &sk->sk_receive_queue;
198 struct sk_buff *skb, *last;
199 unsigned long cpu_flags;
202 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
204 int error = sock_error(sk);
209 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
212 /* Again only user level code calls this function, so nothing
213 * interrupt level will suddenly eat the receive_queue.
215 * Look at current nfs client by the way...
216 * However, this function was correct in any case. 8)
220 last = (struct sk_buff *)queue;
221 spin_lock_irqsave(&queue->lock, cpu_flags);
222 skb_queue_walk(queue, skb) {
224 *peeked = skb->peeked;
225 if (flags & MSG_PEEK) {
226 if (_off >= skb->len && (skb->len || _off ||
232 error = skb_set_peeked(skb);
236 atomic_inc(&skb->users);
238 __skb_unlink(skb, queue);
240 spin_unlock_irqrestore(&queue->lock, cpu_flags);
244 spin_unlock_irqrestore(&queue->lock, cpu_flags);
246 if (sk_can_busy_loop(sk) &&
247 sk_busy_loop(sk, flags & MSG_DONTWAIT))
250 /* User doesn't want to wait */
255 } while (!wait_for_more_packets(sk, err, &timeo, last));
260 spin_unlock_irqrestore(&queue->lock, cpu_flags);
265 EXPORT_SYMBOL(__skb_recv_datagram);
267 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
268 int noblock, int *err)
272 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
275 EXPORT_SYMBOL(skb_recv_datagram);
277 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
280 sk_mem_reclaim_partial(sk);
282 EXPORT_SYMBOL(skb_free_datagram);
284 void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
288 if (likely(atomic_read(&skb->users) == 1))
290 else if (likely(!atomic_dec_and_test(&skb->users)))
293 slow = lock_sock_fast(sk);
295 sk_mem_reclaim_partial(sk);
296 unlock_sock_fast(sk, slow);
298 /* skb is now orphaned, can be freed outside of locked section */
301 EXPORT_SYMBOL(skb_free_datagram_locked);
304 * skb_kill_datagram - Free a datagram skbuff forcibly
306 * @skb: datagram skbuff
309 * This function frees a datagram skbuff that was received by
310 * skb_recv_datagram. The flags argument must match the one
311 * used for skb_recv_datagram.
313 * If the MSG_PEEK flag is set, and the packet is still on the
314 * receive queue of the socket, it will be taken off the queue
315 * before it is freed.
317 * This function currently only disables BH when acquiring the
318 * sk_receive_queue lock. Therefore it must not be used in a
319 * context where that lock is acquired in an IRQ context.
321 * It returns 0 if the packet was removed by us.
324 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
328 if (flags & MSG_PEEK) {
330 spin_lock_bh(&sk->sk_receive_queue.lock);
331 if (skb == skb_peek(&sk->sk_receive_queue)) {
332 __skb_unlink(skb, &sk->sk_receive_queue);
333 atomic_dec(&skb->users);
336 spin_unlock_bh(&sk->sk_receive_queue.lock);
340 atomic_inc(&sk->sk_drops);
341 sk_mem_reclaim_partial(sk);
345 EXPORT_SYMBOL(skb_kill_datagram);
348 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
349 * @skb: buffer to copy
350 * @offset: offset in the buffer to start copying from
351 * @to: iovec iterator to copy to
352 * @len: amount of data to copy from buffer to iovec
354 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
355 struct iov_iter *to, int len)
357 int start = skb_headlen(skb);
358 int i, copy = start - offset;
359 struct sk_buff *frag_iter;
361 trace_skb_copy_datagram_iovec(skb, len);
367 if (copy_to_iter(skb->data + offset, copy, to) != copy)
369 if ((len -= copy) == 0)
374 /* Copy paged appendix. Hmm... why does this look so complicated? */
375 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
377 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
379 WARN_ON(start > offset + len);
381 end = start + skb_frag_size(frag);
382 if ((copy = end - offset) > 0) {
385 if (copy_page_to_iter(skb_frag_page(frag),
386 frag->page_offset + offset -
387 start, copy, to) != copy)
396 skb_walk_frags(skb, frag_iter) {
399 WARN_ON(start > offset + len);
401 end = start + frag_iter->len;
402 if ((copy = end - offset) > 0) {
405 if (skb_copy_datagram_iter(frag_iter, offset - start,
408 if ((len -= copy) == 0)
417 /* This is not really a user copy fault, but rather someone
418 * gave us a bogus length on the skb. We should probably
419 * print a warning here as it may indicate a kernel bug.
426 if (iov_iter_count(to))
431 EXPORT_SYMBOL(skb_copy_datagram_iter);
434 * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
435 * @skb: buffer to copy
436 * @offset: offset in the buffer to start copying to
437 * @from: the copy source
438 * @len: amount of data to copy to buffer from iovec
440 * Returns 0 or -EFAULT.
442 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
443 struct iov_iter *from,
446 int start = skb_headlen(skb);
447 int i, copy = start - offset;
448 struct sk_buff *frag_iter;
454 if (copy_from_iter(skb->data + offset, copy, from) != copy)
456 if ((len -= copy) == 0)
461 /* Copy paged appendix. Hmm... why does this look so complicated? */
462 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
464 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
466 WARN_ON(start > offset + len);
468 end = start + skb_frag_size(frag);
469 if ((copy = end - offset) > 0) {
474 copied = copy_page_from_iter(skb_frag_page(frag),
475 frag->page_offset + offset - start,
487 skb_walk_frags(skb, frag_iter) {
490 WARN_ON(start > offset + len);
492 end = start + frag_iter->len;
493 if ((copy = end - offset) > 0) {
496 if (skb_copy_datagram_from_iter(frag_iter,
500 if ((len -= copy) == 0)
512 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
515 * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
516 * @skb: buffer to copy
517 * @from: the source to copy from
519 * The function will first copy up to headlen, and then pin the userspace
520 * pages and build frags through them.
522 * Returns 0, -EFAULT or -EMSGSIZE.
524 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
526 int len = iov_iter_count(from);
527 int copy = min_t(int, skb_headlen(skb), len);
530 /* copy up to skb headlen */
531 if (skb_copy_datagram_from_iter(skb, 0, from, copy))
534 while (iov_iter_count(from)) {
535 struct page *pages[MAX_SKB_FRAGS];
538 unsigned long truesize;
541 if (frag == MAX_SKB_FRAGS)
544 copied = iov_iter_get_pages(from, pages, ~0U,
545 MAX_SKB_FRAGS - frag, &start);
549 iov_iter_advance(from, copied);
551 truesize = PAGE_ALIGN(copied + start);
552 skb->data_len += copied;
554 skb->truesize += truesize;
555 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
557 int size = min_t(int, copied, PAGE_SIZE - start);
558 skb_fill_page_desc(skb, frag++, pages[n], start, size);
566 EXPORT_SYMBOL(zerocopy_sg_from_iter);
568 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
569 struct iov_iter *to, int len,
572 int start = skb_headlen(skb);
573 int i, copy = start - offset;
574 struct sk_buff *frag_iter;
582 n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
585 if ((len -= copy) == 0)
591 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
593 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
595 WARN_ON(start > offset + len);
597 end = start + skb_frag_size(frag);
598 if ((copy = end - offset) > 0) {
600 struct page *page = skb_frag_page(frag);
601 u8 *vaddr = kmap(page);
605 n = csum_and_copy_to_iter(vaddr + frag->page_offset +
606 offset - start, copy,
611 *csump = csum_block_add(*csump, csum2, pos);
620 skb_walk_frags(skb, frag_iter) {
623 WARN_ON(start > offset + len);
625 end = start + frag_iter->len;
626 if ((copy = end - offset) > 0) {
630 if (skb_copy_and_csum_datagram(frag_iter,
635 *csump = csum_block_add(*csump, csum2, pos);
636 if ((len -= copy) == 0)
650 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
654 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
656 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
657 !skb->csum_complete_sw)
658 netdev_rx_csum_fault(skb->dev);
660 if (!skb_shared(skb))
661 skb->csum_valid = !sum;
664 EXPORT_SYMBOL(__skb_checksum_complete_head);
666 __sum16 __skb_checksum_complete(struct sk_buff *skb)
671 csum = skb_checksum(skb, 0, skb->len, 0);
673 /* skb->csum holds pseudo checksum */
674 sum = csum_fold(csum_add(skb->csum, csum));
676 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
677 !skb->csum_complete_sw)
678 netdev_rx_csum_fault(skb->dev);
681 if (!skb_shared(skb)) {
682 /* Save full packet checksum */
684 skb->ip_summed = CHECKSUM_COMPLETE;
685 skb->csum_complete_sw = 1;
686 skb->csum_valid = !sum;
691 EXPORT_SYMBOL(__skb_checksum_complete);
694 * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
696 * @hlen: hardware length
699 * Caller _must_ check that skb will fit to this iovec.
701 * Returns: 0 - success.
702 * -EINVAL - checksum failure.
703 * -EFAULT - fault during copy.
705 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
706 int hlen, struct msghdr *msg)
709 int chunk = skb->len - hlen;
714 if (msg_data_left(msg) < chunk) {
715 if (__skb_checksum_complete(skb))
717 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
720 csum = csum_partial(skb->data, hlen, skb->csum);
721 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
726 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
727 netdev_rx_csum_fault(skb->dev);
735 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
738 * datagram_poll - generic datagram poll
743 * Datagram poll: Again totally generic. This also handles
744 * sequenced packet sockets providing the socket receive queue
745 * is only ever holding data ready to receive.
747 * Note: when you _don't_ use this routine for this protocol,
748 * and you use a different write policy from sock_writeable()
749 * then please supply your own write_space callback.
751 unsigned int datagram_poll(struct file *file, struct socket *sock,
754 struct sock *sk = sock->sk;
757 sock_poll_wait(file, sk_sleep(sk), wait);
760 /* exceptional events? */
761 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
763 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
765 if (sk->sk_shutdown & RCV_SHUTDOWN)
766 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
767 if (sk->sk_shutdown == SHUTDOWN_MASK)
771 if (!skb_queue_empty(&sk->sk_receive_queue))
772 mask |= POLLIN | POLLRDNORM;
774 /* Connection-based need to check for termination and startup */
775 if (connection_based(sk)) {
776 if (sk->sk_state == TCP_CLOSE)
778 /* connection hasn't started yet? */
779 if (sk->sk_state == TCP_SYN_SENT)
784 if (sock_writeable(sk))
785 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
787 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
791 EXPORT_SYMBOL(datagram_poll);