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_sleep(sk
), &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_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 * @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
)
232 if (likely(atomic_read(&skb
->users
) == 1))
234 else if (likely(!atomic_dec_and_test(&skb
->users
)))
239 sk_mem_reclaim_partial(sk
);
242 /* skb is now orphaned, can be freed outside of locked section */
245 EXPORT_SYMBOL(skb_free_datagram_locked
);
248 * skb_kill_datagram - Free a datagram skbuff forcibly
250 * @skb: datagram skbuff
253 * This function frees a datagram skbuff that was received by
254 * skb_recv_datagram. The flags argument must match the one
255 * used for skb_recv_datagram.
257 * If the MSG_PEEK flag is set, and the packet is still on the
258 * receive queue of the socket, it will be taken off the queue
259 * before it is freed.
261 * This function currently only disables BH when acquiring the
262 * sk_receive_queue lock. Therefore it must not be used in a
263 * context where that lock is acquired in an IRQ context.
265 * It returns 0 if the packet was removed by us.
268 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
272 if (flags
& MSG_PEEK
) {
274 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
275 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
276 __skb_unlink(skb
, &sk
->sk_receive_queue
);
277 atomic_dec(&skb
->users
);
280 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
284 atomic_inc(&sk
->sk_drops
);
285 sk_mem_reclaim_partial(sk
);
290 EXPORT_SYMBOL(skb_kill_datagram
);
293 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
294 * @skb: buffer to copy
295 * @offset: offset in the buffer to start copying from
296 * @to: io vector to copy to
297 * @len: amount of data to copy from buffer to iovec
299 * Note: the iovec is modified during the copy.
301 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
302 struct iovec
*to
, int len
)
304 int start
= skb_headlen(skb
);
305 int i
, copy
= start
- offset
;
306 struct sk_buff
*frag_iter
;
308 trace_skb_copy_datagram_iovec(skb
, len
);
314 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
316 if ((len
-= copy
) == 0)
321 /* Copy paged appendix. Hmm... why does this look so complicated? */
322 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
325 WARN_ON(start
> offset
+ len
);
327 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
328 if ((copy
= end
- offset
) > 0) {
331 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
332 struct page
*page
= frag
->page
;
337 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
338 offset
- start
, copy
);
349 skb_walk_frags(skb
, frag_iter
) {
352 WARN_ON(start
> offset
+ len
);
354 end
= start
+ frag_iter
->len
;
355 if ((copy
= end
- offset
) > 0) {
358 if (skb_copy_datagram_iovec(frag_iter
,
362 if ((len
-= copy
) == 0)
376 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
377 * @skb: buffer to copy
378 * @offset: offset in the buffer to start copying from
379 * @to: io vector to copy to
380 * @to_offset: offset in the io vector to start copying to
381 * @len: amount of data to copy from buffer to iovec
383 * Returns 0 or -EFAULT.
384 * Note: the iovec is not modified during the copy.
386 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
387 const struct iovec
*to
, int to_offset
,
390 int start
= skb_headlen(skb
);
391 int i
, copy
= start
- offset
;
392 struct sk_buff
*frag_iter
;
398 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
400 if ((len
-= copy
) == 0)
406 /* Copy paged appendix. Hmm... why does this look so complicated? */
407 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
410 WARN_ON(start
> offset
+ len
);
412 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
413 if ((copy
= end
- offset
) > 0) {
416 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
417 struct page
*page
= frag
->page
;
422 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
423 offset
- start
, to_offset
, copy
);
435 skb_walk_frags(skb
, frag_iter
) {
438 WARN_ON(start
> offset
+ len
);
440 end
= start
+ frag_iter
->len
;
441 if ((copy
= end
- offset
) > 0) {
444 if (skb_copy_datagram_const_iovec(frag_iter
,
449 if ((len
-= copy
) == 0)
462 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
465 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
466 * @skb: buffer to copy
467 * @offset: offset in the buffer to start copying to
468 * @from: io vector to copy to
469 * @from_offset: offset in the io vector to start copying from
470 * @len: amount of data to copy to buffer from iovec
472 * Returns 0 or -EFAULT.
473 * Note: the iovec is not modified during the copy.
475 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
476 const struct iovec
*from
, int from_offset
,
479 int start
= skb_headlen(skb
);
480 int i
, copy
= start
- offset
;
481 struct sk_buff
*frag_iter
;
487 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
490 if ((len
-= copy
) == 0)
496 /* Copy paged appendix. Hmm... why does this look so complicated? */
497 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
500 WARN_ON(start
> offset
+ len
);
502 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
503 if ((copy
= end
- offset
) > 0) {
506 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
507 struct page
*page
= frag
->page
;
512 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
514 from
, from_offset
, copy
);
527 skb_walk_frags(skb
, frag_iter
) {
530 WARN_ON(start
> offset
+ len
);
532 end
= start
+ frag_iter
->len
;
533 if ((copy
= end
- offset
) > 0) {
536 if (skb_copy_datagram_from_iovec(frag_iter
,
542 if ((len
-= copy
) == 0)
555 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
557 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
558 u8 __user
*to
, int len
,
561 int start
= skb_headlen(skb
);
562 int i
, copy
= start
- offset
;
563 struct sk_buff
*frag_iter
;
571 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
575 if ((len
-= copy
) == 0)
582 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
585 WARN_ON(start
> offset
+ len
);
587 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
588 if ((copy
= end
- offset
) > 0) {
592 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
593 struct page
*page
= frag
->page
;
598 csum2
= csum_and_copy_to_user(vaddr
+
605 *csump
= csum_block_add(*csump
, csum2
, pos
);
615 skb_walk_frags(skb
, frag_iter
) {
618 WARN_ON(start
> offset
+ len
);
620 end
= start
+ frag_iter
->len
;
621 if ((copy
= end
- offset
) > 0) {
625 if (skb_copy_and_csum_datagram(frag_iter
,
630 *csump
= csum_block_add(*csump
, csum2
, pos
);
631 if ((len
-= copy
) == 0)
646 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
650 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
652 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
653 netdev_rx_csum_fault(skb
->dev
);
654 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
658 EXPORT_SYMBOL(__skb_checksum_complete_head
);
660 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
662 return __skb_checksum_complete_head(skb
, skb
->len
);
664 EXPORT_SYMBOL(__skb_checksum_complete
);
667 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
669 * @hlen: hardware length
672 * Caller _must_ check that skb will fit to this iovec.
674 * Returns: 0 - success.
675 * -EINVAL - checksum failure.
676 * -EFAULT - fault during copy. Beware, in this case iovec
679 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
680 int hlen
, struct iovec
*iov
)
683 int chunk
= skb
->len
- hlen
;
688 /* Skip filled elements.
689 * Pretty silly, look at memcpy_toiovec, though 8)
691 while (!iov
->iov_len
)
694 if (iov
->iov_len
< chunk
) {
695 if (__skb_checksum_complete(skb
))
697 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
700 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
701 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
706 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
707 netdev_rx_csum_fault(skb
->dev
);
708 iov
->iov_len
-= chunk
;
709 iov
->iov_base
+= chunk
;
719 * datagram_poll - generic datagram poll
724 * Datagram poll: Again totally generic. This also handles
725 * sequenced packet sockets providing the socket receive queue
726 * is only ever holding data ready to receive.
728 * Note: when you _don't_ use this routine for this protocol,
729 * and you use a different write policy from sock_writeable()
730 * then please supply your own write_space callback.
732 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
735 struct sock
*sk
= sock
->sk
;
738 sock_poll_wait(file
, sk_sleep(sk
), wait
);
741 /* exceptional events? */
742 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
744 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
746 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
750 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
751 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
752 mask
|= POLLIN
| POLLRDNORM
;
754 /* Connection-based need to check for termination and startup */
755 if (connection_based(sk
)) {
756 if (sk
->sk_state
== TCP_CLOSE
)
758 /* connection hasn't started yet? */
759 if (sk
->sk_state
== TCP_SYN_SENT
)
764 if (sock_writeable(sk
))
765 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
767 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
772 EXPORT_SYMBOL(datagram_poll
);
773 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
774 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
775 EXPORT_SYMBOL(skb_recv_datagram
);