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>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
55 #include <net/checksum.h>
57 #include <net/tcp_states.h>
58 #include <trace/events/skb.h>
61 * Is a socket 'connection oriented' ?
63 static inline int connection_based(struct sock
*sk
)
65 return sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
;
68 static int receiver_wake_function(wait_queue_t
*wait
, unsigned int mode
, int sync
,
71 unsigned long bits
= (unsigned long)key
;
74 * Avoid a wakeup if event not interesting for us
76 if (bits
&& !(bits
& (POLLIN
| POLLERR
)))
78 return autoremove_wake_function(wait
, mode
, sync
, key
);
81 * Wait for the last received packet to be different from skb
83 static int wait_for_more_packets(struct sock
*sk
, int *err
, long *timeo_p
,
84 const struct sk_buff
*skb
)
87 DEFINE_WAIT_FUNC(wait
, receiver_wake_function
);
89 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
92 error
= sock_error(sk
);
96 if (sk
->sk_receive_queue
.prev
!= skb
)
99 /* Socket shut down? */
100 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
103 /* Sequenced packets can come disconnected.
104 * If so we report the problem
107 if (connection_based(sk
) &&
108 !(sk
->sk_state
== TCP_ESTABLISHED
|| sk
->sk_state
== TCP_LISTEN
))
112 if (signal_pending(current
))
116 *timeo_p
= schedule_timeout(*timeo_p
);
118 finish_wait(sk_sleep(sk
), &wait
);
121 error
= sock_intr_errno(*timeo_p
);
132 * __skb_recv_datagram - Receive a datagram skbuff
135 * @peeked: returns non-zero if this packet has been seen before
136 * @off: an offset in bytes to peek skb from. Returns an offset
137 * within an skb where data actually starts
138 * @err: error code returned
140 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
141 * and possible races. This replaces identical code in packet, raw and
142 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
143 * the long standing peek and read race for datagram sockets. If you
144 * alter this routine remember it must be re-entrant.
146 * This function will lock the socket if a skb is returned, so the caller
147 * needs to unlock the socket in that case (usually by calling
150 * * It does not lock socket since today. This function is
151 * * free of race conditions. This measure should/can improve
152 * * significantly datagram socket latencies at high loads,
153 * * when data copying to user space takes lots of time.
154 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
158 * The order of the tests when we find no data waiting are specified
159 * quite explicitly by POSIX 1003.1g, don't change them without having
160 * the standard around please.
162 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
163 int *peeked
, int *off
, int *err
)
165 struct sk_buff
*skb
, *last
;
168 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
170 int error
= sock_error(sk
);
175 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
178 /* Again only user level code calls this function, so nothing
179 * interrupt level will suddenly eat the receive_queue.
181 * Look at current nfs client by the way...
182 * However, this function was correct in any case. 8)
184 unsigned long cpu_flags
;
185 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
188 last
= (struct sk_buff
*)queue
;
189 spin_lock_irqsave(&queue
->lock
, cpu_flags
);
190 skb_queue_walk(queue
, skb
) {
192 *peeked
= skb
->peeked
;
193 if (flags
& MSG_PEEK
) {
194 if (_off
>= skb
->len
&& (skb
->len
|| _off
||
200 atomic_inc(&skb
->users
);
202 __skb_unlink(skb
, queue
);
204 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
208 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
210 /* User doesn't want to wait */
215 } while (!wait_for_more_packets(sk
, err
, &timeo
, last
));
223 EXPORT_SYMBOL(__skb_recv_datagram
);
225 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
226 int noblock
, int *err
)
230 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
233 EXPORT_SYMBOL(skb_recv_datagram
);
235 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
238 sk_mem_reclaim_partial(sk
);
240 EXPORT_SYMBOL(skb_free_datagram
);
242 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
246 if (likely(atomic_read(&skb
->users
) == 1))
248 else if (likely(!atomic_dec_and_test(&skb
->users
)))
251 slow
= lock_sock_fast(sk
);
253 sk_mem_reclaim_partial(sk
);
254 unlock_sock_fast(sk
, slow
);
256 /* skb is now orphaned, can be freed outside of locked section */
259 EXPORT_SYMBOL(skb_free_datagram_locked
);
262 * skb_kill_datagram - Free a datagram skbuff forcibly
264 * @skb: datagram skbuff
267 * This function frees a datagram skbuff that was received by
268 * skb_recv_datagram. The flags argument must match the one
269 * used for skb_recv_datagram.
271 * If the MSG_PEEK flag is set, and the packet is still on the
272 * receive queue of the socket, it will be taken off the queue
273 * before it is freed.
275 * This function currently only disables BH when acquiring the
276 * sk_receive_queue lock. Therefore it must not be used in a
277 * context where that lock is acquired in an IRQ context.
279 * It returns 0 if the packet was removed by us.
282 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
286 if (flags
& MSG_PEEK
) {
288 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
289 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
290 __skb_unlink(skb
, &sk
->sk_receive_queue
);
291 atomic_dec(&skb
->users
);
294 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
298 atomic_inc(&sk
->sk_drops
);
299 sk_mem_reclaim_partial(sk
);
303 EXPORT_SYMBOL(skb_kill_datagram
);
306 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
307 * @skb: buffer to copy
308 * @offset: offset in the buffer to start copying from
309 * @to: io vector to copy to
310 * @len: amount of data to copy from buffer to iovec
312 * Note: the iovec is modified during the copy.
314 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
315 struct iovec
*to
, int len
)
317 int start
= skb_headlen(skb
);
318 int i
, copy
= start
- offset
;
319 struct sk_buff
*frag_iter
;
321 trace_skb_copy_datagram_iovec(skb
, len
);
327 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
329 if ((len
-= copy
) == 0)
334 /* Copy paged appendix. Hmm... why does this look so complicated? */
335 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
337 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
339 WARN_ON(start
> offset
+ len
);
341 end
= start
+ skb_frag_size(frag
);
342 if ((copy
= end
- offset
) > 0) {
345 struct page
*page
= skb_frag_page(frag
);
350 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
351 offset
- start
, copy
);
362 skb_walk_frags(skb
, frag_iter
) {
365 WARN_ON(start
> offset
+ len
);
367 end
= start
+ frag_iter
->len
;
368 if ((copy
= end
- offset
) > 0) {
371 if (skb_copy_datagram_iovec(frag_iter
,
375 if ((len
-= copy
) == 0)
387 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
390 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
391 * @skb: buffer to copy
392 * @offset: offset in the buffer to start copying from
393 * @to: io vector to copy to
394 * @to_offset: offset in the io vector to start copying to
395 * @len: amount of data to copy from buffer to iovec
397 * Returns 0 or -EFAULT.
398 * Note: the iovec is not modified during the copy.
400 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
401 const struct iovec
*to
, int to_offset
,
404 int start
= skb_headlen(skb
);
405 int i
, copy
= start
- offset
;
406 struct sk_buff
*frag_iter
;
412 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
414 if ((len
-= copy
) == 0)
420 /* Copy paged appendix. Hmm... why does this look so complicated? */
421 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
423 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
425 WARN_ON(start
> offset
+ len
);
427 end
= start
+ skb_frag_size(frag
);
428 if ((copy
= end
- offset
) > 0) {
431 struct page
*page
= skb_frag_page(frag
);
436 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
437 offset
- start
, to_offset
, copy
);
449 skb_walk_frags(skb
, frag_iter
) {
452 WARN_ON(start
> offset
+ len
);
454 end
= start
+ frag_iter
->len
;
455 if ((copy
= end
- offset
) > 0) {
458 if (skb_copy_datagram_const_iovec(frag_iter
,
463 if ((len
-= copy
) == 0)
476 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
479 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
480 * @skb: buffer to copy
481 * @offset: offset in the buffer to start copying to
482 * @from: io vector to copy to
483 * @from_offset: offset in the io vector to start copying from
484 * @len: amount of data to copy to buffer from iovec
486 * Returns 0 or -EFAULT.
487 * Note: the iovec is not modified during the copy.
489 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
490 const struct iovec
*from
, int from_offset
,
493 int start
= skb_headlen(skb
);
494 int i
, copy
= start
- offset
;
495 struct sk_buff
*frag_iter
;
501 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
504 if ((len
-= copy
) == 0)
510 /* Copy paged appendix. Hmm... why does this look so complicated? */
511 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
513 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
515 WARN_ON(start
> offset
+ len
);
517 end
= start
+ skb_frag_size(frag
);
518 if ((copy
= end
- offset
) > 0) {
521 struct page
*page
= skb_frag_page(frag
);
526 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
528 from
, from_offset
, copy
);
541 skb_walk_frags(skb
, frag_iter
) {
544 WARN_ON(start
> offset
+ len
);
546 end
= start
+ frag_iter
->len
;
547 if ((copy
= end
- offset
) > 0) {
550 if (skb_copy_datagram_from_iovec(frag_iter
,
556 if ((len
-= copy
) == 0)
569 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
571 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
572 u8 __user
*to
, int len
,
575 int start
= skb_headlen(skb
);
576 int i
, copy
= start
- offset
;
577 struct sk_buff
*frag_iter
;
585 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
589 if ((len
-= copy
) == 0)
596 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
598 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
600 WARN_ON(start
> offset
+ len
);
602 end
= start
+ skb_frag_size(frag
);
603 if ((copy
= end
- offset
) > 0) {
607 struct page
*page
= skb_frag_page(frag
);
612 csum2
= csum_and_copy_to_user(vaddr
+
619 *csump
= csum_block_add(*csump
, csum2
, pos
);
629 skb_walk_frags(skb
, frag_iter
) {
632 WARN_ON(start
> offset
+ len
);
634 end
= start
+ frag_iter
->len
;
635 if ((copy
= end
- offset
) > 0) {
639 if (skb_copy_and_csum_datagram(frag_iter
,
644 *csump
= csum_block_add(*csump
, csum2
, pos
);
645 if ((len
-= copy
) == 0)
660 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
664 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
666 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
667 netdev_rx_csum_fault(skb
->dev
);
668 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
672 EXPORT_SYMBOL(__skb_checksum_complete_head
);
674 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
676 return __skb_checksum_complete_head(skb
, skb
->len
);
678 EXPORT_SYMBOL(__skb_checksum_complete
);
681 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
683 * @hlen: hardware length
686 * Caller _must_ check that skb will fit to this iovec.
688 * Returns: 0 - success.
689 * -EINVAL - checksum failure.
690 * -EFAULT - fault during copy. Beware, in this case iovec
693 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
694 int hlen
, struct iovec
*iov
)
697 int chunk
= skb
->len
- hlen
;
702 /* Skip filled elements.
703 * Pretty silly, look at memcpy_toiovec, though 8)
705 while (!iov
->iov_len
)
708 if (iov
->iov_len
< chunk
) {
709 if (__skb_checksum_complete(skb
))
711 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
714 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
715 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
720 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
721 netdev_rx_csum_fault(skb
->dev
);
722 iov
->iov_len
-= chunk
;
723 iov
->iov_base
+= chunk
;
731 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
734 * datagram_poll - generic datagram poll
739 * Datagram poll: Again totally generic. This also handles
740 * sequenced packet sockets providing the socket receive queue
741 * is only ever holding data ready to receive.
743 * Note: when you _don't_ use this routine for this protocol,
744 * and you use a different write policy from sock_writeable()
745 * then please supply your own write_space callback.
747 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
750 struct sock
*sk
= sock
->sk
;
753 sock_poll_wait(file
, sk_sleep(sk
), wait
);
756 /* exceptional events? */
757 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
759 (sock_flag(sk
, SOCK_SELECT_ERR_QUEUE
) ? POLLPRI
: 0);
761 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
762 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
763 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
767 if (!skb_queue_empty(&sk
->sk_receive_queue
))
768 mask
|= POLLIN
| POLLRDNORM
;
770 /* Connection-based need to check for termination and startup */
771 if (connection_based(sk
)) {
772 if (sk
->sk_state
== TCP_CLOSE
)
774 /* connection hasn't started yet? */
775 if (sk
->sk_state
== TCP_SYN_SENT
)
780 if (sock_writeable(sk
))
781 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
783 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
787 EXPORT_SYMBOL(datagram_poll
);