[PARISC] Add sync required after fdc to enforce insn ordering
[linux-2.6.22.y-op.git] / net / core / datagram.c
blob81987df536eb83f04d28da673806693589582993
1 /*
2 * SUCS NET3:
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
13 * udp.c code)
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.
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>
46 #include <linux/netdevice.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/poll.h>
49 #include <linux/highmem.h>
51 #include <net/protocol.h>
52 #include <linux/skbuff.h>
54 #include <net/checksum.h>
55 #include <net/sock.h>
56 #include <net/tcp_states.h>
59 * Is a socket 'connection oriented' ?
61 static inline int connection_based(struct sock *sk)
63 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
67 * Wait for a packet..
69 static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
71 int error;
72 DEFINE_WAIT(wait);
74 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
76 /* Socket errors? */
77 error = sock_error(sk);
78 if (error)
79 goto out_err;
81 if (!skb_queue_empty(&sk->sk_receive_queue))
82 goto out;
84 /* Socket shut down? */
85 if (sk->sk_shutdown & RCV_SHUTDOWN)
86 goto out_noerr;
88 /* Sequenced packets can come disconnected.
89 * If so we report the problem
91 error = -ENOTCONN;
92 if (connection_based(sk) &&
93 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
94 goto out_err;
96 /* handle signals */
97 if (signal_pending(current))
98 goto interrupted;
100 error = 0;
101 *timeo_p = schedule_timeout(*timeo_p);
102 out:
103 finish_wait(sk->sk_sleep, &wait);
104 return error;
105 interrupted:
106 error = sock_intr_errno(*timeo_p);
107 out_err:
108 *err = error;
109 goto out;
110 out_noerr:
111 *err = 0;
112 error = 1;
113 goto out;
117 * skb_recv_datagram - Receive a datagram skbuff
118 * @sk: socket
119 * @flags: MSG_ flags
120 * @noblock: blocking operation?
121 * @err: error code returned
123 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
124 * and possible races. This replaces identical code in packet, raw and
125 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
126 * the long standing peek and read race for datagram sockets. If you
127 * alter this routine remember it must be re-entrant.
129 * This function will lock the socket if a skb is returned, so the caller
130 * needs to unlock the socket in that case (usually by calling
131 * skb_free_datagram)
133 * * It does not lock socket since today. This function is
134 * * free of race conditions. This measure should/can improve
135 * * significantly datagram socket latencies at high loads,
136 * * when data copying to user space takes lots of time.
137 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
138 * * 8) Great win.)
139 * * --ANK (980729)
141 * The order of the tests when we find no data waiting are specified
142 * quite explicitly by POSIX 1003.1g, don't change them without having
143 * the standard around please.
145 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
146 int noblock, int *err)
148 struct sk_buff *skb;
149 long timeo;
151 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
153 int error = sock_error(sk);
155 if (error)
156 goto no_packet;
158 timeo = sock_rcvtimeo(sk, noblock);
160 do {
161 /* Again only user level code calls this function, so nothing
162 * interrupt level will suddenly eat the receive_queue.
164 * Look at current nfs client by the way...
165 * However, this function was corrent in any case. 8)
167 if (flags & MSG_PEEK) {
168 unsigned long cpu_flags;
170 spin_lock_irqsave(&sk->sk_receive_queue.lock,
171 cpu_flags);
172 skb = skb_peek(&sk->sk_receive_queue);
173 if (skb)
174 atomic_inc(&skb->users);
175 spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
176 cpu_flags);
177 } else
178 skb = skb_dequeue(&sk->sk_receive_queue);
180 if (skb)
181 return skb;
183 /* User doesn't want to wait */
184 error = -EAGAIN;
185 if (!timeo)
186 goto no_packet;
188 } while (!wait_for_packet(sk, err, &timeo));
190 return NULL;
192 no_packet:
193 *err = error;
194 return NULL;
197 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
199 kfree_skb(skb);
203 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
204 * @skb: buffer to copy
205 * @offset: offset in the buffer to start copying from
206 * @to: io vector to copy to
207 * @len: amount of data to copy from buffer to iovec
209 * Note: the iovec is modified during the copy.
211 int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
212 struct iovec *to, int len)
214 int i, err, fraglen, end = 0;
215 struct sk_buff *next = skb_shinfo(skb)->frag_list;
216 next_skb:
217 fraglen = skb_headlen(skb);
218 i = -1;
220 while (1) {
221 int start = end;
223 if ((end += fraglen) > offset) {
224 int copy = end - offset, o = offset - start;
226 if (copy > len)
227 copy = len;
228 if (i == -1)
229 err = memcpy_toiovec(to, skb->data + o, copy);
230 else {
231 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
232 struct page *page = frag->page;
233 void *p = kmap(page) + frag->page_offset + o;
234 err = memcpy_toiovec(to, p, copy);
235 kunmap(page);
237 if (err)
238 goto fault;
239 if (!(len -= copy))
240 return 0;
241 offset += copy;
243 if (++i >= skb_shinfo(skb)->nr_frags)
244 break;
245 fraglen = skb_shinfo(skb)->frags[i].size;
247 if (next) {
248 skb = next;
249 BUG_ON(skb_shinfo(skb)->frag_list);
250 next = skb->next;
251 goto next_skb;
253 fault:
254 return -EFAULT;
257 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
258 u8 __user *to, int len,
259 unsigned int *csump)
261 int start = skb_headlen(skb);
262 int pos = 0;
263 int i, copy = start - offset;
265 /* Copy header. */
266 if (copy > 0) {
267 int err = 0;
268 if (copy > len)
269 copy = len;
270 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
271 *csump, &err);
272 if (err)
273 goto fault;
274 if ((len -= copy) == 0)
275 return 0;
276 offset += copy;
277 to += copy;
278 pos = copy;
281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
282 int end;
284 BUG_TRAP(start <= offset + len);
286 end = start + skb_shinfo(skb)->frags[i].size;
287 if ((copy = end - offset) > 0) {
288 unsigned int csum2;
289 int err = 0;
290 u8 *vaddr;
291 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
292 struct page *page = frag->page;
294 if (copy > len)
295 copy = len;
296 vaddr = kmap(page);
297 csum2 = csum_and_copy_to_user(vaddr +
298 frag->page_offset +
299 offset - start,
300 to, copy, 0, &err);
301 kunmap(page);
302 if (err)
303 goto fault;
304 *csump = csum_block_add(*csump, csum2, pos);
305 if (!(len -= copy))
306 return 0;
307 offset += copy;
308 to += copy;
309 pos += copy;
311 start = end;
314 if (skb_shinfo(skb)->frag_list) {
315 struct sk_buff *list = skb_shinfo(skb)->frag_list;
317 for (; list; list=list->next) {
318 int end;
320 BUG_TRAP(start <= offset + len);
322 end = start + list->len;
323 if ((copy = end - offset) > 0) {
324 unsigned int csum2 = 0;
325 if (copy > len)
326 copy = len;
327 if (skb_copy_and_csum_datagram(list,
328 offset - start,
329 to, copy,
330 &csum2))
331 goto fault;
332 *csump = csum_block_add(*csump, csum2, pos);
333 if ((len -= copy) == 0)
334 return 0;
335 offset += copy;
336 to += copy;
337 pos += copy;
339 start = end;
342 if (!len)
343 return 0;
345 fault:
346 return -EFAULT;
350 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
351 * @skb: skbuff
352 * @hlen: hardware length
353 * @iov: io vector
355 * Caller _must_ check that skb will fit to this iovec.
357 * Returns: 0 - success.
358 * -EINVAL - checksum failure.
359 * -EFAULT - fault during copy. Beware, in this case iovec
360 * can be modified!
362 int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb,
363 int hlen, struct iovec *iov)
365 unsigned int csum;
366 int chunk = skb->len - hlen;
368 /* Skip filled elements.
369 * Pretty silly, look at memcpy_toiovec, though 8)
371 while (!iov->iov_len)
372 iov++;
374 if (iov->iov_len < chunk) {
375 if ((unsigned short)csum_fold(skb_checksum(skb, 0, chunk + hlen,
376 skb->csum)))
377 goto csum_error;
378 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
379 goto fault;
380 } else {
381 csum = csum_partial(skb->data, hlen, skb->csum);
382 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
383 chunk, &csum))
384 goto fault;
385 if ((unsigned short)csum_fold(csum))
386 goto csum_error;
387 iov->iov_len -= chunk;
388 iov->iov_base += chunk;
390 return 0;
391 csum_error:
392 return -EINVAL;
393 fault:
394 return -EFAULT;
398 * datagram_poll - generic datagram poll
399 * @file: file struct
400 * @sock: socket
401 * @wait: poll table
403 * Datagram poll: Again totally generic. This also handles
404 * sequenced packet sockets providing the socket receive queue
405 * is only ever holding data ready to receive.
407 * Note: when you _don't_ use this routine for this protocol,
408 * and you use a different write policy from sock_writeable()
409 * then please supply your own write_space callback.
411 unsigned int datagram_poll(struct file *file, struct socket *sock,
412 poll_table *wait)
414 struct sock *sk = sock->sk;
415 unsigned int mask;
417 poll_wait(file, sk->sk_sleep, wait);
418 mask = 0;
420 /* exceptional events? */
421 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
422 mask |= POLLERR;
423 if (sk->sk_shutdown == SHUTDOWN_MASK)
424 mask |= POLLHUP;
426 /* readable? */
427 if (!skb_queue_empty(&sk->sk_receive_queue) ||
428 (sk->sk_shutdown & RCV_SHUTDOWN))
429 mask |= POLLIN | POLLRDNORM;
431 /* Connection-based need to check for termination and startup */
432 if (connection_based(sk)) {
433 if (sk->sk_state == TCP_CLOSE)
434 mask |= POLLHUP;
435 /* connection hasn't started yet? */
436 if (sk->sk_state == TCP_SYN_SENT)
437 return mask;
440 /* writable? */
441 if (sock_writeable(sk))
442 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
443 else
444 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
446 return mask;
449 EXPORT_SYMBOL(datagram_poll);
450 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
451 EXPORT_SYMBOL(skb_copy_datagram_iovec);
452 EXPORT_SYMBOL(skb_free_datagram);
453 EXPORT_SYMBOL(skb_recv_datagram);