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>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
56 #include <net/checksum.h>
58 #include <net/tcp_states.h>
59 #include <trace/events/skb.h>
60 #include <net/busy_poll.h>
63 * Is a socket 'connection oriented' ?
65 static inline int connection_based(struct sock
*sk
)
67 return sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
;
70 static int receiver_wake_function(wait_queue_t
*wait
, unsigned int mode
, int sync
,
73 unsigned long bits
= (unsigned long)key
;
76 * Avoid a wakeup if event not interesting for us
78 if (bits
&& !(bits
& (POLLIN
| POLLERR
)))
80 return autoremove_wake_function(wait
, mode
, sync
, key
);
83 * Wait for the last received packet to be different from skb
85 static int wait_for_more_packets(struct sock
*sk
, int *err
, long *timeo_p
,
86 const struct sk_buff
*skb
)
89 DEFINE_WAIT_FUNC(wait
, receiver_wake_function
);
91 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
94 error
= sock_error(sk
);
98 if (sk
->sk_receive_queue
.prev
!= skb
)
101 /* Socket shut down? */
102 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
105 /* Sequenced packets can come disconnected.
106 * If so we report the problem
109 if (connection_based(sk
) &&
110 !(sk
->sk_state
== TCP_ESTABLISHED
|| sk
->sk_state
== TCP_LISTEN
))
114 if (signal_pending(current
))
118 *timeo_p
= schedule_timeout(*timeo_p
);
120 finish_wait(sk_sleep(sk
), &wait
);
123 error
= sock_intr_errno(*timeo_p
);
134 * __skb_recv_datagram - Receive a datagram skbuff
137 * @peeked: returns non-zero if this packet has been seen before
138 * @off: an offset in bytes to peek skb from. Returns an offset
139 * within an skb where data actually starts
140 * @err: error code returned
142 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
143 * and possible races. This replaces identical code in packet, raw and
144 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
145 * the long standing peek and read race for datagram sockets. If you
146 * alter this routine remember it must be re-entrant.
148 * This function will lock the socket if a skb is returned, so the caller
149 * needs to unlock the socket in that case (usually by calling
152 * * It does not lock socket since today. This function is
153 * * free of race conditions. This measure should/can improve
154 * * significantly datagram socket latencies at high loads,
155 * * when data copying to user space takes lots of time.
156 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
160 * The order of the tests when we find no data waiting are specified
161 * quite explicitly by POSIX 1003.1g, don't change them without having
162 * the standard around please.
164 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
165 int *peeked
, int *off
, int *err
)
167 struct sk_buff
*skb
, *last
;
170 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
172 int error
= sock_error(sk
);
177 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
180 /* Again only user level code calls this function, so nothing
181 * interrupt level will suddenly eat the receive_queue.
183 * Look at current nfs client by the way...
184 * However, this function was correct in any case. 8)
186 unsigned long cpu_flags
;
187 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
190 last
= (struct sk_buff
*)queue
;
191 spin_lock_irqsave(&queue
->lock
, cpu_flags
);
192 skb_queue_walk(queue
, skb
) {
194 *peeked
= skb
->peeked
;
195 if (flags
& MSG_PEEK
) {
196 if (_off
>= skb
->len
&& (skb
->len
|| _off
||
202 atomic_inc(&skb
->users
);
204 __skb_unlink(skb
, queue
);
206 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
210 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
212 if (sk_can_busy_loop(sk
) &&
213 sk_busy_loop(sk
, flags
& MSG_DONTWAIT
))
216 /* User doesn't want to wait */
221 } while (!wait_for_more_packets(sk
, err
, &timeo
, last
));
229 EXPORT_SYMBOL(__skb_recv_datagram
);
231 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
232 int noblock
, int *err
)
236 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
239 EXPORT_SYMBOL(skb_recv_datagram
);
241 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
244 sk_mem_reclaim_partial(sk
);
246 EXPORT_SYMBOL(skb_free_datagram
);
248 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
252 if (likely(atomic_read(&skb
->users
) == 1))
254 else if (likely(!atomic_dec_and_test(&skb
->users
)))
257 slow
= lock_sock_fast(sk
);
259 sk_mem_reclaim_partial(sk
);
260 unlock_sock_fast(sk
, slow
);
262 /* skb is now orphaned, can be freed outside of locked section */
265 EXPORT_SYMBOL(skb_free_datagram_locked
);
268 * skb_kill_datagram - Free a datagram skbuff forcibly
270 * @skb: datagram skbuff
273 * This function frees a datagram skbuff that was received by
274 * skb_recv_datagram. The flags argument must match the one
275 * used for skb_recv_datagram.
277 * If the MSG_PEEK flag is set, and the packet is still on the
278 * receive queue of the socket, it will be taken off the queue
279 * before it is freed.
281 * This function currently only disables BH when acquiring the
282 * sk_receive_queue lock. Therefore it must not be used in a
283 * context where that lock is acquired in an IRQ context.
285 * It returns 0 if the packet was removed by us.
288 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
292 if (flags
& MSG_PEEK
) {
294 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
295 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
296 __skb_unlink(skb
, &sk
->sk_receive_queue
);
297 atomic_dec(&skb
->users
);
300 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
304 atomic_inc(&sk
->sk_drops
);
305 sk_mem_reclaim_partial(sk
);
309 EXPORT_SYMBOL(skb_kill_datagram
);
312 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
313 * @skb: buffer to copy
314 * @offset: offset in the buffer to start copying from
315 * @to: io vector to copy to
316 * @len: amount of data to copy from buffer to iovec
318 * Note: the iovec is modified during the copy.
320 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
321 struct iovec
*to
, int len
)
323 int start
= skb_headlen(skb
);
324 int i
, copy
= start
- offset
;
325 struct sk_buff
*frag_iter
;
327 trace_skb_copy_datagram_iovec(skb
, len
);
333 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
335 if ((len
-= copy
) == 0)
340 /* Copy paged appendix. Hmm... why does this look so complicated? */
341 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
343 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
345 WARN_ON(start
> offset
+ len
);
347 end
= start
+ skb_frag_size(frag
);
348 if ((copy
= end
- offset
) > 0) {
351 struct page
*page
= skb_frag_page(frag
);
356 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
357 offset
- start
, copy
);
368 skb_walk_frags(skb
, frag_iter
) {
371 WARN_ON(start
> offset
+ len
);
373 end
= start
+ frag_iter
->len
;
374 if ((copy
= end
- offset
) > 0) {
377 if (skb_copy_datagram_iovec(frag_iter
,
381 if ((len
-= copy
) == 0)
393 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
396 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
397 * @skb: buffer to copy
398 * @offset: offset in the buffer to start copying from
399 * @to: io vector to copy to
400 * @to_offset: offset in the io vector to start copying to
401 * @len: amount of data to copy from buffer to iovec
403 * Returns 0 or -EFAULT.
404 * Note: the iovec is not modified during the copy.
406 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
407 const struct iovec
*to
, int to_offset
,
410 int start
= skb_headlen(skb
);
411 int i
, copy
= start
- offset
;
412 struct sk_buff
*frag_iter
;
418 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
420 if ((len
-= copy
) == 0)
426 /* Copy paged appendix. Hmm... why does this look so complicated? */
427 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
429 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
431 WARN_ON(start
> offset
+ len
);
433 end
= start
+ skb_frag_size(frag
);
434 if ((copy
= end
- offset
) > 0) {
437 struct page
*page
= skb_frag_page(frag
);
442 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
443 offset
- start
, to_offset
, copy
);
455 skb_walk_frags(skb
, frag_iter
) {
458 WARN_ON(start
> offset
+ len
);
460 end
= start
+ frag_iter
->len
;
461 if ((copy
= end
- offset
) > 0) {
464 if (skb_copy_datagram_const_iovec(frag_iter
,
469 if ((len
-= copy
) == 0)
482 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
485 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
486 * @skb: buffer to copy
487 * @offset: offset in the buffer to start copying to
488 * @from: io vector to copy to
489 * @from_offset: offset in the io vector to start copying from
490 * @len: amount of data to copy to buffer from iovec
492 * Returns 0 or -EFAULT.
493 * Note: the iovec is not modified during the copy.
495 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
496 const struct iovec
*from
, int from_offset
,
499 int start
= skb_headlen(skb
);
500 int i
, copy
= start
- offset
;
501 struct sk_buff
*frag_iter
;
507 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
510 if ((len
-= copy
) == 0)
516 /* Copy paged appendix. Hmm... why does this look so complicated? */
517 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
519 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
521 WARN_ON(start
> offset
+ len
);
523 end
= start
+ skb_frag_size(frag
);
524 if ((copy
= end
- offset
) > 0) {
527 struct page
*page
= skb_frag_page(frag
);
532 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
534 from
, from_offset
, copy
);
547 skb_walk_frags(skb
, frag_iter
) {
550 WARN_ON(start
> offset
+ len
);
552 end
= start
+ frag_iter
->len
;
553 if ((copy
= end
- offset
) > 0) {
556 if (skb_copy_datagram_from_iovec(frag_iter
,
562 if ((len
-= copy
) == 0)
575 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
578 * zerocopy_sg_from_iovec - Build a zerocopy datagram from an iovec
579 * @skb: buffer to copy
580 * @from: io vector to copy to
581 * @offset: offset in the io vector to start copying from
582 * @count: amount of vectors to copy to buffer from
584 * The function will first copy up to headlen, and then pin the userspace
585 * pages and build frags through them.
587 * Returns 0, -EFAULT or -EMSGSIZE.
588 * Note: the iovec is not modified during the copy
590 int zerocopy_sg_from_iovec(struct sk_buff
*skb
, const struct iovec
*from
,
591 int offset
, size_t count
)
593 int len
= iov_length(from
, count
) - offset
;
594 int copy
= skb_headlen(skb
);
595 int size
, offset1
= 0;
598 /* Skip over from offset */
599 while (count
&& (offset
>= from
->iov_len
)) {
600 offset
-= from
->iov_len
;
605 /* copy up to skb headlen */
606 while (count
&& (copy
> 0)) {
607 size
= min_t(unsigned int, copy
, from
->iov_len
- offset
);
608 if (copy_from_user(skb
->data
+ offset1
, from
->iov_base
+ offset
,
625 struct page
*page
[MAX_SKB_FRAGS
];
628 unsigned long truesize
;
630 len
= from
->iov_len
- offset
;
636 base
= (unsigned long)from
->iov_base
+ offset
;
637 size
= ((base
& ~PAGE_MASK
) + len
+ ~PAGE_MASK
) >> PAGE_SHIFT
;
638 if (i
+ size
> MAX_SKB_FRAGS
)
640 num_pages
= get_user_pages_fast(base
, size
, 0, &page
[i
]);
641 if (num_pages
!= size
) {
642 release_pages(&page
[i
], num_pages
, 0);
645 truesize
= size
* PAGE_SIZE
;
646 skb
->data_len
+= len
;
648 skb
->truesize
+= truesize
;
649 atomic_add(truesize
, &skb
->sk
->sk_wmem_alloc
);
651 int off
= base
& ~PAGE_MASK
;
652 int size
= min_t(int, len
, PAGE_SIZE
- off
);
653 skb_fill_page_desc(skb
, i
, page
[i
], off
, size
);
663 EXPORT_SYMBOL(zerocopy_sg_from_iovec
);
665 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
666 u8 __user
*to
, int len
,
669 int start
= skb_headlen(skb
);
670 int i
, copy
= start
- offset
;
671 struct sk_buff
*frag_iter
;
679 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
683 if ((len
-= copy
) == 0)
690 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
692 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
694 WARN_ON(start
> offset
+ len
);
696 end
= start
+ skb_frag_size(frag
);
697 if ((copy
= end
- offset
) > 0) {
701 struct page
*page
= skb_frag_page(frag
);
706 csum2
= csum_and_copy_to_user(vaddr
+
713 *csump
= csum_block_add(*csump
, csum2
, pos
);
723 skb_walk_frags(skb
, frag_iter
) {
726 WARN_ON(start
> offset
+ len
);
728 end
= start
+ frag_iter
->len
;
729 if ((copy
= end
- offset
) > 0) {
733 if (skb_copy_and_csum_datagram(frag_iter
,
738 *csump
= csum_block_add(*csump
, csum2
, pos
);
739 if ((len
-= copy
) == 0)
754 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
758 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
760 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
761 netdev_rx_csum_fault(skb
->dev
);
762 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
766 EXPORT_SYMBOL(__skb_checksum_complete_head
);
768 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
770 return __skb_checksum_complete_head(skb
, skb
->len
);
772 EXPORT_SYMBOL(__skb_checksum_complete
);
775 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
777 * @hlen: hardware length
780 * Caller _must_ check that skb will fit to this iovec.
782 * Returns: 0 - success.
783 * -EINVAL - checksum failure.
784 * -EFAULT - fault during copy. Beware, in this case iovec
787 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
788 int hlen
, struct iovec
*iov
)
791 int chunk
= skb
->len
- hlen
;
796 /* Skip filled elements.
797 * Pretty silly, look at memcpy_toiovec, though 8)
799 while (!iov
->iov_len
)
802 if (iov
->iov_len
< chunk
) {
803 if (__skb_checksum_complete(skb
))
805 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
808 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
809 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
814 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
815 netdev_rx_csum_fault(skb
->dev
);
816 iov
->iov_len
-= chunk
;
817 iov
->iov_base
+= chunk
;
825 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
828 * datagram_poll - generic datagram poll
833 * Datagram poll: Again totally generic. This also handles
834 * sequenced packet sockets providing the socket receive queue
835 * is only ever holding data ready to receive.
837 * Note: when you _don't_ use this routine for this protocol,
838 * and you use a different write policy from sock_writeable()
839 * then please supply your own write_space callback.
841 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
844 struct sock
*sk
= sock
->sk
;
847 sock_poll_wait(file
, sk_sleep(sk
), wait
);
850 /* exceptional events? */
851 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
853 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
855 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
856 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
857 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
861 if (!skb_queue_empty(&sk
->sk_receive_queue
))
862 mask
|= POLLIN
| POLLRDNORM
;
864 /* Connection-based need to check for termination and startup */
865 if (connection_based(sk
)) {
866 if (sk
->sk_state
== TCP_CLOSE
)
868 /* connection hasn't started yet? */
869 if (sk
->sk_state
== TCP_SYN_SENT
)
874 if (sock_writeable(sk
))
875 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
877 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
881 EXPORT_SYMBOL(datagram_poll
);