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