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>
40 #include <asm/system.h>
42 #include <linux/interrupt.h>
43 #include <linux/errno.h>
44 #include <linux/sched.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/poll.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/slab.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>
62 * Is a socket 'connection oriented' ?
64 static inline int connection_based(struct sock
*sk
)
66 return sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
;
69 static int receiver_wake_function(wait_queue_t
*wait
, unsigned mode
, int sync
,
72 unsigned long bits
= (unsigned long)key
;
75 * Avoid a wakeup if event not interesting for us
77 if (bits
&& !(bits
& (POLLIN
| POLLERR
)))
79 return autoremove_wake_function(wait
, mode
, sync
, key
);
84 static int wait_for_packet(struct sock
*sk
, int *err
, long *timeo_p
)
87 DEFINE_WAIT_FUNC(wait
, receiver_wake_function
);
89 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
92 error
= sock_error(sk
);
96 if (!skb_queue_empty(&sk
->sk_receive_queue
))
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
->sk_sleep
, &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 * @err: error code returned
138 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
139 * and possible races. This replaces identical code in packet, raw and
140 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
141 * the long standing peek and read race for datagram sockets. If you
142 * alter this routine remember it must be re-entrant.
144 * This function will lock the socket if a skb is returned, so the caller
145 * needs to unlock the socket in that case (usually by calling
148 * * It does not lock socket since today. This function is
149 * * free of race conditions. This measure should/can improve
150 * * significantly datagram socket latencies at high loads,
151 * * when data copying to user space takes lots of time.
152 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
156 * The order of the tests when we find no data waiting are specified
157 * quite explicitly by POSIX 1003.1g, don't change them without having
158 * the standard around please.
160 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned flags
,
161 int *peeked
, int *err
)
166 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
168 int error
= sock_error(sk
);
173 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
176 /* Again only user level code calls this function, so nothing
177 * interrupt level will suddenly eat the receive_queue.
179 * Look at current nfs client by the way...
180 * However, this function was corrent in any case. 8)
182 unsigned long cpu_flags
;
184 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, cpu_flags
);
185 skb
= skb_peek(&sk
->sk_receive_queue
);
187 *peeked
= skb
->peeked
;
188 if (flags
& MSG_PEEK
) {
190 atomic_inc(&skb
->users
);
192 __skb_unlink(skb
, &sk
->sk_receive_queue
);
194 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, cpu_flags
);
199 /* User doesn't want to wait */
204 } while (!wait_for_packet(sk
, err
, &timeo
));
212 EXPORT_SYMBOL(__skb_recv_datagram
);
214 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
215 int noblock
, int *err
)
219 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
223 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
226 sk_mem_reclaim_partial(sk
);
228 EXPORT_SYMBOL(skb_free_datagram
);
230 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
233 skb_free_datagram(sk
, skb
);
236 EXPORT_SYMBOL(skb_free_datagram_locked
);
239 * skb_kill_datagram - Free a datagram skbuff forcibly
241 * @skb: datagram skbuff
244 * This function frees a datagram skbuff that was received by
245 * skb_recv_datagram. The flags argument must match the one
246 * used for skb_recv_datagram.
248 * If the MSG_PEEK flag is set, and the packet is still on the
249 * receive queue of the socket, it will be taken off the queue
250 * before it is freed.
252 * This function currently only disables BH when acquiring the
253 * sk_receive_queue lock. Therefore it must not be used in a
254 * context where that lock is acquired in an IRQ context.
256 * It returns 0 if the packet was removed by us.
259 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
263 if (flags
& MSG_PEEK
) {
265 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
266 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
267 __skb_unlink(skb
, &sk
->sk_receive_queue
);
268 atomic_dec(&skb
->users
);
271 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
275 atomic_inc(&sk
->sk_drops
);
276 sk_mem_reclaim_partial(sk
);
281 EXPORT_SYMBOL(skb_kill_datagram
);
284 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
285 * @skb: buffer to copy
286 * @offset: offset in the buffer to start copying from
287 * @to: io vector to copy to
288 * @len: amount of data to copy from buffer to iovec
290 * Note: the iovec is modified during the copy.
292 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
293 struct iovec
*to
, int len
)
295 int start
= skb_headlen(skb
);
296 int i
, copy
= start
- offset
;
297 struct sk_buff
*frag_iter
;
299 trace_skb_copy_datagram_iovec(skb
, len
);
305 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
307 if ((len
-= copy
) == 0)
312 /* Copy paged appendix. Hmm... why does this look so complicated? */
313 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
316 WARN_ON(start
> offset
+ len
);
318 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
319 if ((copy
= end
- offset
) > 0) {
322 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
323 struct page
*page
= frag
->page
;
328 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
329 offset
- start
, copy
);
340 skb_walk_frags(skb
, frag_iter
) {
343 WARN_ON(start
> offset
+ len
);
345 end
= start
+ frag_iter
->len
;
346 if ((copy
= end
- offset
) > 0) {
349 if (skb_copy_datagram_iovec(frag_iter
,
353 if ((len
-= copy
) == 0)
367 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
368 * @skb: buffer to copy
369 * @offset: offset in the buffer to start copying from
370 * @to: io vector to copy to
371 * @to_offset: offset in the io vector to start copying to
372 * @len: amount of data to copy from buffer to iovec
374 * Returns 0 or -EFAULT.
375 * Note: the iovec is not modified during the copy.
377 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
378 const struct iovec
*to
, int to_offset
,
381 int start
= skb_headlen(skb
);
382 int i
, copy
= start
- offset
;
383 struct sk_buff
*frag_iter
;
389 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
391 if ((len
-= copy
) == 0)
397 /* Copy paged appendix. Hmm... why does this look so complicated? */
398 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
401 WARN_ON(start
> offset
+ len
);
403 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
404 if ((copy
= end
- offset
) > 0) {
407 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
408 struct page
*page
= frag
->page
;
413 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
414 offset
- start
, to_offset
, copy
);
426 skb_walk_frags(skb
, frag_iter
) {
429 WARN_ON(start
> offset
+ len
);
431 end
= start
+ frag_iter
->len
;
432 if ((copy
= end
- offset
) > 0) {
435 if (skb_copy_datagram_const_iovec(frag_iter
,
440 if ((len
-= copy
) == 0)
453 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
456 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
457 * @skb: buffer to copy
458 * @offset: offset in the buffer to start copying to
459 * @from: io vector to copy to
460 * @from_offset: offset in the io vector to start copying from
461 * @len: amount of data to copy to buffer from iovec
463 * Returns 0 or -EFAULT.
464 * Note: the iovec is not modified during the copy.
466 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
467 const struct iovec
*from
, int from_offset
,
470 int start
= skb_headlen(skb
);
471 int i
, copy
= start
- offset
;
472 struct sk_buff
*frag_iter
;
478 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
481 if ((len
-= copy
) == 0)
487 /* Copy paged appendix. Hmm... why does this look so complicated? */
488 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
491 WARN_ON(start
> offset
+ len
);
493 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
494 if ((copy
= end
- offset
) > 0) {
497 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
498 struct page
*page
= frag
->page
;
503 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
505 from
, from_offset
, copy
);
518 skb_walk_frags(skb
, frag_iter
) {
521 WARN_ON(start
> offset
+ len
);
523 end
= start
+ frag_iter
->len
;
524 if ((copy
= end
- offset
) > 0) {
527 if (skb_copy_datagram_from_iovec(frag_iter
,
533 if ((len
-= copy
) == 0)
546 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
548 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
549 u8 __user
*to
, int len
,
552 int start
= skb_headlen(skb
);
553 int i
, copy
= start
- offset
;
554 struct sk_buff
*frag_iter
;
562 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
566 if ((len
-= copy
) == 0)
573 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
576 WARN_ON(start
> offset
+ len
);
578 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
579 if ((copy
= end
- offset
) > 0) {
583 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
584 struct page
*page
= frag
->page
;
589 csum2
= csum_and_copy_to_user(vaddr
+
596 *csump
= csum_block_add(*csump
, csum2
, pos
);
606 skb_walk_frags(skb
, frag_iter
) {
609 WARN_ON(start
> offset
+ len
);
611 end
= start
+ frag_iter
->len
;
612 if ((copy
= end
- offset
) > 0) {
616 if (skb_copy_and_csum_datagram(frag_iter
,
621 *csump
= csum_block_add(*csump
, csum2
, pos
);
622 if ((len
-= copy
) == 0)
637 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
641 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
643 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
644 netdev_rx_csum_fault(skb
->dev
);
645 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
649 EXPORT_SYMBOL(__skb_checksum_complete_head
);
651 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
653 return __skb_checksum_complete_head(skb
, skb
->len
);
655 EXPORT_SYMBOL(__skb_checksum_complete
);
658 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
660 * @hlen: hardware length
663 * Caller _must_ check that skb will fit to this iovec.
665 * Returns: 0 - success.
666 * -EINVAL - checksum failure.
667 * -EFAULT - fault during copy. Beware, in this case iovec
670 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
671 int hlen
, struct iovec
*iov
)
674 int chunk
= skb
->len
- hlen
;
679 /* Skip filled elements.
680 * Pretty silly, look at memcpy_toiovec, though 8)
682 while (!iov
->iov_len
)
685 if (iov
->iov_len
< chunk
) {
686 if (__skb_checksum_complete(skb
))
688 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
691 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
692 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
697 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
698 netdev_rx_csum_fault(skb
->dev
);
699 iov
->iov_len
-= chunk
;
700 iov
->iov_base
+= chunk
;
710 * datagram_poll - generic datagram poll
715 * Datagram poll: Again totally generic. This also handles
716 * sequenced packet sockets providing the socket receive queue
717 * is only ever holding data ready to receive.
719 * Note: when you _don't_ use this routine for this protocol,
720 * and you use a different write policy from sock_writeable()
721 * then please supply your own write_space callback.
723 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
726 struct sock
*sk
= sock
->sk
;
729 sock_poll_wait(file
, sk
->sk_sleep
, wait
);
732 /* exceptional events? */
733 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
735 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
737 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
741 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
742 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
743 mask
|= POLLIN
| POLLRDNORM
;
745 /* Connection-based need to check for termination and startup */
746 if (connection_based(sk
)) {
747 if (sk
->sk_state
== TCP_CLOSE
)
749 /* connection hasn't started yet? */
750 if (sk
->sk_state
== TCP_SYN_SENT
)
755 if (sock_writeable(sk
))
756 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
758 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
763 EXPORT_SYMBOL(datagram_poll
);
764 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
765 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
766 EXPORT_SYMBOL(skb_recv_datagram
);