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 | * | |
113aa838 | 12 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old |
1da177e4 LT |
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> | |
1da177e4 LT |
40 | #include <linux/mm.h> |
41 | #include <linux/interrupt.h> | |
42 | #include <linux/errno.h> | |
43 | #include <linux/sched.h> | |
44 | #include <linux/inet.h> | |
1da177e4 LT |
45 | #include <linux/netdevice.h> |
46 | #include <linux/rtnetlink.h> | |
47 | #include <linux/poll.h> | |
48 | #include <linux/highmem.h> | |
3305b80c | 49 | #include <linux/spinlock.h> |
5a0e3ad6 | 50 | #include <linux/slab.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> | |
e9b3cc1b | 58 | #include <trace/events/skb.h> |
1da177e4 LT |
59 | |
60 | /* | |
61 | * Is a socket 'connection oriented' ? | |
62 | */ | |
63 | static inline int connection_based(struct sock *sk) | |
64 | { | |
65 | return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM; | |
66 | } | |
67 | ||
95c96174 | 68 | static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync, |
bf368e4e ED |
69 | void *key) |
70 | { | |
71 | unsigned long bits = (unsigned long)key; | |
72 | ||
73 | /* | |
74 | * Avoid a wakeup if event not interesting for us | |
75 | */ | |
76 | if (bits && !(bits & (POLLIN | POLLERR))) | |
77 | return 0; | |
78 | return autoremove_wake_function(wait, mode, sync, key); | |
79 | } | |
1da177e4 | 80 | /* |
39cc8613 | 81 | * Wait for the last received packet to be different from skb |
1da177e4 | 82 | */ |
39cc8613 BP |
83 | static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p, |
84 | const struct sk_buff *skb) | |
1da177e4 LT |
85 | { |
86 | int error; | |
bf368e4e | 87 | DEFINE_WAIT_FUNC(wait, receiver_wake_function); |
1da177e4 | 88 | |
aa395145 | 89 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
1da177e4 LT |
90 | |
91 | /* Socket errors? */ | |
92 | error = sock_error(sk); | |
93 | if (error) | |
94 | goto out_err; | |
95 | ||
39cc8613 | 96 | if (sk->sk_receive_queue.prev != skb) |
1da177e4 LT |
97 | goto out; |
98 | ||
99 | /* Socket shut down? */ | |
100 | if (sk->sk_shutdown & RCV_SHUTDOWN) | |
101 | goto out_noerr; | |
102 | ||
103 | /* Sequenced packets can come disconnected. | |
104 | * If so we report the problem | |
105 | */ | |
106 | error = -ENOTCONN; | |
107 | if (connection_based(sk) && | |
108 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) | |
109 | goto out_err; | |
110 | ||
111 | /* handle signals */ | |
112 | if (signal_pending(current)) | |
113 | goto interrupted; | |
114 | ||
115 | error = 0; | |
116 | *timeo_p = schedule_timeout(*timeo_p); | |
117 | out: | |
aa395145 | 118 | finish_wait(sk_sleep(sk), &wait); |
1da177e4 LT |
119 | return error; |
120 | interrupted: | |
121 | error = sock_intr_errno(*timeo_p); | |
122 | out_err: | |
123 | *err = error; | |
124 | goto out; | |
125 | out_noerr: | |
126 | *err = 0; | |
127 | error = 1; | |
128 | goto out; | |
129 | } | |
130 | ||
5fa39f16 | 131 | static struct sk_buff *skb_set_peeked(struct sk_buff *skb) |
0ba48ae9 HX |
132 | { |
133 | struct sk_buff *nskb; | |
134 | ||
135 | if (skb->peeked) | |
5fa39f16 | 136 | return skb; |
0ba48ae9 HX |
137 | |
138 | /* We have to unshare an skb before modifying it. */ | |
139 | if (!skb_shared(skb)) | |
140 | goto done; | |
141 | ||
142 | nskb = skb_clone(skb, GFP_ATOMIC); | |
143 | if (!nskb) | |
5fa39f16 | 144 | return ERR_PTR(-ENOMEM); |
0ba48ae9 HX |
145 | |
146 | skb->prev->next = nskb; | |
147 | skb->next->prev = nskb; | |
148 | nskb->prev = skb->prev; | |
149 | nskb->next = skb->next; | |
150 | ||
151 | consume_skb(skb); | |
152 | skb = nskb; | |
153 | ||
154 | done: | |
155 | skb->peeked = 1; | |
156 | ||
5fa39f16 | 157 | return skb; |
0ba48ae9 HX |
158 | } |
159 | ||
1da177e4 | 160 | /** |
a59322be | 161 | * __skb_recv_datagram - Receive a datagram skbuff |
4dc3b16b PP |
162 | * @sk: socket |
163 | * @flags: MSG_ flags | |
39cc8613 | 164 | * @peeked: returns non-zero if this packet has been seen before |
3f518bf7 PE |
165 | * @off: an offset in bytes to peek skb from. Returns an offset |
166 | * within an skb where data actually starts | |
4dc3b16b | 167 | * @err: error code returned |
1da177e4 LT |
168 | * |
169 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups | |
170 | * and possible races. This replaces identical code in packet, raw and | |
171 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes | |
172 | * the long standing peek and read race for datagram sockets. If you | |
173 | * alter this routine remember it must be re-entrant. | |
174 | * | |
175 | * This function will lock the socket if a skb is returned, so the caller | |
176 | * needs to unlock the socket in that case (usually by calling | |
177 | * skb_free_datagram) | |
178 | * | |
179 | * * It does not lock socket since today. This function is | |
180 | * * free of race conditions. This measure should/can improve | |
181 | * * significantly datagram socket latencies at high loads, | |
182 | * * when data copying to user space takes lots of time. | |
183 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet | |
184 | * * 8) Great win.) | |
185 | * * --ANK (980729) | |
186 | * | |
187 | * The order of the tests when we find no data waiting are specified | |
188 | * quite explicitly by POSIX 1003.1g, don't change them without having | |
189 | * the standard around please. | |
190 | */ | |
95c96174 | 191 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags, |
3f518bf7 | 192 | int *peeked, int *off, int *err) |
1da177e4 | 193 | { |
0ba48ae9 | 194 | struct sk_buff_head *queue = &sk->sk_receive_queue; |
39cc8613 | 195 | struct sk_buff *skb, *last; |
0ba48ae9 | 196 | unsigned long cpu_flags; |
1da177e4 LT |
197 | long timeo; |
198 | /* | |
199 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() | |
200 | */ | |
201 | int error = sock_error(sk); | |
202 | ||
203 | if (error) | |
204 | goto no_packet; | |
205 | ||
a59322be | 206 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
1da177e4 LT |
207 | |
208 | do { | |
209 | /* Again only user level code calls this function, so nothing | |
210 | * interrupt level will suddenly eat the receive_queue. | |
211 | * | |
212 | * Look at current nfs client by the way... | |
8917a3c0 | 213 | * However, this function was correct in any case. 8) |
1da177e4 | 214 | */ |
39cc8613 | 215 | int _off = *off; |
a59322be | 216 | |
39cc8613 | 217 | last = (struct sk_buff *)queue; |
4934b032 | 218 | spin_lock_irqsave(&queue->lock, cpu_flags); |
3f518bf7 | 219 | skb_queue_walk(queue, skb) { |
39cc8613 | 220 | last = skb; |
a59322be HX |
221 | *peeked = skb->peeked; |
222 | if (flags & MSG_PEEK) { | |
39cc8613 | 223 | if (_off >= skb->len && (skb->len || _off || |
add05ad4 | 224 | skb->peeked)) { |
39cc8613 | 225 | _off -= skb->len; |
3f518bf7 PE |
226 | continue; |
227 | } | |
0ba48ae9 | 228 | |
5fa39f16 HX |
229 | skb = skb_set_peeked(skb); |
230 | error = PTR_ERR(skb); | |
231 | if (IS_ERR(skb)) | |
0ba48ae9 HX |
232 | goto unlock_err; |
233 | ||
1da177e4 | 234 | atomic_inc(&skb->users); |
a59322be | 235 | } else |
4934b032 | 236 | __skb_unlink(skb, queue); |
1da177e4 | 237 | |
3f518bf7 | 238 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
39cc8613 | 239 | *off = _off; |
1da177e4 | 240 | return skb; |
3f518bf7 PE |
241 | } |
242 | spin_unlock_irqrestore(&queue->lock, cpu_flags); | |
1da177e4 LT |
243 | |
244 | /* User doesn't want to wait */ | |
245 | error = -EAGAIN; | |
246 | if (!timeo) | |
247 | goto no_packet; | |
248 | ||
39cc8613 | 249 | } while (!wait_for_more_packets(sk, err, &timeo, last)); |
1da177e4 LT |
250 | |
251 | return NULL; | |
252 | ||
0ba48ae9 HX |
253 | unlock_err: |
254 | spin_unlock_irqrestore(&queue->lock, cpu_flags); | |
1da177e4 LT |
255 | no_packet: |
256 | *err = error; | |
257 | return NULL; | |
258 | } | |
a59322be HX |
259 | EXPORT_SYMBOL(__skb_recv_datagram); |
260 | ||
95c96174 | 261 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, |
a59322be HX |
262 | int noblock, int *err) |
263 | { | |
3f518bf7 | 264 | int peeked, off = 0; |
a59322be HX |
265 | |
266 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), | |
3f518bf7 | 267 | &peeked, &off, err); |
a59322be | 268 | } |
9e34a5b5 | 269 | EXPORT_SYMBOL(skb_recv_datagram); |
1da177e4 LT |
270 | |
271 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) | |
272 | { | |
ead2ceb0 | 273 | consume_skb(skb); |
270acefa | 274 | sk_mem_reclaim_partial(sk); |
1da177e4 | 275 | } |
9d410c79 ED |
276 | EXPORT_SYMBOL(skb_free_datagram); |
277 | ||
278 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb) | |
279 | { | |
8a74ad60 ED |
280 | bool slow; |
281 | ||
93bb64ea ED |
282 | if (likely(atomic_read(&skb->users) == 1)) |
283 | smp_rmb(); | |
284 | else if (likely(!atomic_dec_and_test(&skb->users))) | |
285 | return; | |
286 | ||
8a74ad60 | 287 | slow = lock_sock_fast(sk); |
4b0b72f7 ED |
288 | skb_orphan(skb); |
289 | sk_mem_reclaim_partial(sk); | |
8a74ad60 | 290 | unlock_sock_fast(sk, slow); |
4b0b72f7 | 291 | |
93bb64ea ED |
292 | /* skb is now orphaned, can be freed outside of locked section */ |
293 | __kfree_skb(skb); | |
9d410c79 ED |
294 | } |
295 | EXPORT_SYMBOL(skb_free_datagram_locked); | |
1da177e4 | 296 | |
3305b80c HX |
297 | /** |
298 | * skb_kill_datagram - Free a datagram skbuff forcibly | |
299 | * @sk: socket | |
300 | * @skb: datagram skbuff | |
301 | * @flags: MSG_ flags | |
302 | * | |
303 | * This function frees a datagram skbuff that was received by | |
304 | * skb_recv_datagram. The flags argument must match the one | |
305 | * used for skb_recv_datagram. | |
306 | * | |
307 | * If the MSG_PEEK flag is set, and the packet is still on the | |
308 | * receive queue of the socket, it will be taken off the queue | |
309 | * before it is freed. | |
310 | * | |
311 | * This function currently only disables BH when acquiring the | |
312 | * sk_receive_queue lock. Therefore it must not be used in a | |
313 | * context where that lock is acquired in an IRQ context. | |
27ab2568 HX |
314 | * |
315 | * It returns 0 if the packet was removed by us. | |
3305b80c HX |
316 | */ |
317 | ||
27ab2568 | 318 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) |
3305b80c | 319 | { |
27ab2568 HX |
320 | int err = 0; |
321 | ||
3305b80c | 322 | if (flags & MSG_PEEK) { |
27ab2568 | 323 | err = -ENOENT; |
3305b80c HX |
324 | spin_lock_bh(&sk->sk_receive_queue.lock); |
325 | if (skb == skb_peek(&sk->sk_receive_queue)) { | |
326 | __skb_unlink(skb, &sk->sk_receive_queue); | |
327 | atomic_dec(&skb->users); | |
27ab2568 | 328 | err = 0; |
3305b80c HX |
329 | } |
330 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
331 | } | |
332 | ||
61de71c6 | 333 | kfree_skb(skb); |
8edf19c2 | 334 | atomic_inc(&sk->sk_drops); |
61de71c6 JD |
335 | sk_mem_reclaim_partial(sk); |
336 | ||
27ab2568 | 337 | return err; |
3305b80c | 338 | } |
3305b80c HX |
339 | EXPORT_SYMBOL(skb_kill_datagram); |
340 | ||
1da177e4 LT |
341 | /** |
342 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. | |
4dc3b16b PP |
343 | * @skb: buffer to copy |
344 | * @offset: offset in the buffer to start copying from | |
67be2dd1 | 345 | * @to: io vector to copy to |
4dc3b16b | 346 | * @len: amount of data to copy from buffer to iovec |
1da177e4 LT |
347 | * |
348 | * Note: the iovec is modified during the copy. | |
349 | */ | |
350 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, | |
351 | struct iovec *to, int len) | |
352 | { | |
1a028e50 DM |
353 | int start = skb_headlen(skb); |
354 | int i, copy = start - offset; | |
5b1a002a | 355 | struct sk_buff *frag_iter; |
c75d721c | 356 | |
e9b3cc1b NH |
357 | trace_skb_copy_datagram_iovec(skb, len); |
358 | ||
b4d9eda0 DM |
359 | /* Copy header. */ |
360 | if (copy > 0) { | |
361 | if (copy > len) | |
362 | copy = len; | |
363 | if (memcpy_toiovec(to, skb->data + offset, copy)) | |
364 | goto fault; | |
365 | if ((len -= copy) == 0) | |
366 | return 0; | |
367 | offset += copy; | |
368 | } | |
c75d721c | 369 | |
b4d9eda0 DM |
370 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
371 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1a028e50 | 372 | int end; |
9e903e08 | 373 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
1da177e4 | 374 | |
547b792c | 375 | WARN_ON(start > offset + len); |
1a028e50 | 376 | |
9e903e08 | 377 | end = start + skb_frag_size(frag); |
b4d9eda0 DM |
378 | if ((copy = end - offset) > 0) { |
379 | int err; | |
380 | u8 *vaddr; | |
ea2ab693 | 381 | struct page *page = skb_frag_page(frag); |
1da177e4 LT |
382 | |
383 | if (copy > len) | |
384 | copy = len; | |
b4d9eda0 | 385 | vaddr = kmap(page); |
1a028e50 DM |
386 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
387 | offset - start, copy); | |
b4d9eda0 | 388 | kunmap(page); |
1da177e4 LT |
389 | if (err) |
390 | goto fault; | |
391 | if (!(len -= copy)) | |
392 | return 0; | |
393 | offset += copy; | |
394 | } | |
1a028e50 | 395 | start = end; |
1da177e4 | 396 | } |
b4d9eda0 | 397 | |
5b1a002a DM |
398 | skb_walk_frags(skb, frag_iter) { |
399 | int end; | |
400 | ||
401 | WARN_ON(start > offset + len); | |
402 | ||
403 | end = start + frag_iter->len; | |
404 | if ((copy = end - offset) > 0) { | |
405 | if (copy > len) | |
406 | copy = len; | |
407 | if (skb_copy_datagram_iovec(frag_iter, | |
408 | offset - start, | |
409 | to, copy)) | |
410 | goto fault; | |
411 | if ((len -= copy) == 0) | |
412 | return 0; | |
413 | offset += copy; | |
b4d9eda0 | 414 | } |
5b1a002a | 415 | start = end; |
1da177e4 | 416 | } |
b4d9eda0 DM |
417 | if (!len) |
418 | return 0; | |
419 | ||
1da177e4 LT |
420 | fault: |
421 | return -EFAULT; | |
422 | } | |
9e34a5b5 | 423 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
1da177e4 | 424 | |
0a1ec07a MT |
425 | /** |
426 | * skb_copy_datagram_const_iovec - Copy a datagram to an iovec. | |
427 | * @skb: buffer to copy | |
428 | * @offset: offset in the buffer to start copying from | |
429 | * @to: io vector to copy to | |
430 | * @to_offset: offset in the io vector to start copying to | |
431 | * @len: amount of data to copy from buffer to iovec | |
432 | * | |
433 | * Returns 0 or -EFAULT. | |
434 | * Note: the iovec is not modified during the copy. | |
435 | */ | |
436 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, | |
437 | const struct iovec *to, int to_offset, | |
438 | int len) | |
439 | { | |
440 | int start = skb_headlen(skb); | |
441 | int i, copy = start - offset; | |
5b1a002a | 442 | struct sk_buff *frag_iter; |
0a1ec07a MT |
443 | |
444 | /* Copy header. */ | |
445 | if (copy > 0) { | |
446 | if (copy > len) | |
447 | copy = len; | |
448 | if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) | |
449 | goto fault; | |
450 | if ((len -= copy) == 0) | |
451 | return 0; | |
452 | offset += copy; | |
453 | to_offset += copy; | |
454 | } | |
455 | ||
456 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
457 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
458 | int end; | |
9e903e08 | 459 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
0a1ec07a MT |
460 | |
461 | WARN_ON(start > offset + len); | |
462 | ||
9e903e08 | 463 | end = start + skb_frag_size(frag); |
0a1ec07a MT |
464 | if ((copy = end - offset) > 0) { |
465 | int err; | |
466 | u8 *vaddr; | |
ea2ab693 | 467 | struct page *page = skb_frag_page(frag); |
0a1ec07a MT |
468 | |
469 | if (copy > len) | |
470 | copy = len; | |
471 | vaddr = kmap(page); | |
472 | err = memcpy_toiovecend(to, vaddr + frag->page_offset + | |
473 | offset - start, to_offset, copy); | |
474 | kunmap(page); | |
475 | if (err) | |
476 | goto fault; | |
477 | if (!(len -= copy)) | |
478 | return 0; | |
479 | offset += copy; | |
480 | to_offset += copy; | |
481 | } | |
482 | start = end; | |
483 | } | |
484 | ||
5b1a002a DM |
485 | skb_walk_frags(skb, frag_iter) { |
486 | int end; | |
487 | ||
488 | WARN_ON(start > offset + len); | |
489 | ||
490 | end = start + frag_iter->len; | |
491 | if ((copy = end - offset) > 0) { | |
492 | if (copy > len) | |
493 | copy = len; | |
494 | if (skb_copy_datagram_const_iovec(frag_iter, | |
495 | offset - start, | |
496 | to, to_offset, | |
497 | copy)) | |
498 | goto fault; | |
499 | if ((len -= copy) == 0) | |
500 | return 0; | |
501 | offset += copy; | |
502 | to_offset += copy; | |
0a1ec07a | 503 | } |
5b1a002a | 504 | start = end; |
0a1ec07a MT |
505 | } |
506 | if (!len) | |
507 | return 0; | |
508 | ||
509 | fault: | |
510 | return -EFAULT; | |
511 | } | |
512 | EXPORT_SYMBOL(skb_copy_datagram_const_iovec); | |
513 | ||
db543c1f RR |
514 | /** |
515 | * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. | |
516 | * @skb: buffer to copy | |
517 | * @offset: offset in the buffer to start copying to | |
518 | * @from: io vector to copy to | |
6f26c9a7 | 519 | * @from_offset: offset in the io vector to start copying from |
db543c1f RR |
520 | * @len: amount of data to copy to buffer from iovec |
521 | * | |
522 | * Returns 0 or -EFAULT. | |
6f26c9a7 | 523 | * Note: the iovec is not modified during the copy. |
db543c1f RR |
524 | */ |
525 | int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset, | |
6f26c9a7 MT |
526 | const struct iovec *from, int from_offset, |
527 | int len) | |
db543c1f RR |
528 | { |
529 | int start = skb_headlen(skb); | |
530 | int i, copy = start - offset; | |
5b1a002a | 531 | struct sk_buff *frag_iter; |
db543c1f RR |
532 | |
533 | /* Copy header. */ | |
534 | if (copy > 0) { | |
535 | if (copy > len) | |
536 | copy = len; | |
d2d27bfd SS |
537 | if (memcpy_fromiovecend(skb->data + offset, from, from_offset, |
538 | copy)) | |
db543c1f RR |
539 | goto fault; |
540 | if ((len -= copy) == 0) | |
541 | return 0; | |
542 | offset += copy; | |
6f26c9a7 | 543 | from_offset += copy; |
db543c1f RR |
544 | } |
545 | ||
546 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
547 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
548 | int end; | |
9e903e08 | 549 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
db543c1f RR |
550 | |
551 | WARN_ON(start > offset + len); | |
552 | ||
9e903e08 | 553 | end = start + skb_frag_size(frag); |
db543c1f RR |
554 | if ((copy = end - offset) > 0) { |
555 | int err; | |
556 | u8 *vaddr; | |
ea2ab693 | 557 | struct page *page = skb_frag_page(frag); |
db543c1f RR |
558 | |
559 | if (copy > len) | |
560 | copy = len; | |
561 | vaddr = kmap(page); | |
6f26c9a7 MT |
562 | err = memcpy_fromiovecend(vaddr + frag->page_offset + |
563 | offset - start, | |
564 | from, from_offset, copy); | |
db543c1f RR |
565 | kunmap(page); |
566 | if (err) | |
567 | goto fault; | |
568 | ||
569 | if (!(len -= copy)) | |
570 | return 0; | |
571 | offset += copy; | |
6f26c9a7 | 572 | from_offset += copy; |
db543c1f RR |
573 | } |
574 | start = end; | |
575 | } | |
576 | ||
5b1a002a DM |
577 | skb_walk_frags(skb, frag_iter) { |
578 | int end; | |
579 | ||
580 | WARN_ON(start > offset + len); | |
581 | ||
582 | end = start + frag_iter->len; | |
583 | if ((copy = end - offset) > 0) { | |
584 | if (copy > len) | |
585 | copy = len; | |
586 | if (skb_copy_datagram_from_iovec(frag_iter, | |
587 | offset - start, | |
588 | from, | |
589 | from_offset, | |
590 | copy)) | |
591 | goto fault; | |
592 | if ((len -= copy) == 0) | |
593 | return 0; | |
594 | offset += copy; | |
595 | from_offset += copy; | |
db543c1f | 596 | } |
5b1a002a | 597 | start = end; |
db543c1f RR |
598 | } |
599 | if (!len) | |
600 | return 0; | |
601 | ||
602 | fault: | |
603 | return -EFAULT; | |
604 | } | |
605 | EXPORT_SYMBOL(skb_copy_datagram_from_iovec); | |
606 | ||
1da177e4 LT |
607 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
608 | u8 __user *to, int len, | |
5084205f | 609 | __wsum *csump) |
1da177e4 | 610 | { |
1a028e50 | 611 | int start = skb_headlen(skb); |
1a028e50 | 612 | int i, copy = start - offset; |
5b1a002a DM |
613 | struct sk_buff *frag_iter; |
614 | int pos = 0; | |
1da177e4 LT |
615 | |
616 | /* Copy header. */ | |
617 | if (copy > 0) { | |
618 | int err = 0; | |
619 | if (copy > len) | |
620 | copy = len; | |
621 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, | |
622 | *csump, &err); | |
623 | if (err) | |
624 | goto fault; | |
625 | if ((len -= copy) == 0) | |
626 | return 0; | |
627 | offset += copy; | |
628 | to += copy; | |
629 | pos = copy; | |
630 | } | |
631 | ||
632 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1a028e50 | 633 | int end; |
9e903e08 | 634 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
1da177e4 | 635 | |
547b792c | 636 | WARN_ON(start > offset + len); |
1a028e50 | 637 | |
9e903e08 | 638 | end = start + skb_frag_size(frag); |
1da177e4 | 639 | if ((copy = end - offset) > 0) { |
5084205f | 640 | __wsum csum2; |
1da177e4 LT |
641 | int err = 0; |
642 | u8 *vaddr; | |
ea2ab693 | 643 | struct page *page = skb_frag_page(frag); |
1da177e4 LT |
644 | |
645 | if (copy > len) | |
646 | copy = len; | |
647 | vaddr = kmap(page); | |
648 | csum2 = csum_and_copy_to_user(vaddr + | |
1a028e50 DM |
649 | frag->page_offset + |
650 | offset - start, | |
1da177e4 LT |
651 | to, copy, 0, &err); |
652 | kunmap(page); | |
653 | if (err) | |
654 | goto fault; | |
655 | *csump = csum_block_add(*csump, csum2, pos); | |
656 | if (!(len -= copy)) | |
657 | return 0; | |
658 | offset += copy; | |
659 | to += copy; | |
660 | pos += copy; | |
661 | } | |
1a028e50 | 662 | start = end; |
1da177e4 LT |
663 | } |
664 | ||
5b1a002a DM |
665 | skb_walk_frags(skb, frag_iter) { |
666 | int end; | |
667 | ||
668 | WARN_ON(start > offset + len); | |
669 | ||
670 | end = start + frag_iter->len; | |
671 | if ((copy = end - offset) > 0) { | |
672 | __wsum csum2 = 0; | |
673 | if (copy > len) | |
674 | copy = len; | |
675 | if (skb_copy_and_csum_datagram(frag_iter, | |
676 | offset - start, | |
677 | to, copy, | |
678 | &csum2)) | |
679 | goto fault; | |
680 | *csump = csum_block_add(*csump, csum2, pos); | |
681 | if ((len -= copy) == 0) | |
682 | return 0; | |
683 | offset += copy; | |
684 | to += copy; | |
685 | pos += copy; | |
1da177e4 | 686 | } |
5b1a002a | 687 | start = end; |
1da177e4 LT |
688 | } |
689 | if (!len) | |
690 | return 0; | |
691 | ||
692 | fault: | |
693 | return -EFAULT; | |
694 | } | |
695 | ||
759e5d00 | 696 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
fb286bb2 | 697 | { |
d3bc23e7 | 698 | __sum16 sum; |
fb286bb2 | 699 | |
759e5d00 | 700 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
fb286bb2 | 701 | if (likely(!sum)) { |
84fa7933 | 702 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
fb286bb2 | 703 | netdev_rx_csum_fault(skb->dev); |
4164cda8 HX |
704 | if (!skb_shared(skb)) |
705 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
fb286bb2 HX |
706 | } |
707 | return sum; | |
708 | } | |
759e5d00 HX |
709 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
710 | ||
711 | __sum16 __skb_checksum_complete(struct sk_buff *skb) | |
712 | { | |
713 | return __skb_checksum_complete_head(skb, skb->len); | |
714 | } | |
fb286bb2 HX |
715 | EXPORT_SYMBOL(__skb_checksum_complete); |
716 | ||
1da177e4 LT |
717 | /** |
718 | * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec. | |
4dc3b16b PP |
719 | * @skb: skbuff |
720 | * @hlen: hardware length | |
67be2dd1 | 721 | * @iov: io vector |
4ec93edb | 722 | * |
1da177e4 LT |
723 | * Caller _must_ check that skb will fit to this iovec. |
724 | * | |
725 | * Returns: 0 - success. | |
726 | * -EINVAL - checksum failure. | |
727 | * -EFAULT - fault during copy. Beware, in this case iovec | |
728 | * can be modified! | |
729 | */ | |
fb286bb2 | 730 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
1da177e4 LT |
731 | int hlen, struct iovec *iov) |
732 | { | |
d3bc23e7 | 733 | __wsum csum; |
1da177e4 LT |
734 | int chunk = skb->len - hlen; |
735 | ||
ef8aef55 HX |
736 | if (!chunk) |
737 | return 0; | |
738 | ||
1da177e4 LT |
739 | /* Skip filled elements. |
740 | * Pretty silly, look at memcpy_toiovec, though 8) | |
741 | */ | |
742 | while (!iov->iov_len) | |
743 | iov++; | |
744 | ||
745 | if (iov->iov_len < chunk) { | |
fb286bb2 | 746 | if (__skb_checksum_complete(skb)) |
1da177e4 LT |
747 | goto csum_error; |
748 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) | |
749 | goto fault; | |
750 | } else { | |
751 | csum = csum_partial(skb->data, hlen, skb->csum); | |
752 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, | |
753 | chunk, &csum)) | |
754 | goto fault; | |
d3bc23e7 | 755 | if (csum_fold(csum)) |
1da177e4 | 756 | goto csum_error; |
84fa7933 | 757 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
fb286bb2 | 758 | netdev_rx_csum_fault(skb->dev); |
1da177e4 LT |
759 | iov->iov_len -= chunk; |
760 | iov->iov_base += chunk; | |
761 | } | |
762 | return 0; | |
763 | csum_error: | |
764 | return -EINVAL; | |
765 | fault: | |
766 | return -EFAULT; | |
767 | } | |
9e34a5b5 | 768 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
1da177e4 LT |
769 | |
770 | /** | |
771 | * datagram_poll - generic datagram poll | |
4dc3b16b PP |
772 | * @file: file struct |
773 | * @sock: socket | |
774 | * @wait: poll table | |
1da177e4 LT |
775 | * |
776 | * Datagram poll: Again totally generic. This also handles | |
777 | * sequenced packet sockets providing the socket receive queue | |
778 | * is only ever holding data ready to receive. | |
779 | * | |
780 | * Note: when you _don't_ use this routine for this protocol, | |
781 | * and you use a different write policy from sock_writeable() | |
782 | * then please supply your own write_space callback. | |
783 | */ | |
784 | unsigned int datagram_poll(struct file *file, struct socket *sock, | |
785 | poll_table *wait) | |
786 | { | |
787 | struct sock *sk = sock->sk; | |
788 | unsigned int mask; | |
789 | ||
aa395145 | 790 | sock_poll_wait(file, sk_sleep(sk), wait); |
1da177e4 LT |
791 | mask = 0; |
792 | ||
793 | /* exceptional events? */ | |
794 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | |
7d4c04fc | 795 | mask |= POLLERR | |
8facd5fb | 796 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
7d4c04fc | 797 | |
f348d70a | 798 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
db40980f | 799 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
1da177e4 LT |
800 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
801 | mask |= POLLHUP; | |
802 | ||
803 | /* readable? */ | |
db40980f | 804 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
1da177e4 LT |
805 | mask |= POLLIN | POLLRDNORM; |
806 | ||
807 | /* Connection-based need to check for termination and startup */ | |
808 | if (connection_based(sk)) { | |
809 | if (sk->sk_state == TCP_CLOSE) | |
810 | mask |= POLLHUP; | |
811 | /* connection hasn't started yet? */ | |
812 | if (sk->sk_state == TCP_SYN_SENT) | |
813 | return mask; | |
814 | } | |
815 | ||
816 | /* writable? */ | |
817 | if (sock_writeable(sk)) | |
818 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | |
819 | else | |
820 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | |
821 | ||
822 | return mask; | |
823 | } | |
1da177e4 | 824 | EXPORT_SYMBOL(datagram_poll); |