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@redhat.com>. (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>
60 * Is a socket 'connection oriented' ?
62 static inline int connection_based(struct sock
*sk
)
64 return sk
->sk_type
== SOCK_SEQPACKET
|| sk
->sk_type
== SOCK_STREAM
;
70 static int wait_for_packet(struct sock
*sk
, int *err
, long *timeo_p
)
75 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
, TASK_INTERRUPTIBLE
);
78 error
= sock_error(sk
);
82 if (!skb_queue_empty(&sk
->sk_receive_queue
))
85 /* Socket shut down? */
86 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
89 /* Sequenced packets can come disconnected.
90 * If so we report the problem
93 if (connection_based(sk
) &&
94 !(sk
->sk_state
== TCP_ESTABLISHED
|| sk
->sk_state
== TCP_LISTEN
))
98 if (signal_pending(current
))
102 *timeo_p
= schedule_timeout(*timeo_p
);
104 finish_wait(sk
->sk_sleep
, &wait
);
107 error
= sock_intr_errno(*timeo_p
);
118 * __skb_recv_datagram - Receive a datagram skbuff
121 * @peeked: returns non-zero if this packet has been seen before
122 * @err: error code returned
124 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
125 * and possible races. This replaces identical code in packet, raw and
126 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
127 * the long standing peek and read race for datagram sockets. If you
128 * alter this routine remember it must be re-entrant.
130 * This function will lock the socket if a skb is returned, so the caller
131 * needs to unlock the socket in that case (usually by calling
134 * * It does not lock socket since today. This function is
135 * * free of race conditions. This measure should/can improve
136 * * significantly datagram socket latencies at high loads,
137 * * when data copying to user space takes lots of time.
138 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
142 * The order of the tests when we find no data waiting are specified
143 * quite explicitly by POSIX 1003.1g, don't change them without having
144 * the standard around please.
146 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned flags
,
147 int *peeked
, int *err
)
152 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
154 int error
= sock_error(sk
);
159 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
162 /* Again only user level code calls this function, so nothing
163 * interrupt level will suddenly eat the receive_queue.
165 * Look at current nfs client by the way...
166 * However, this function was corrent in any case. 8)
168 unsigned long cpu_flags
;
170 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, cpu_flags
);
171 skb
= skb_peek(&sk
->sk_receive_queue
);
173 *peeked
= skb
->peeked
;
174 if (flags
& MSG_PEEK
) {
176 atomic_inc(&skb
->users
);
178 __skb_unlink(skb
, &sk
->sk_receive_queue
);
180 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, cpu_flags
);
185 /* User doesn't want to wait */
190 } while (!wait_for_packet(sk
, err
, &timeo
));
198 EXPORT_SYMBOL(__skb_recv_datagram
);
200 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
201 int noblock
, int *err
)
205 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
209 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
216 * skb_kill_datagram - Free a datagram skbuff forcibly
218 * @skb: datagram skbuff
221 * This function frees a datagram skbuff that was received by
222 * skb_recv_datagram. The flags argument must match the one
223 * used for skb_recv_datagram.
225 * If the MSG_PEEK flag is set, and the packet is still on the
226 * receive queue of the socket, it will be taken off the queue
227 * before it is freed.
229 * This function currently only disables BH when acquiring the
230 * sk_receive_queue lock. Therefore it must not be used in a
231 * context where that lock is acquired in an IRQ context.
233 * It returns 0 if the packet was removed by us.
236 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
240 if (flags
& MSG_PEEK
) {
242 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
243 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
244 __skb_unlink(skb
, &sk
->sk_receive_queue
);
245 atomic_dec(&skb
->users
);
248 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
256 EXPORT_SYMBOL(skb_kill_datagram
);
259 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
260 * @skb: buffer to copy
261 * @offset: offset in the buffer to start copying from
262 * @to: io vector to copy to
263 * @len: amount of data to copy from buffer to iovec
265 * Note: the iovec is modified during the copy.
267 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
268 struct iovec
*to
, int len
)
270 int start
= skb_headlen(skb
);
271 int i
, copy
= start
- offset
;
277 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
279 if ((len
-= copy
) == 0)
284 /* Copy paged appendix. Hmm... why does this look so complicated? */
285 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
288 WARN_ON(start
> offset
+ len
);
290 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
291 if ((copy
= end
- offset
) > 0) {
294 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
295 struct page
*page
= frag
->page
;
300 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
301 offset
- start
, copy
);
312 if (skb_shinfo(skb
)->frag_list
) {
313 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
315 for (; list
; list
= list
->next
) {
318 WARN_ON(start
> offset
+ len
);
320 end
= start
+ list
->len
;
321 if ((copy
= end
- offset
) > 0) {
324 if (skb_copy_datagram_iovec(list
,
328 if ((len
-= copy
) == 0)
343 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
344 * @skb: buffer to copy
345 * @offset: offset in the buffer to start copying to
346 * @from: io vector to copy to
347 * @len: amount of data to copy to buffer from iovec
349 * Returns 0 or -EFAULT.
350 * Note: the iovec is modified during the copy.
352 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
353 struct iovec
*from
, int len
)
355 int start
= skb_headlen(skb
);
356 int i
, copy
= start
- offset
;
362 if (memcpy_fromiovec(skb
->data
+ offset
, from
, copy
))
364 if ((len
-= copy
) == 0)
369 /* Copy paged appendix. Hmm... why does this look so complicated? */
370 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
373 WARN_ON(start
> offset
+ len
);
375 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
376 if ((copy
= end
- offset
) > 0) {
379 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
380 struct page
*page
= frag
->page
;
385 err
= memcpy_fromiovec(vaddr
+ frag
->page_offset
+
386 offset
- start
, from
, copy
);
398 if (skb_shinfo(skb
)->frag_list
) {
399 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
401 for (; list
; list
= list
->next
) {
404 WARN_ON(start
> offset
+ len
);
406 end
= start
+ list
->len
;
407 if ((copy
= end
- offset
) > 0) {
410 if (skb_copy_datagram_from_iovec(list
,
414 if ((len
-= copy
) == 0)
427 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
429 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
430 u8 __user
*to
, int len
,
433 int start
= skb_headlen(skb
);
435 int i
, copy
= start
- offset
;
442 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
446 if ((len
-= copy
) == 0)
453 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
456 WARN_ON(start
> offset
+ len
);
458 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
459 if ((copy
= end
- offset
) > 0) {
463 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
464 struct page
*page
= frag
->page
;
469 csum2
= csum_and_copy_to_user(vaddr
+
476 *csump
= csum_block_add(*csump
, csum2
, pos
);
486 if (skb_shinfo(skb
)->frag_list
) {
487 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
489 for (; list
; list
=list
->next
) {
492 WARN_ON(start
> offset
+ len
);
494 end
= start
+ list
->len
;
495 if ((copy
= end
- offset
) > 0) {
499 if (skb_copy_and_csum_datagram(list
,
504 *csump
= csum_block_add(*csump
, csum2
, pos
);
505 if ((len
-= copy
) == 0)
521 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
525 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
527 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
528 netdev_rx_csum_fault(skb
->dev
);
529 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
533 EXPORT_SYMBOL(__skb_checksum_complete_head
);
535 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
537 return __skb_checksum_complete_head(skb
, skb
->len
);
539 EXPORT_SYMBOL(__skb_checksum_complete
);
542 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
544 * @hlen: hardware length
547 * Caller _must_ check that skb will fit to this iovec.
549 * Returns: 0 - success.
550 * -EINVAL - checksum failure.
551 * -EFAULT - fault during copy. Beware, in this case iovec
554 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
555 int hlen
, struct iovec
*iov
)
558 int chunk
= skb
->len
- hlen
;
563 /* Skip filled elements.
564 * Pretty silly, look at memcpy_toiovec, though 8)
566 while (!iov
->iov_len
)
569 if (iov
->iov_len
< chunk
) {
570 if (__skb_checksum_complete(skb
))
572 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
575 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
576 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
581 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
582 netdev_rx_csum_fault(skb
->dev
);
583 iov
->iov_len
-= chunk
;
584 iov
->iov_base
+= chunk
;
594 * datagram_poll - generic datagram poll
599 * Datagram poll: Again totally generic. This also handles
600 * sequenced packet sockets providing the socket receive queue
601 * is only ever holding data ready to receive.
603 * Note: when you _don't_ use this routine for this protocol,
604 * and you use a different write policy from sock_writeable()
605 * then please supply your own write_space callback.
607 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
610 struct sock
*sk
= sock
->sk
;
613 poll_wait(file
, sk
->sk_sleep
, wait
);
616 /* exceptional events? */
617 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
619 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
621 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
625 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
626 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
627 mask
|= POLLIN
| POLLRDNORM
;
629 /* Connection-based need to check for termination and startup */
630 if (connection_based(sk
)) {
631 if (sk
->sk_state
== TCP_CLOSE
)
633 /* connection hasn't started yet? */
634 if (sk
->sk_state
== TCP_SYN_SENT
)
639 if (sock_writeable(sk
))
640 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
642 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
647 EXPORT_SYMBOL(datagram_poll
);
648 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
649 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
650 EXPORT_SYMBOL(skb_free_datagram
);
651 EXPORT_SYMBOL(skb_recv_datagram
);