]>
Commit | Line | Data |
---|---|---|
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> | |
46 | #include <linux/tcp.h> | |
47 | #include <linux/netdevice.h> | |
48 | #include <linux/rtnetlink.h> | |
49 | #include <linux/poll.h> | |
50 | #include <linux/highmem.h> | |
51 | ||
52 | #include <net/protocol.h> | |
53 | #include <linux/skbuff.h> | |
54 | #include <net/sock.h> | |
55 | #include <net/checksum.h> | |
56 | ||
57 | ||
58 | /* | |
59 | * Is a socket 'connection oriented' ? | |
60 | */ | |
61 | static inline int connection_based(struct sock *sk) | |
62 | { | |
63 | return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Wait for a packet.. | |
68 | */ | |
69 | static int wait_for_packet(struct sock *sk, int *err, long *timeo_p) | |
70 | { | |
71 | int error; | |
72 | DEFINE_WAIT(wait); | |
73 | ||
74 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); | |
75 | ||
76 | /* Socket errors? */ | |
77 | error = sock_error(sk); | |
78 | if (error) | |
79 | goto out_err; | |
80 | ||
81 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
82 | goto out; | |
83 | ||
84 | /* Socket shut down? */ | |
85 | if (sk->sk_shutdown & RCV_SHUTDOWN) | |
86 | goto out_noerr; | |
87 | ||
88 | /* Sequenced packets can come disconnected. | |
89 | * If so we report the problem | |
90 | */ | |
91 | error = -ENOTCONN; | |
92 | if (connection_based(sk) && | |
93 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) | |
94 | goto out_err; | |
95 | ||
96 | /* handle signals */ | |
97 | if (signal_pending(current)) | |
98 | goto interrupted; | |
99 | ||
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; | |
114 | } | |
115 | ||
116 | /** | |
117 | * skb_recv_datagram - Receive a datagram skbuff | |
4dc3b16b PP |
118 | * @sk: socket |
119 | * @flags: MSG_ flags | |
120 | * @noblock: blocking operation? | |
121 | * @err: error code returned | |
1da177e4 LT |
122 | * |
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. | |
128 | * | |
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) | |
132 | * | |
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) | |
140 | * | |
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. | |
144 | */ | |
145 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, | |
146 | int noblock, int *err) | |
147 | { | |
148 | struct sk_buff *skb; | |
149 | long timeo; | |
150 | /* | |
151 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() | |
152 | */ | |
153 | int error = sock_error(sk); | |
154 | ||
155 | if (error) | |
156 | goto no_packet; | |
157 | ||
158 | timeo = sock_rcvtimeo(sk, noblock); | |
159 | ||
160 | do { | |
161 | /* Again only user level code calls this function, so nothing | |
162 | * interrupt level will suddenly eat the receive_queue. | |
163 | * | |
164 | * Look at current nfs client by the way... | |
165 | * However, this function was corrent in any case. 8) | |
166 | */ | |
167 | if (flags & MSG_PEEK) { | |
168 | unsigned long cpu_flags; | |
169 | ||
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); | |
179 | ||
180 | if (skb) | |
181 | return skb; | |
182 | ||
183 | /* User doesn't want to wait */ | |
184 | error = -EAGAIN; | |
185 | if (!timeo) | |
186 | goto no_packet; | |
187 | ||
188 | } while (!wait_for_packet(sk, err, &timeo)); | |
189 | ||
190 | return NULL; | |
191 | ||
192 | no_packet: | |
193 | *err = error; | |
194 | return NULL; | |
195 | } | |
196 | ||
197 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) | |
198 | { | |
199 | kfree_skb(skb); | |
200 | } | |
201 | ||
202 | /** | |
203 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. | |
4dc3b16b PP |
204 | * @skb: buffer to copy |
205 | * @offset: offset in the buffer to start copying from | |
67be2dd1 | 206 | * @to: io vector to copy to |
4dc3b16b | 207 | * @len: amount of data to copy from buffer to iovec |
1da177e4 LT |
208 | * |
209 | * Note: the iovec is modified during the copy. | |
210 | */ | |
211 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, | |
212 | struct iovec *to, int len) | |
213 | { | |
214 | int start = skb_headlen(skb); | |
215 | int i, copy = start - offset; | |
216 | ||
217 | /* Copy header. */ | |
218 | if (copy > 0) { | |
219 | if (copy > len) | |
220 | copy = len; | |
221 | if (memcpy_toiovec(to, skb->data + offset, copy)) | |
222 | goto fault; | |
223 | if ((len -= copy) == 0) | |
224 | return 0; | |
225 | offset += copy; | |
226 | } | |
227 | ||
228 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
229 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
230 | int end; | |
231 | ||
232 | BUG_TRAP(start <= offset + len); | |
233 | ||
234 | end = start + skb_shinfo(skb)->frags[i].size; | |
235 | if ((copy = end - offset) > 0) { | |
236 | int err; | |
237 | u8 *vaddr; | |
238 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
239 | struct page *page = frag->page; | |
240 | ||
241 | if (copy > len) | |
242 | copy = len; | |
243 | vaddr = kmap(page); | |
244 | err = memcpy_toiovec(to, vaddr + frag->page_offset + | |
245 | offset - start, copy); | |
246 | kunmap(page); | |
247 | if (err) | |
248 | goto fault; | |
249 | if (!(len -= copy)) | |
250 | return 0; | |
251 | offset += copy; | |
252 | } | |
253 | start = end; | |
254 | } | |
255 | ||
256 | if (skb_shinfo(skb)->frag_list) { | |
257 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
258 | ||
259 | for (; list; list = list->next) { | |
260 | int end; | |
261 | ||
262 | BUG_TRAP(start <= offset + len); | |
263 | ||
264 | end = start + list->len; | |
265 | if ((copy = end - offset) > 0) { | |
266 | if (copy > len) | |
267 | copy = len; | |
268 | if (skb_copy_datagram_iovec(list, | |
269 | offset - start, | |
270 | to, copy)) | |
271 | goto fault; | |
272 | if ((len -= copy) == 0) | |
273 | return 0; | |
274 | offset += copy; | |
275 | } | |
276 | start = end; | |
277 | } | |
278 | } | |
279 | if (!len) | |
280 | return 0; | |
281 | ||
282 | fault: | |
283 | return -EFAULT; | |
284 | } | |
285 | ||
286 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, | |
287 | u8 __user *to, int len, | |
288 | unsigned int *csump) | |
289 | { | |
290 | int start = skb_headlen(skb); | |
291 | int pos = 0; | |
292 | int i, copy = start - offset; | |
293 | ||
294 | /* Copy header. */ | |
295 | if (copy > 0) { | |
296 | int err = 0; | |
297 | if (copy > len) | |
298 | copy = len; | |
299 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, | |
300 | *csump, &err); | |
301 | if (err) | |
302 | goto fault; | |
303 | if ((len -= copy) == 0) | |
304 | return 0; | |
305 | offset += copy; | |
306 | to += copy; | |
307 | pos = copy; | |
308 | } | |
309 | ||
310 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
311 | int end; | |
312 | ||
313 | BUG_TRAP(start <= offset + len); | |
314 | ||
315 | end = start + skb_shinfo(skb)->frags[i].size; | |
316 | if ((copy = end - offset) > 0) { | |
317 | unsigned int csum2; | |
318 | int err = 0; | |
319 | u8 *vaddr; | |
320 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
321 | struct page *page = frag->page; | |
322 | ||
323 | if (copy > len) | |
324 | copy = len; | |
325 | vaddr = kmap(page); | |
326 | csum2 = csum_and_copy_to_user(vaddr + | |
327 | frag->page_offset + | |
328 | offset - start, | |
329 | to, copy, 0, &err); | |
330 | kunmap(page); | |
331 | if (err) | |
332 | goto fault; | |
333 | *csump = csum_block_add(*csump, csum2, pos); | |
334 | if (!(len -= copy)) | |
335 | return 0; | |
336 | offset += copy; | |
337 | to += copy; | |
338 | pos += copy; | |
339 | } | |
340 | start = end; | |
341 | } | |
342 | ||
343 | if (skb_shinfo(skb)->frag_list) { | |
344 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
345 | ||
346 | for (; list; list=list->next) { | |
347 | int end; | |
348 | ||
349 | BUG_TRAP(start <= offset + len); | |
350 | ||
351 | end = start + list->len; | |
352 | if ((copy = end - offset) > 0) { | |
353 | unsigned int csum2 = 0; | |
354 | if (copy > len) | |
355 | copy = len; | |
356 | if (skb_copy_and_csum_datagram(list, | |
357 | offset - start, | |
358 | to, copy, | |
359 | &csum2)) | |
360 | goto fault; | |
361 | *csump = csum_block_add(*csump, csum2, pos); | |
362 | if ((len -= copy) == 0) | |
363 | return 0; | |
364 | offset += copy; | |
365 | to += copy; | |
366 | pos += copy; | |
367 | } | |
368 | start = end; | |
369 | } | |
370 | } | |
371 | if (!len) | |
372 | return 0; | |
373 | ||
374 | fault: | |
375 | return -EFAULT; | |
376 | } | |
377 | ||
378 | /** | |
379 | * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec. | |
4dc3b16b PP |
380 | * @skb: skbuff |
381 | * @hlen: hardware length | |
67be2dd1 | 382 | * @iov: io vector |
1da177e4 LT |
383 | * |
384 | * Caller _must_ check that skb will fit to this iovec. | |
385 | * | |
386 | * Returns: 0 - success. | |
387 | * -EINVAL - checksum failure. | |
388 | * -EFAULT - fault during copy. Beware, in this case iovec | |
389 | * can be modified! | |
390 | */ | |
391 | int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, | |
392 | int hlen, struct iovec *iov) | |
393 | { | |
394 | unsigned int csum; | |
395 | int chunk = skb->len - hlen; | |
396 | ||
397 | /* Skip filled elements. | |
398 | * Pretty silly, look at memcpy_toiovec, though 8) | |
399 | */ | |
400 | while (!iov->iov_len) | |
401 | iov++; | |
402 | ||
403 | if (iov->iov_len < chunk) { | |
404 | if ((unsigned short)csum_fold(skb_checksum(skb, 0, chunk + hlen, | |
405 | skb->csum))) | |
406 | goto csum_error; | |
407 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) | |
408 | goto fault; | |
409 | } else { | |
410 | csum = csum_partial(skb->data, hlen, skb->csum); | |
411 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, | |
412 | chunk, &csum)) | |
413 | goto fault; | |
414 | if ((unsigned short)csum_fold(csum)) | |
415 | goto csum_error; | |
416 | iov->iov_len -= chunk; | |
417 | iov->iov_base += chunk; | |
418 | } | |
419 | return 0; | |
420 | csum_error: | |
421 | return -EINVAL; | |
422 | fault: | |
423 | return -EFAULT; | |
424 | } | |
425 | ||
426 | /** | |
427 | * datagram_poll - generic datagram poll | |
4dc3b16b PP |
428 | * @file: file struct |
429 | * @sock: socket | |
430 | * @wait: poll table | |
1da177e4 LT |
431 | * |
432 | * Datagram poll: Again totally generic. This also handles | |
433 | * sequenced packet sockets providing the socket receive queue | |
434 | * is only ever holding data ready to receive. | |
435 | * | |
436 | * Note: when you _don't_ use this routine for this protocol, | |
437 | * and you use a different write policy from sock_writeable() | |
438 | * then please supply your own write_space callback. | |
439 | */ | |
440 | unsigned int datagram_poll(struct file *file, struct socket *sock, | |
441 | poll_table *wait) | |
442 | { | |
443 | struct sock *sk = sock->sk; | |
444 | unsigned int mask; | |
445 | ||
446 | poll_wait(file, sk->sk_sleep, wait); | |
447 | mask = 0; | |
448 | ||
449 | /* exceptional events? */ | |
450 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | |
451 | mask |= POLLERR; | |
452 | if (sk->sk_shutdown == SHUTDOWN_MASK) | |
453 | mask |= POLLHUP; | |
454 | ||
455 | /* readable? */ | |
456 | if (!skb_queue_empty(&sk->sk_receive_queue) || | |
457 | (sk->sk_shutdown & RCV_SHUTDOWN)) | |
458 | mask |= POLLIN | POLLRDNORM; | |
459 | ||
460 | /* Connection-based need to check for termination and startup */ | |
461 | if (connection_based(sk)) { | |
462 | if (sk->sk_state == TCP_CLOSE) | |
463 | mask |= POLLHUP; | |
464 | /* connection hasn't started yet? */ | |
465 | if (sk->sk_state == TCP_SYN_SENT) | |
466 | return mask; | |
467 | } | |
468 | ||
469 | /* writable? */ | |
470 | if (sock_writeable(sk)) | |
471 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | |
472 | else | |
473 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | |
474 | ||
475 | return mask; | |
476 | } | |
477 | ||
478 | EXPORT_SYMBOL(datagram_poll); | |
479 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); | |
480 | EXPORT_SYMBOL(skb_copy_datagram_iovec); | |
481 | EXPORT_SYMBOL(skb_free_datagram); | |
482 | EXPORT_SYMBOL(skb_recv_datagram); |