[UDP]: Restore missing inDatagrams increments
[deliverable/linux.git] / net / core / datagram.c
CommitLineData
1da177e4
LT
1/*
2 * SUCS NET3:
3 *
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.
11 *
12 * Authors: Alan Cox <alan@redhat.com>. (datagram_poll() from old
13 * udp.c code)
14 *
15 * Fixes:
16 * Alan Cox : NULL return from skb_peek_copy()
17 * understood
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
23 * feasible.
24 * Alan Cox : Fixed write poll of non IP protocol
25 * crash.
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.
33 *
34 */
35
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>
41#include <linux/mm.h>
42#include <linux/interrupt.h>
43#include <linux/errno.h>
44#include <linux/sched.h>
45#include <linux/inet.h>
1da177e4
LT
46#include <linux/netdevice.h>
47#include <linux/rtnetlink.h>
48#include <linux/poll.h>
49#include <linux/highmem.h>
3305b80c 50#include <linux/spinlock.h>
1da177e4
LT
51
52#include <net/protocol.h>
53#include <linux/skbuff.h>
1da177e4 54
c752f073
ACM
55#include <net/checksum.h>
56#include <net/sock.h>
57#include <net/tcp_states.h>
1da177e4
LT
58
59/*
60 * Is a socket 'connection oriented' ?
61 */
62static inline int connection_based(struct sock *sk)
63{
64 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
65}
66
67/*
68 * Wait for a packet..
69 */
70static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
71{
72 int error;
73 DEFINE_WAIT(wait);
74
75 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
76
77 /* Socket errors? */
78 error = sock_error(sk);
79 if (error)
80 goto out_err;
81
82 if (!skb_queue_empty(&sk->sk_receive_queue))
83 goto out;
84
85 /* Socket shut down? */
86 if (sk->sk_shutdown & RCV_SHUTDOWN)
87 goto out_noerr;
88
89 /* Sequenced packets can come disconnected.
90 * If so we report the problem
91 */
92 error = -ENOTCONN;
93 if (connection_based(sk) &&
94 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
95 goto out_err;
96
97 /* handle signals */
98 if (signal_pending(current))
99 goto interrupted;
100
101 error = 0;
102 *timeo_p = schedule_timeout(*timeo_p);
103out:
104 finish_wait(sk->sk_sleep, &wait);
105 return error;
106interrupted:
107 error = sock_intr_errno(*timeo_p);
108out_err:
109 *err = error;
110 goto out;
111out_noerr:
112 *err = 0;
113 error = 1;
114 goto out;
115}
116
117/**
118 * skb_recv_datagram - Receive a datagram skbuff
4dc3b16b
PP
119 * @sk: socket
120 * @flags: MSG_ flags
121 * @noblock: blocking operation?
122 * @err: error code returned
1da177e4
LT
123 *
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.
129 *
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
132 * skb_free_datagram)
133 *
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
139 * * 8) Great win.)
140 * * --ANK (980729)
141 *
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.
145 */
146struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
147 int noblock, int *err)
148{
149 struct sk_buff *skb;
150 long timeo;
151 /*
152 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
153 */
154 int error = sock_error(sk);
155
156 if (error)
157 goto no_packet;
158
159 timeo = sock_rcvtimeo(sk, noblock);
160
161 do {
162 /* Again only user level code calls this function, so nothing
163 * interrupt level will suddenly eat the receive_queue.
164 *
165 * Look at current nfs client by the way...
166 * However, this function was corrent in any case. 8)
167 */
168 if (flags & MSG_PEEK) {
169 unsigned long cpu_flags;
170
171 spin_lock_irqsave(&sk->sk_receive_queue.lock,
172 cpu_flags);
173 skb = skb_peek(&sk->sk_receive_queue);
174 if (skb)
175 atomic_inc(&skb->users);
176 spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
177 cpu_flags);
178 } else
179 skb = skb_dequeue(&sk->sk_receive_queue);
180
181 if (skb)
182 return skb;
183
184 /* User doesn't want to wait */
185 error = -EAGAIN;
186 if (!timeo)
187 goto no_packet;
188
189 } while (!wait_for_packet(sk, err, &timeo));
190
191 return NULL;
192
193no_packet:
194 *err = error;
195 return NULL;
196}
197
198void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
199{
200 kfree_skb(skb);
201}
202
3305b80c
HX
203/**
204 * skb_kill_datagram - Free a datagram skbuff forcibly
205 * @sk: socket
206 * @skb: datagram skbuff
207 * @flags: MSG_ flags
208 *
209 * This function frees a datagram skbuff that was received by
210 * skb_recv_datagram. The flags argument must match the one
211 * used for skb_recv_datagram.
212 *
213 * If the MSG_PEEK flag is set, and the packet is still on the
214 * receive queue of the socket, it will be taken off the queue
215 * before it is freed.
216 *
217 * This function currently only disables BH when acquiring the
218 * sk_receive_queue lock. Therefore it must not be used in a
219 * context where that lock is acquired in an IRQ context.
27ab2568
HX
220 *
221 * It returns 0 if the packet was removed by us.
3305b80c
HX
222 */
223
27ab2568 224int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
3305b80c 225{
27ab2568
HX
226 int err = 0;
227
3305b80c 228 if (flags & MSG_PEEK) {
27ab2568 229 err = -ENOENT;
3305b80c
HX
230 spin_lock_bh(&sk->sk_receive_queue.lock);
231 if (skb == skb_peek(&sk->sk_receive_queue)) {
232 __skb_unlink(skb, &sk->sk_receive_queue);
233 atomic_dec(&skb->users);
27ab2568 234 err = 0;
3305b80c
HX
235 }
236 spin_unlock_bh(&sk->sk_receive_queue.lock);
237 }
238
239 kfree_skb(skb);
27ab2568 240 return err;
3305b80c
HX
241}
242
243EXPORT_SYMBOL(skb_kill_datagram);
244
1da177e4
LT
245/**
246 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
4dc3b16b
PP
247 * @skb: buffer to copy
248 * @offset: offset in the buffer to start copying from
67be2dd1 249 * @to: io vector to copy to
4dc3b16b 250 * @len: amount of data to copy from buffer to iovec
1da177e4
LT
251 *
252 * Note: the iovec is modified during the copy.
253 */
254int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
255 struct iovec *to, int len)
256{
1a028e50
DM
257 int start = skb_headlen(skb);
258 int i, copy = start - offset;
c75d721c 259
b4d9eda0
DM
260 /* Copy header. */
261 if (copy > 0) {
262 if (copy > len)
263 copy = len;
264 if (memcpy_toiovec(to, skb->data + offset, copy))
265 goto fault;
266 if ((len -= copy) == 0)
267 return 0;
268 offset += copy;
269 }
c75d721c 270
b4d9eda0
DM
271 /* Copy paged appendix. Hmm... why does this look so complicated? */
272 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1a028e50 273 int end;
1da177e4 274
1a028e50
DM
275 BUG_TRAP(start <= offset + len);
276
277 end = start + skb_shinfo(skb)->frags[i].size;
b4d9eda0
DM
278 if ((copy = end - offset) > 0) {
279 int err;
280 u8 *vaddr;
281 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
282 struct page *page = frag->page;
1da177e4
LT
283
284 if (copy > len)
285 copy = len;
b4d9eda0 286 vaddr = kmap(page);
1a028e50
DM
287 err = memcpy_toiovec(to, vaddr + frag->page_offset +
288 offset - start, copy);
b4d9eda0 289 kunmap(page);
1da177e4
LT
290 if (err)
291 goto fault;
292 if (!(len -= copy))
293 return 0;
294 offset += copy;
295 }
1a028e50 296 start = end;
1da177e4 297 }
b4d9eda0
DM
298
299 if (skb_shinfo(skb)->frag_list) {
300 struct sk_buff *list = skb_shinfo(skb)->frag_list;
301
302 for (; list; list = list->next) {
1a028e50
DM
303 int end;
304
305 BUG_TRAP(start <= offset + len);
b4d9eda0 306
1a028e50 307 end = start + list->len;
b4d9eda0
DM
308 if ((copy = end - offset) > 0) {
309 if (copy > len)
310 copy = len;
1a028e50
DM
311 if (skb_copy_datagram_iovec(list,
312 offset - start,
313 to, copy))
b4d9eda0
DM
314 goto fault;
315 if ((len -= copy) == 0)
316 return 0;
317 offset += copy;
318 }
1a028e50 319 start = end;
b4d9eda0 320 }
1da177e4 321 }
b4d9eda0
DM
322 if (!len)
323 return 0;
324
1da177e4
LT
325fault:
326 return -EFAULT;
327}
328
329static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
330 u8 __user *to, int len,
5084205f 331 __wsum *csump)
1da177e4 332{
1a028e50 333 int start = skb_headlen(skb);
1da177e4 334 int pos = 0;
1a028e50 335 int i, copy = start - offset;
1da177e4
LT
336
337 /* Copy header. */
338 if (copy > 0) {
339 int err = 0;
340 if (copy > len)
341 copy = len;
342 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
343 *csump, &err);
344 if (err)
345 goto fault;
346 if ((len -= copy) == 0)
347 return 0;
348 offset += copy;
349 to += copy;
350 pos = copy;
351 }
352
353 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1a028e50 354 int end;
1da177e4 355
1a028e50
DM
356 BUG_TRAP(start <= offset + len);
357
358 end = start + skb_shinfo(skb)->frags[i].size;
1da177e4 359 if ((copy = end - offset) > 0) {
5084205f 360 __wsum csum2;
1da177e4
LT
361 int err = 0;
362 u8 *vaddr;
363 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
364 struct page *page = frag->page;
365
366 if (copy > len)
367 copy = len;
368 vaddr = kmap(page);
369 csum2 = csum_and_copy_to_user(vaddr +
1a028e50
DM
370 frag->page_offset +
371 offset - start,
1da177e4
LT
372 to, copy, 0, &err);
373 kunmap(page);
374 if (err)
375 goto fault;
376 *csump = csum_block_add(*csump, csum2, pos);
377 if (!(len -= copy))
378 return 0;
379 offset += copy;
380 to += copy;
381 pos += copy;
382 }
1a028e50 383 start = end;
1da177e4
LT
384 }
385
386 if (skb_shinfo(skb)->frag_list) {
387 struct sk_buff *list = skb_shinfo(skb)->frag_list;
388
389 for (; list; list=list->next) {
1a028e50
DM
390 int end;
391
392 BUG_TRAP(start <= offset + len);
1da177e4 393
1a028e50 394 end = start + list->len;
1da177e4 395 if ((copy = end - offset) > 0) {
5084205f 396 __wsum csum2 = 0;
1da177e4
LT
397 if (copy > len)
398 copy = len;
1a028e50
DM
399 if (skb_copy_and_csum_datagram(list,
400 offset - start,
1da177e4
LT
401 to, copy,
402 &csum2))
403 goto fault;
404 *csump = csum_block_add(*csump, csum2, pos);
405 if ((len -= copy) == 0)
406 return 0;
407 offset += copy;
408 to += copy;
409 pos += copy;
410 }
1a028e50 411 start = end;
1da177e4
LT
412 }
413 }
414 if (!len)
415 return 0;
416
417fault:
418 return -EFAULT;
419}
420
759e5d00 421__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
fb286bb2 422{
d3bc23e7 423 __sum16 sum;
fb286bb2 424
759e5d00 425 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
fb286bb2 426 if (likely(!sum)) {
84fa7933 427 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
fb286bb2
HX
428 netdev_rx_csum_fault(skb->dev);
429 skb->ip_summed = CHECKSUM_UNNECESSARY;
430 }
431 return sum;
432}
759e5d00
HX
433EXPORT_SYMBOL(__skb_checksum_complete_head);
434
435__sum16 __skb_checksum_complete(struct sk_buff *skb)
436{
437 return __skb_checksum_complete_head(skb, skb->len);
438}
fb286bb2
HX
439EXPORT_SYMBOL(__skb_checksum_complete);
440
1da177e4
LT
441/**
442 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
4dc3b16b
PP
443 * @skb: skbuff
444 * @hlen: hardware length
67be2dd1 445 * @iov: io vector
4ec93edb 446 *
1da177e4
LT
447 * Caller _must_ check that skb will fit to this iovec.
448 *
449 * Returns: 0 - success.
450 * -EINVAL - checksum failure.
451 * -EFAULT - fault during copy. Beware, in this case iovec
452 * can be modified!
453 */
fb286bb2 454int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
1da177e4
LT
455 int hlen, struct iovec *iov)
456{
d3bc23e7 457 __wsum csum;
1da177e4
LT
458 int chunk = skb->len - hlen;
459
ef8aef55
HX
460 if (!chunk)
461 return 0;
462
1da177e4
LT
463 /* Skip filled elements.
464 * Pretty silly, look at memcpy_toiovec, though 8)
465 */
466 while (!iov->iov_len)
467 iov++;
468
469 if (iov->iov_len < chunk) {
fb286bb2 470 if (__skb_checksum_complete(skb))
1da177e4
LT
471 goto csum_error;
472 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
473 goto fault;
474 } else {
475 csum = csum_partial(skb->data, hlen, skb->csum);
476 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
477 chunk, &csum))
478 goto fault;
d3bc23e7 479 if (csum_fold(csum))
1da177e4 480 goto csum_error;
84fa7933 481 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
fb286bb2 482 netdev_rx_csum_fault(skb->dev);
1da177e4
LT
483 iov->iov_len -= chunk;
484 iov->iov_base += chunk;
485 }
486 return 0;
487csum_error:
488 return -EINVAL;
489fault:
490 return -EFAULT;
491}
492
493/**
494 * datagram_poll - generic datagram poll
4dc3b16b
PP
495 * @file: file struct
496 * @sock: socket
497 * @wait: poll table
1da177e4
LT
498 *
499 * Datagram poll: Again totally generic. This also handles
500 * sequenced packet sockets providing the socket receive queue
501 * is only ever holding data ready to receive.
502 *
503 * Note: when you _don't_ use this routine for this protocol,
504 * and you use a different write policy from sock_writeable()
505 * then please supply your own write_space callback.
506 */
507unsigned int datagram_poll(struct file *file, struct socket *sock,
508 poll_table *wait)
509{
510 struct sock *sk = sock->sk;
511 unsigned int mask;
512
513 poll_wait(file, sk->sk_sleep, wait);
514 mask = 0;
515
516 /* exceptional events? */
517 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
518 mask |= POLLERR;
f348d70a
DL
519 if (sk->sk_shutdown & RCV_SHUTDOWN)
520 mask |= POLLRDHUP;
1da177e4
LT
521 if (sk->sk_shutdown == SHUTDOWN_MASK)
522 mask |= POLLHUP;
523
524 /* readable? */
525 if (!skb_queue_empty(&sk->sk_receive_queue) ||
526 (sk->sk_shutdown & RCV_SHUTDOWN))
527 mask |= POLLIN | POLLRDNORM;
528
529 /* Connection-based need to check for termination and startup */
530 if (connection_based(sk)) {
531 if (sk->sk_state == TCP_CLOSE)
532 mask |= POLLHUP;
533 /* connection hasn't started yet? */
534 if (sk->sk_state == TCP_SYN_SENT)
535 return mask;
536 }
537
538 /* writable? */
539 if (sock_writeable(sk))
540 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
541 else
542 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
543
544 return mask;
545}
546
547EXPORT_SYMBOL(datagram_poll);
548EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
549EXPORT_SYMBOL(skb_copy_datagram_iovec);
550EXPORT_SYMBOL(skb_free_datagram);
551EXPORT_SYMBOL(skb_recv_datagram);
This page took 0.429946 seconds and 5 git commands to generate.