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>
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 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
);
83 static int wait_for_packet(struct sock
*sk
, int *err
, long *timeo_p
)
86 DEFINE_WAIT_FUNC(wait
, receiver_wake_function
);
88 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
91 error
= sock_error(sk
);
95 if (!skb_queue_empty(&sk
->sk_receive_queue
))
98 /* Socket shut down? */
99 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
102 /* Sequenced packets can come disconnected.
103 * If so we report the problem
106 if (connection_based(sk
) &&
107 !(sk
->sk_state
== TCP_ESTABLISHED
|| sk
->sk_state
== TCP_LISTEN
))
111 if (signal_pending(current
))
115 *timeo_p
= schedule_timeout(*timeo_p
);
117 finish_wait(sk
->sk_sleep
, &wait
);
120 error
= sock_intr_errno(*timeo_p
);
131 * __skb_recv_datagram - Receive a datagram skbuff
134 * @peeked: returns non-zero if this packet has been seen before
135 * @err: error code returned
137 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
138 * and possible races. This replaces identical code in packet, raw and
139 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
140 * the long standing peek and read race for datagram sockets. If you
141 * alter this routine remember it must be re-entrant.
143 * This function will lock the socket if a skb is returned, so the caller
144 * needs to unlock the socket in that case (usually by calling
147 * * It does not lock socket since today. This function is
148 * * free of race conditions. This measure should/can improve
149 * * significantly datagram socket latencies at high loads,
150 * * when data copying to user space takes lots of time.
151 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
155 * The order of the tests when we find no data waiting are specified
156 * quite explicitly by POSIX 1003.1g, don't change them without having
157 * the standard around please.
159 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned flags
,
160 int *peeked
, int *err
)
165 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
167 int error
= sock_error(sk
);
172 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
175 /* Again only user level code calls this function, so nothing
176 * interrupt level will suddenly eat the receive_queue.
178 * Look at current nfs client by the way...
179 * However, this function was corrent in any case. 8)
181 unsigned long cpu_flags
;
183 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, cpu_flags
);
184 skb
= skb_peek(&sk
->sk_receive_queue
);
186 *peeked
= skb
->peeked
;
187 if (flags
& MSG_PEEK
) {
189 atomic_inc(&skb
->users
);
191 __skb_unlink(skb
, &sk
->sk_receive_queue
);
193 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, cpu_flags
);
198 /* User doesn't want to wait */
203 } while (!wait_for_packet(sk
, err
, &timeo
));
211 EXPORT_SYMBOL(__skb_recv_datagram
);
213 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
214 int noblock
, int *err
)
218 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
222 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
225 sk_mem_reclaim_partial(sk
);
227 EXPORT_SYMBOL(skb_free_datagram
);
229 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
232 skb_free_datagram(sk
, skb
);
235 EXPORT_SYMBOL(skb_free_datagram_locked
);
238 * skb_kill_datagram - Free a datagram skbuff forcibly
240 * @skb: datagram skbuff
243 * This function frees a datagram skbuff that was received by
244 * skb_recv_datagram. The flags argument must match the one
245 * used for skb_recv_datagram.
247 * If the MSG_PEEK flag is set, and the packet is still on the
248 * receive queue of the socket, it will be taken off the queue
249 * before it is freed.
251 * This function currently only disables BH when acquiring the
252 * sk_receive_queue lock. Therefore it must not be used in a
253 * context where that lock is acquired in an IRQ context.
255 * It returns 0 if the packet was removed by us.
258 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
262 if (flags
& MSG_PEEK
) {
264 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
265 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
266 __skb_unlink(skb
, &sk
->sk_receive_queue
);
267 atomic_dec(&skb
->users
);
270 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
274 atomic_inc(&sk
->sk_drops
);
275 sk_mem_reclaim_partial(sk
);
280 EXPORT_SYMBOL(skb_kill_datagram
);
283 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
284 * @skb: buffer to copy
285 * @offset: offset in the buffer to start copying from
286 * @to: io vector to copy to
287 * @len: amount of data to copy from buffer to iovec
289 * Note: the iovec is modified during the copy.
291 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
292 struct iovec
*to
, int len
)
294 int start
= skb_headlen(skb
);
295 int i
, copy
= start
- offset
;
296 struct sk_buff
*frag_iter
;
298 trace_skb_copy_datagram_iovec(skb
, len
);
304 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
306 if ((len
-= copy
) == 0)
311 /* Copy paged appendix. Hmm... why does this look so complicated? */
312 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
315 WARN_ON(start
> offset
+ len
);
317 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
318 if ((copy
= end
- offset
) > 0) {
321 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
322 struct page
*page
= frag
->page
;
327 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
328 offset
- start
, copy
);
339 skb_walk_frags(skb
, frag_iter
) {
342 WARN_ON(start
> offset
+ len
);
344 end
= start
+ frag_iter
->len
;
345 if ((copy
= end
- offset
) > 0) {
348 if (skb_copy_datagram_iovec(frag_iter
,
352 if ((len
-= copy
) == 0)
366 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
367 * @skb: buffer to copy
368 * @offset: offset in the buffer to start copying from
369 * @to: io vector to copy to
370 * @to_offset: offset in the io vector to start copying to
371 * @len: amount of data to copy from buffer to iovec
373 * Returns 0 or -EFAULT.
374 * Note: the iovec is not modified during the copy.
376 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
377 const struct iovec
*to
, int to_offset
,
380 int start
= skb_headlen(skb
);
381 int i
, copy
= start
- offset
;
382 struct sk_buff
*frag_iter
;
388 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
390 if ((len
-= copy
) == 0)
396 /* Copy paged appendix. Hmm... why does this look so complicated? */
397 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
400 WARN_ON(start
> offset
+ len
);
402 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
403 if ((copy
= end
- offset
) > 0) {
406 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
407 struct page
*page
= frag
->page
;
412 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
413 offset
- start
, to_offset
, copy
);
425 skb_walk_frags(skb
, frag_iter
) {
428 WARN_ON(start
> offset
+ len
);
430 end
= start
+ frag_iter
->len
;
431 if ((copy
= end
- offset
) > 0) {
434 if (skb_copy_datagram_const_iovec(frag_iter
,
439 if ((len
-= copy
) == 0)
452 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
455 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
456 * @skb: buffer to copy
457 * @offset: offset in the buffer to start copying to
458 * @from: io vector to copy to
459 * @from_offset: offset in the io vector to start copying from
460 * @len: amount of data to copy to buffer from iovec
462 * Returns 0 or -EFAULT.
463 * Note: the iovec is not modified during the copy.
465 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
466 const struct iovec
*from
, int from_offset
,
469 int start
= skb_headlen(skb
);
470 int i
, copy
= start
- offset
;
471 struct sk_buff
*frag_iter
;
477 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
480 if ((len
-= copy
) == 0)
486 /* Copy paged appendix. Hmm... why does this look so complicated? */
487 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
490 WARN_ON(start
> offset
+ len
);
492 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
493 if ((copy
= end
- offset
) > 0) {
496 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
497 struct page
*page
= frag
->page
;
502 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
504 from
, from_offset
, copy
);
517 skb_walk_frags(skb
, frag_iter
) {
520 WARN_ON(start
> offset
+ len
);
522 end
= start
+ frag_iter
->len
;
523 if ((copy
= end
- offset
) > 0) {
526 if (skb_copy_datagram_from_iovec(frag_iter
,
532 if ((len
-= copy
) == 0)
545 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
547 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
548 u8 __user
*to
, int len
,
551 int start
= skb_headlen(skb
);
552 int i
, copy
= start
- offset
;
553 struct sk_buff
*frag_iter
;
561 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
565 if ((len
-= copy
) == 0)
572 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
575 WARN_ON(start
> offset
+ len
);
577 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
578 if ((copy
= end
- offset
) > 0) {
582 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
583 struct page
*page
= frag
->page
;
588 csum2
= csum_and_copy_to_user(vaddr
+
595 *csump
= csum_block_add(*csump
, csum2
, pos
);
605 skb_walk_frags(skb
, frag_iter
) {
608 WARN_ON(start
> offset
+ len
);
610 end
= start
+ frag_iter
->len
;
611 if ((copy
= end
- offset
) > 0) {
615 if (skb_copy_and_csum_datagram(frag_iter
,
620 *csump
= csum_block_add(*csump
, csum2
, pos
);
621 if ((len
-= copy
) == 0)
636 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
640 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
642 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
643 netdev_rx_csum_fault(skb
->dev
);
644 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
648 EXPORT_SYMBOL(__skb_checksum_complete_head
);
650 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
652 return __skb_checksum_complete_head(skb
, skb
->len
);
654 EXPORT_SYMBOL(__skb_checksum_complete
);
657 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
659 * @hlen: hardware length
662 * Caller _must_ check that skb will fit to this iovec.
664 * Returns: 0 - success.
665 * -EINVAL - checksum failure.
666 * -EFAULT - fault during copy. Beware, in this case iovec
669 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
670 int hlen
, struct iovec
*iov
)
673 int chunk
= skb
->len
- hlen
;
678 /* Skip filled elements.
679 * Pretty silly, look at memcpy_toiovec, though 8)
681 while (!iov
->iov_len
)
684 if (iov
->iov_len
< chunk
) {
685 if (__skb_checksum_complete(skb
))
687 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
690 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
691 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
696 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
697 netdev_rx_csum_fault(skb
->dev
);
698 iov
->iov_len
-= chunk
;
699 iov
->iov_base
+= chunk
;
709 * datagram_poll - generic datagram poll
714 * Datagram poll: Again totally generic. This also handles
715 * sequenced packet sockets providing the socket receive queue
716 * is only ever holding data ready to receive.
718 * Note: when you _don't_ use this routine for this protocol,
719 * and you use a different write policy from sock_writeable()
720 * then please supply your own write_space callback.
722 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
725 struct sock
*sk
= sock
->sk
;
728 sock_poll_wait(file
, sk
->sk_sleep
, wait
);
731 /* exceptional events? */
732 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
734 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
736 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
740 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
741 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
742 mask
|= POLLIN
| POLLRDNORM
;
744 /* Connection-based need to check for termination and startup */
745 if (connection_based(sk
)) {
746 if (sk
->sk_state
== TCP_CLOSE
)
748 /* connection hasn't started yet? */
749 if (sk
->sk_state
== TCP_SYN_SENT
)
754 if (sock_writeable(sk
))
755 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
757 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
762 EXPORT_SYMBOL(datagram_poll
);
763 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
764 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
765 EXPORT_SYMBOL(skb_recv_datagram
);