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 int __skb_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
);
133 EXPORT_SYMBOL(__skb_wait_for_more_packets
);
135 static struct sk_buff
*skb_set_peeked(struct sk_buff
*skb
)
137 struct sk_buff
*nskb
;
142 /* We have to unshare an skb before modifying it. */
143 if (!skb_shared(skb
))
146 nskb
= skb_clone(skb
, GFP_ATOMIC
);
148 return ERR_PTR(-ENOMEM
);
150 skb
->prev
->next
= nskb
;
151 skb
->next
->prev
= nskb
;
152 nskb
->prev
= skb
->prev
;
153 nskb
->next
= skb
->next
;
165 * __skb_try_recv_datagram - Receive a datagram skbuff
168 * @peeked: returns non-zero if this packet has been seen before
169 * @off: an offset in bytes to peek skb from. Returns an offset
170 * within an skb where data actually starts
171 * @err: error code returned
172 * @last: set to last peeked message to inform the wait function
173 * what to look for when peeking
175 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
176 * and possible races. This replaces identical code in packet, raw and
177 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
178 * the long standing peek and read race for datagram sockets. If you
179 * alter this routine remember it must be re-entrant.
181 * This function will lock the socket if a skb is returned, so
182 * the caller needs to unlock the socket in that case (usually by
183 * calling skb_free_datagram). Returns NULL with *err set to
184 * -EAGAIN if no data was available or to some other value if an
185 * error was detected.
187 * * It does not lock socket since today. This function is
188 * * free of race conditions. This measure should/can improve
189 * * significantly datagram socket latencies at high loads,
190 * * when data copying to user space takes lots of time.
191 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
195 * The order of the tests when we find no data waiting are specified
196 * quite explicitly by POSIX 1003.1g, don't change them without having
197 * the standard around please.
199 struct sk_buff
*__skb_try_recv_datagram(struct sock
*sk
, unsigned int flags
,
200 int *peeked
, int *off
, int *err
,
201 struct sk_buff
**last
)
203 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
205 unsigned long cpu_flags
;
207 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
209 int error
= sock_error(sk
);
215 /* Again only user level code calls this function, so nothing
216 * interrupt level will suddenly eat the receive_queue.
218 * Look at current nfs client by the way...
219 * However, this function was correct in any case. 8)
223 *last
= (struct sk_buff
*)queue
;
224 spin_lock_irqsave(&queue
->lock
, cpu_flags
);
225 skb_queue_walk(queue
, skb
) {
227 *peeked
= skb
->peeked
;
228 if (flags
& MSG_PEEK
) {
229 if (_off
>= skb
->len
&& (skb
->len
|| _off
||
235 skb
= skb_set_peeked(skb
);
236 error
= PTR_ERR(skb
);
238 spin_unlock_irqrestore(&queue
->lock
,
243 atomic_inc(&skb
->users
);
245 __skb_unlink(skb
, queue
);
247 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
252 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
253 } while (sk_can_busy_loop(sk
) &&
254 sk_busy_loop(sk
, flags
& MSG_DONTWAIT
));
262 EXPORT_SYMBOL(__skb_try_recv_datagram
);
264 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
265 int *peeked
, int *off
, int *err
)
267 struct sk_buff
*skb
, *last
;
270 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
273 skb
= __skb_try_recv_datagram(sk
, flags
, peeked
, off
, err
,
281 !__skb_wait_for_more_packets(sk
, err
, &timeo
, last
));
285 EXPORT_SYMBOL(__skb_recv_datagram
);
287 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
288 int noblock
, int *err
)
292 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
295 EXPORT_SYMBOL(skb_recv_datagram
);
297 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
300 sk_mem_reclaim_partial(sk
);
302 EXPORT_SYMBOL(skb_free_datagram
);
304 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
308 if (likely(atomic_read(&skb
->users
) == 1))
310 else if (likely(!atomic_dec_and_test(&skb
->users
)))
313 slow
= lock_sock_fast(sk
);
315 sk_mem_reclaim_partial(sk
);
316 unlock_sock_fast(sk
, slow
);
318 /* skb is now orphaned, can be freed outside of locked section */
321 EXPORT_SYMBOL(skb_free_datagram_locked
);
324 * skb_kill_datagram - Free a datagram skbuff forcibly
326 * @skb: datagram skbuff
329 * This function frees a datagram skbuff that was received by
330 * skb_recv_datagram. The flags argument must match the one
331 * used for skb_recv_datagram.
333 * If the MSG_PEEK flag is set, and the packet is still on the
334 * receive queue of the socket, it will be taken off the queue
335 * before it is freed.
337 * This function currently only disables BH when acquiring the
338 * sk_receive_queue lock. Therefore it must not be used in a
339 * context where that lock is acquired in an IRQ context.
341 * It returns 0 if the packet was removed by us.
344 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
348 if (flags
& MSG_PEEK
) {
350 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
351 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
352 __skb_unlink(skb
, &sk
->sk_receive_queue
);
353 atomic_dec(&skb
->users
);
356 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
360 atomic_inc(&sk
->sk_drops
);
361 sk_mem_reclaim_partial(sk
);
365 EXPORT_SYMBOL(skb_kill_datagram
);
368 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
369 * @skb: buffer to copy
370 * @offset: offset in the buffer to start copying from
371 * @to: iovec iterator to copy to
372 * @len: amount of data to copy from buffer to iovec
374 int skb_copy_datagram_iter(const struct sk_buff
*skb
, int offset
,
375 struct iov_iter
*to
, int len
)
377 int start
= skb_headlen(skb
);
378 int i
, copy
= start
- offset
;
379 struct sk_buff
*frag_iter
;
381 trace_skb_copy_datagram_iovec(skb
, len
);
387 if (copy_to_iter(skb
->data
+ offset
, copy
, to
) != copy
)
389 if ((len
-= copy
) == 0)
394 /* Copy paged appendix. Hmm... why does this look so complicated? */
395 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
397 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
399 WARN_ON(start
> offset
+ len
);
401 end
= start
+ skb_frag_size(frag
);
402 if ((copy
= end
- offset
) > 0) {
405 if (copy_page_to_iter(skb_frag_page(frag
),
406 frag
->page_offset
+ offset
-
407 start
, copy
, to
) != copy
)
416 skb_walk_frags(skb
, frag_iter
) {
419 WARN_ON(start
> offset
+ len
);
421 end
= start
+ frag_iter
->len
;
422 if ((copy
= end
- offset
) > 0) {
425 if (skb_copy_datagram_iter(frag_iter
, offset
- start
,
428 if ((len
-= copy
) == 0)
437 /* This is not really a user copy fault, but rather someone
438 * gave us a bogus length on the skb. We should probably
439 * print a warning here as it may indicate a kernel bug.
446 if (iov_iter_count(to
))
451 EXPORT_SYMBOL(skb_copy_datagram_iter
);
454 * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
455 * @skb: buffer to copy
456 * @offset: offset in the buffer to start copying to
457 * @from: the copy source
458 * @len: amount of data to copy to buffer from iovec
460 * Returns 0 or -EFAULT.
462 int skb_copy_datagram_from_iter(struct sk_buff
*skb
, int offset
,
463 struct iov_iter
*from
,
466 int start
= skb_headlen(skb
);
467 int i
, copy
= start
- offset
;
468 struct sk_buff
*frag_iter
;
474 if (copy_from_iter(skb
->data
+ offset
, copy
, from
) != copy
)
476 if ((len
-= copy
) == 0)
481 /* Copy paged appendix. Hmm... why does this look so complicated? */
482 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
484 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
486 WARN_ON(start
> offset
+ len
);
488 end
= start
+ skb_frag_size(frag
);
489 if ((copy
= end
- offset
) > 0) {
494 copied
= copy_page_from_iter(skb_frag_page(frag
),
495 frag
->page_offset
+ offset
- start
,
507 skb_walk_frags(skb
, frag_iter
) {
510 WARN_ON(start
> offset
+ len
);
512 end
= start
+ frag_iter
->len
;
513 if ((copy
= end
- offset
) > 0) {
516 if (skb_copy_datagram_from_iter(frag_iter
,
520 if ((len
-= copy
) == 0)
532 EXPORT_SYMBOL(skb_copy_datagram_from_iter
);
535 * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
536 * @skb: buffer to copy
537 * @from: the source to copy from
539 * The function will first copy up to headlen, and then pin the userspace
540 * pages and build frags through them.
542 * Returns 0, -EFAULT or -EMSGSIZE.
544 int zerocopy_sg_from_iter(struct sk_buff
*skb
, struct iov_iter
*from
)
546 int len
= iov_iter_count(from
);
547 int copy
= min_t(int, skb_headlen(skb
), len
);
550 /* copy up to skb headlen */
551 if (skb_copy_datagram_from_iter(skb
, 0, from
, copy
))
554 while (iov_iter_count(from
)) {
555 struct page
*pages
[MAX_SKB_FRAGS
];
558 unsigned long truesize
;
561 if (frag
== MAX_SKB_FRAGS
)
564 copied
= iov_iter_get_pages(from
, pages
, ~0U,
565 MAX_SKB_FRAGS
- frag
, &start
);
569 iov_iter_advance(from
, copied
);
571 truesize
= PAGE_ALIGN(copied
+ start
);
572 skb
->data_len
+= copied
;
574 skb
->truesize
+= truesize
;
575 atomic_add(truesize
, &skb
->sk
->sk_wmem_alloc
);
577 int size
= min_t(int, copied
, PAGE_SIZE
- start
);
578 skb_fill_page_desc(skb
, frag
++, pages
[n
], start
, size
);
586 EXPORT_SYMBOL(zerocopy_sg_from_iter
);
588 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
589 struct iov_iter
*to
, int len
,
592 int start
= skb_headlen(skb
);
593 int i
, copy
= start
- offset
;
594 struct sk_buff
*frag_iter
;
602 n
= csum_and_copy_to_iter(skb
->data
+ offset
, copy
, csump
, to
);
605 if ((len
-= copy
) == 0)
611 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
613 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
615 WARN_ON(start
> offset
+ len
);
617 end
= start
+ skb_frag_size(frag
);
618 if ((copy
= end
- offset
) > 0) {
620 struct page
*page
= skb_frag_page(frag
);
621 u8
*vaddr
= kmap(page
);
625 n
= csum_and_copy_to_iter(vaddr
+ frag
->page_offset
+
626 offset
- start
, copy
,
631 *csump
= csum_block_add(*csump
, csum2
, pos
);
640 skb_walk_frags(skb
, frag_iter
) {
643 WARN_ON(start
> offset
+ len
);
645 end
= start
+ frag_iter
->len
;
646 if ((copy
= end
- offset
) > 0) {
650 if (skb_copy_and_csum_datagram(frag_iter
,
655 *csump
= csum_block_add(*csump
, csum2
, pos
);
656 if ((len
-= copy
) == 0)
670 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
674 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
676 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
677 !skb
->csum_complete_sw
)
678 netdev_rx_csum_fault(skb
->dev
);
680 if (!skb_shared(skb
))
681 skb
->csum_valid
= !sum
;
684 EXPORT_SYMBOL(__skb_checksum_complete_head
);
686 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
691 csum
= skb_checksum(skb
, 0, skb
->len
, 0);
693 /* skb->csum holds pseudo checksum */
694 sum
= csum_fold(csum_add(skb
->csum
, csum
));
696 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
697 !skb
->csum_complete_sw
)
698 netdev_rx_csum_fault(skb
->dev
);
701 if (!skb_shared(skb
)) {
702 /* Save full packet checksum */
704 skb
->ip_summed
= CHECKSUM_COMPLETE
;
705 skb
->csum_complete_sw
= 1;
706 skb
->csum_valid
= !sum
;
711 EXPORT_SYMBOL(__skb_checksum_complete
);
714 * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
716 * @hlen: hardware length
719 * Caller _must_ check that skb will fit to this iovec.
721 * Returns: 0 - success.
722 * -EINVAL - checksum failure.
723 * -EFAULT - fault during copy.
725 int skb_copy_and_csum_datagram_msg(struct sk_buff
*skb
,
726 int hlen
, struct msghdr
*msg
)
729 int chunk
= skb
->len
- hlen
;
734 if (msg_data_left(msg
) < chunk
) {
735 if (__skb_checksum_complete(skb
))
737 if (skb_copy_datagram_msg(skb
, hlen
, msg
, chunk
))
740 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
741 if (skb_copy_and_csum_datagram(skb
, hlen
, &msg
->msg_iter
,
746 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
747 netdev_rx_csum_fault(skb
->dev
);
755 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg
);
758 * datagram_poll - generic datagram poll
763 * Datagram poll: Again totally generic. This also handles
764 * sequenced packet sockets providing the socket receive queue
765 * is only ever holding data ready to receive.
767 * Note: when you _don't_ use this routine for this protocol,
768 * and you use a different write policy from sock_writeable()
769 * then please supply your own write_space callback.
771 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
774 struct sock
*sk
= sock
->sk
;
777 sock_poll_wait(file
, sk_sleep(sk
), wait
);
780 /* exceptional events? */
781 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
783 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
785 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
786 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
787 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
791 if (!skb_queue_empty(&sk
->sk_receive_queue
))
792 mask
|= POLLIN
| POLLRDNORM
;
794 /* Connection-based need to check for termination and startup */
795 if (connection_based(sk
)) {
796 if (sk
->sk_state
== TCP_CLOSE
)
798 /* connection hasn't started yet? */
799 if (sk
->sk_state
== TCP_SYN_SENT
)
804 if (sock_writeable(sk
))
805 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
807 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
811 EXPORT_SYMBOL(datagram_poll
);