]>
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> | |
7c0f6ba6 | 39 | #include <linux/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 | ||
ac6424b9 | 71 | static int receiver_wake_function(wait_queue_entry_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 | */ |
ea3793ee RW |
86 | int __skb_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 | } | |
ea3793ee | 133 | EXPORT_SYMBOL(__skb_wait_for_more_packets); |
1da177e4 | 134 | |
a0a2a660 | 135 | static struct sk_buff *skb_set_peeked(struct sk_buff *skb) |
738ac1eb HX |
136 | { |
137 | struct sk_buff *nskb; | |
138 | ||
139 | if (skb->peeked) | |
a0a2a660 | 140 | return skb; |
738ac1eb HX |
141 | |
142 | /* We have to unshare an skb before modifying it. */ | |
143 | if (!skb_shared(skb)) | |
144 | goto done; | |
145 | ||
146 | nskb = skb_clone(skb, GFP_ATOMIC); | |
147 | if (!nskb) | |
a0a2a660 | 148 | return ERR_PTR(-ENOMEM); |
738ac1eb HX |
149 | |
150 | skb->prev->next = nskb; | |
151 | skb->next->prev = nskb; | |
152 | nskb->prev = skb->prev; | |
153 | nskb->next = skb->next; | |
154 | ||
155 | consume_skb(skb); | |
156 | skb = nskb; | |
157 | ||
158 | done: | |
159 | skb->peeked = 1; | |
160 | ||
a0a2a660 | 161 | return skb; |
738ac1eb HX |
162 | } |
163 | ||
65101aec PA |
164 | struct sk_buff *__skb_try_recv_from_queue(struct sock *sk, |
165 | struct sk_buff_head *queue, | |
166 | unsigned int flags, | |
167 | void (*destructor)(struct sock *sk, | |
168 | struct sk_buff *skb), | |
169 | int *peeked, int *off, int *err, | |
170 | struct sk_buff **last) | |
171 | { | |
a0917e0b | 172 | bool peek_at_off = false; |
65101aec | 173 | struct sk_buff *skb; |
a0917e0b MD |
174 | int _off = 0; |
175 | ||
176 | if (unlikely(flags & MSG_PEEK && *off >= 0)) { | |
177 | peek_at_off = true; | |
178 | _off = *off; | |
179 | } | |
65101aec PA |
180 | |
181 | *last = queue->prev; | |
182 | skb_queue_walk(queue, skb) { | |
183 | if (flags & MSG_PEEK) { | |
a0917e0b MD |
184 | if (peek_at_off && _off >= skb->len && |
185 | (_off || skb->peeked)) { | |
de321ed3 | 186 | _off -= skb->len; |
65101aec PA |
187 | continue; |
188 | } | |
189 | if (!skb->len) { | |
190 | skb = skb_set_peeked(skb); | |
191 | if (unlikely(IS_ERR(skb))) { | |
192 | *err = PTR_ERR(skb); | |
de321ed3 | 193 | return NULL; |
65101aec PA |
194 | } |
195 | } | |
196 | *peeked = 1; | |
63354797 | 197 | refcount_inc(&skb->users); |
65101aec PA |
198 | } else { |
199 | __skb_unlink(skb, queue); | |
200 | if (destructor) | |
201 | destructor(sk, skb); | |
202 | } | |
de321ed3 | 203 | *off = _off; |
65101aec PA |
204 | return skb; |
205 | } | |
206 | return NULL; | |
207 | } | |
208 | ||
1da177e4 | 209 | /** |
ea3793ee | 210 | * __skb_try_recv_datagram - Receive a datagram skbuff |
4dc3b16b | 211 | * @sk: socket |
d3f6cd9e | 212 | * @flags: MSG\_ flags |
7c13f97f | 213 | * @destructor: invoked under the receive lock on successful dequeue |
39cc8613 | 214 | * @peeked: returns non-zero if this packet has been seen before |
3f518bf7 PE |
215 | * @off: an offset in bytes to peek skb from. Returns an offset |
216 | * within an skb where data actually starts | |
4dc3b16b | 217 | * @err: error code returned |
ea3793ee RW |
218 | * @last: set to last peeked message to inform the wait function |
219 | * what to look for when peeking | |
1da177e4 LT |
220 | * |
221 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups | |
222 | * and possible races. This replaces identical code in packet, raw and | |
223 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes | |
224 | * the long standing peek and read race for datagram sockets. If you | |
225 | * alter this routine remember it must be re-entrant. | |
226 | * | |
ea3793ee RW |
227 | * This function will lock the socket if a skb is returned, so |
228 | * the caller needs to unlock the socket in that case (usually by | |
d651983d | 229 | * calling skb_free_datagram). Returns NULL with @err set to |
ea3793ee RW |
230 | * -EAGAIN if no data was available or to some other value if an |
231 | * error was detected. | |
1da177e4 LT |
232 | * |
233 | * * It does not lock socket since today. This function is | |
234 | * * free of race conditions. This measure should/can improve | |
235 | * * significantly datagram socket latencies at high loads, | |
236 | * * when data copying to user space takes lots of time. | |
237 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet | |
238 | * * 8) Great win.) | |
239 | * * --ANK (980729) | |
240 | * | |
241 | * The order of the tests when we find no data waiting are specified | |
242 | * quite explicitly by POSIX 1003.1g, don't change them without having | |
243 | * the standard around please. | |
244 | */ | |
ea3793ee | 245 | struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags, |
7c13f97f PA |
246 | void (*destructor)(struct sock *sk, |
247 | struct sk_buff *skb), | |
ea3793ee RW |
248 | int *peeked, int *off, int *err, |
249 | struct sk_buff **last) | |
1da177e4 | 250 | { |
738ac1eb | 251 | struct sk_buff_head *queue = &sk->sk_receive_queue; |
ea3793ee | 252 | struct sk_buff *skb; |
738ac1eb | 253 | unsigned long cpu_flags; |
1da177e4 LT |
254 | /* |
255 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() | |
256 | */ | |
257 | int error = sock_error(sk); | |
258 | ||
259 | if (error) | |
260 | goto no_packet; | |
261 | ||
a297569f | 262 | *peeked = 0; |
1da177e4 LT |
263 | do { |
264 | /* Again only user level code calls this function, so nothing | |
265 | * interrupt level will suddenly eat the receive_queue. | |
266 | * | |
267 | * Look at current nfs client by the way... | |
8917a3c0 | 268 | * However, this function was correct in any case. 8) |
1da177e4 | 269 | */ |
4934b032 | 270 | spin_lock_irqsave(&queue->lock, cpu_flags); |
65101aec | 271 | skb = __skb_try_recv_from_queue(sk, queue, flags, destructor, |
de321ed3 | 272 | peeked, off, &error, last); |
3f518bf7 | 273 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
de321ed3 AV |
274 | if (error) |
275 | goto no_packet; | |
65101aec PA |
276 | if (skb) |
277 | return skb; | |
2b5cd0df AD |
278 | |
279 | if (!sk_can_busy_loop(sk)) | |
280 | break; | |
281 | ||
282 | sk_busy_loop(sk, flags & MSG_DONTWAIT); | |
283 | } while (!skb_queue_empty(&sk->sk_receive_queue)); | |
1da177e4 | 284 | |
ea3793ee | 285 | error = -EAGAIN; |
a5b50476 | 286 | |
ea3793ee RW |
287 | no_packet: |
288 | *err = error; | |
289 | return NULL; | |
290 | } | |
291 | EXPORT_SYMBOL(__skb_try_recv_datagram); | |
1da177e4 | 292 | |
ea3793ee | 293 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags, |
7c13f97f PA |
294 | void (*destructor)(struct sock *sk, |
295 | struct sk_buff *skb), | |
ea3793ee RW |
296 | int *peeked, int *off, int *err) |
297 | { | |
298 | struct sk_buff *skb, *last; | |
299 | long timeo; | |
1da177e4 | 300 | |
ea3793ee RW |
301 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
302 | ||
303 | do { | |
7c13f97f PA |
304 | skb = __skb_try_recv_datagram(sk, flags, destructor, peeked, |
305 | off, err, &last); | |
ea3793ee RW |
306 | if (skb) |
307 | return skb; | |
308 | ||
760a4322 | 309 | if (*err != -EAGAIN) |
ea3793ee RW |
310 | break; |
311 | } while (timeo && | |
312 | !__skb_wait_for_more_packets(sk, err, &timeo, last)); | |
1da177e4 | 313 | |
1da177e4 LT |
314 | return NULL; |
315 | } | |
a59322be HX |
316 | EXPORT_SYMBOL(__skb_recv_datagram); |
317 | ||
95c96174 | 318 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, |
a59322be HX |
319 | int noblock, int *err) |
320 | { | |
3f518bf7 | 321 | int peeked, off = 0; |
a59322be HX |
322 | |
323 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), | |
7c13f97f | 324 | NULL, &peeked, &off, err); |
a59322be | 325 | } |
9e34a5b5 | 326 | EXPORT_SYMBOL(skb_recv_datagram); |
1da177e4 LT |
327 | |
328 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) | |
329 | { | |
ead2ceb0 | 330 | consume_skb(skb); |
270acefa | 331 | sk_mem_reclaim_partial(sk); |
1da177e4 | 332 | } |
9d410c79 ED |
333 | EXPORT_SYMBOL(skb_free_datagram); |
334 | ||
627d2d6b | 335 | void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len) |
9d410c79 | 336 | { |
8a74ad60 ED |
337 | bool slow; |
338 | ||
3889a803 | 339 | if (!skb_unref(skb)) { |
627d2d6b | 340 | sk_peek_offset_bwd(sk, len); |
93bb64ea | 341 | return; |
627d2d6b | 342 | } |
93bb64ea | 343 | |
8a74ad60 | 344 | slow = lock_sock_fast(sk); |
627d2d6b | 345 | sk_peek_offset_bwd(sk, len); |
4b0b72f7 ED |
346 | skb_orphan(skb); |
347 | sk_mem_reclaim_partial(sk); | |
8a74ad60 | 348 | unlock_sock_fast(sk, slow); |
4b0b72f7 | 349 | |
93bb64ea ED |
350 | /* skb is now orphaned, can be freed outside of locked section */ |
351 | __kfree_skb(skb); | |
9d410c79 | 352 | } |
627d2d6b | 353 | EXPORT_SYMBOL(__skb_free_datagram_locked); |
1da177e4 | 354 | |
65101aec PA |
355 | int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue, |
356 | struct sk_buff *skb, unsigned int flags, | |
69629464 ED |
357 | void (*destructor)(struct sock *sk, |
358 | struct sk_buff *skb)) | |
f8c3bf00 PA |
359 | { |
360 | int err = 0; | |
361 | ||
362 | if (flags & MSG_PEEK) { | |
363 | err = -ENOENT; | |
65101aec | 364 | spin_lock_bh(&sk_queue->lock); |
fd6055a8 | 365 | if (skb->next) { |
65101aec | 366 | __skb_unlink(skb, sk_queue); |
63354797 | 367 | refcount_dec(&skb->users); |
69629464 ED |
368 | if (destructor) |
369 | destructor(sk, skb); | |
f8c3bf00 PA |
370 | err = 0; |
371 | } | |
65101aec | 372 | spin_unlock_bh(&sk_queue->lock); |
f8c3bf00 PA |
373 | } |
374 | ||
375 | atomic_inc(&sk->sk_drops); | |
376 | return err; | |
377 | } | |
378 | EXPORT_SYMBOL(__sk_queue_drop_skb); | |
379 | ||
3305b80c HX |
380 | /** |
381 | * skb_kill_datagram - Free a datagram skbuff forcibly | |
382 | * @sk: socket | |
383 | * @skb: datagram skbuff | |
d3f6cd9e | 384 | * @flags: MSG\_ flags |
3305b80c HX |
385 | * |
386 | * This function frees a datagram skbuff that was received by | |
387 | * skb_recv_datagram. The flags argument must match the one | |
388 | * used for skb_recv_datagram. | |
389 | * | |
390 | * If the MSG_PEEK flag is set, and the packet is still on the | |
391 | * receive queue of the socket, it will be taken off the queue | |
392 | * before it is freed. | |
393 | * | |
394 | * This function currently only disables BH when acquiring the | |
395 | * sk_receive_queue lock. Therefore it must not be used in a | |
396 | * context where that lock is acquired in an IRQ context. | |
27ab2568 HX |
397 | * |
398 | * It returns 0 if the packet was removed by us. | |
3305b80c HX |
399 | */ |
400 | ||
27ab2568 | 401 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) |
3305b80c | 402 | { |
65101aec PA |
403 | int err = __sk_queue_drop_skb(sk, &sk->sk_receive_queue, skb, flags, |
404 | NULL); | |
3305b80c | 405 | |
61de71c6 JD |
406 | kfree_skb(skb); |
407 | sk_mem_reclaim_partial(sk); | |
27ab2568 | 408 | return err; |
3305b80c | 409 | } |
3305b80c HX |
410 | EXPORT_SYMBOL(skb_kill_datagram); |
411 | ||
a8f820aa HX |
412 | /** |
413 | * skb_copy_datagram_iter - Copy a datagram to an iovec iterator. | |
414 | * @skb: buffer to copy | |
415 | * @offset: offset in the buffer to start copying from | |
416 | * @to: iovec iterator to copy to | |
417 | * @len: amount of data to copy from buffer to iovec | |
418 | */ | |
419 | int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, | |
420 | struct iov_iter *to, int len) | |
421 | { | |
422 | int start = skb_headlen(skb); | |
32786821 | 423 | int i, copy = start - offset, start_off = offset, n; |
a8f820aa HX |
424 | struct sk_buff *frag_iter; |
425 | ||
426 | trace_skb_copy_datagram_iovec(skb, len); | |
427 | ||
428 | /* Copy header. */ | |
429 | if (copy > 0) { | |
430 | if (copy > len) | |
431 | copy = len; | |
32786821 AV |
432 | n = copy_to_iter(skb->data + offset, copy, to); |
433 | offset += n; | |
434 | if (n != copy) | |
a8f820aa HX |
435 | goto short_copy; |
436 | if ((len -= copy) == 0) | |
437 | return 0; | |
a8f820aa HX |
438 | } |
439 | ||
440 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
441 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
442 | int end; | |
443 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
444 | ||
445 | WARN_ON(start > offset + len); | |
446 | ||
447 | end = start + skb_frag_size(frag); | |
448 | if ((copy = end - offset) > 0) { | |
449 | if (copy > len) | |
450 | copy = len; | |
32786821 | 451 | n = copy_page_to_iter(skb_frag_page(frag), |
a8f820aa | 452 | frag->page_offset + offset - |
32786821 AV |
453 | start, copy, to); |
454 | offset += n; | |
455 | if (n != copy) | |
a8f820aa HX |
456 | goto short_copy; |
457 | if (!(len -= copy)) | |
458 | return 0; | |
a8f820aa HX |
459 | } |
460 | start = end; | |
461 | } | |
462 | ||
463 | skb_walk_frags(skb, frag_iter) { | |
464 | int end; | |
465 | ||
466 | WARN_ON(start > offset + len); | |
467 | ||
468 | end = start + frag_iter->len; | |
469 | if ((copy = end - offset) > 0) { | |
470 | if (copy > len) | |
471 | copy = len; | |
472 | if (skb_copy_datagram_iter(frag_iter, offset - start, | |
473 | to, copy)) | |
474 | goto fault; | |
475 | if ((len -= copy) == 0) | |
476 | return 0; | |
477 | offset += copy; | |
478 | } | |
479 | start = end; | |
480 | } | |
481 | if (!len) | |
482 | return 0; | |
483 | ||
484 | /* This is not really a user copy fault, but rather someone | |
485 | * gave us a bogus length on the skb. We should probably | |
486 | * print a warning here as it may indicate a kernel bug. | |
487 | */ | |
488 | ||
489 | fault: | |
32786821 | 490 | iov_iter_revert(to, offset - start_off); |
a8f820aa HX |
491 | return -EFAULT; |
492 | ||
493 | short_copy: | |
494 | if (iov_iter_count(to)) | |
495 | goto fault; | |
496 | ||
497 | return 0; | |
498 | } | |
499 | EXPORT_SYMBOL(skb_copy_datagram_iter); | |
500 | ||
db543c1f | 501 | /** |
8feb2fb2 | 502 | * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter. |
db543c1f RR |
503 | * @skb: buffer to copy |
504 | * @offset: offset in the buffer to start copying to | |
8feb2fb2 | 505 | * @from: the copy source |
db543c1f RR |
506 | * @len: amount of data to copy to buffer from iovec |
507 | * | |
508 | * Returns 0 or -EFAULT. | |
db543c1f | 509 | */ |
3a654f97 AV |
510 | int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset, |
511 | struct iov_iter *from, | |
512 | int len) | |
513 | { | |
514 | int start = skb_headlen(skb); | |
515 | int i, copy = start - offset; | |
516 | struct sk_buff *frag_iter; | |
517 | ||
518 | /* Copy header. */ | |
519 | if (copy > 0) { | |
520 | if (copy > len) | |
521 | copy = len; | |
522 | if (copy_from_iter(skb->data + offset, copy, from) != copy) | |
523 | goto fault; | |
524 | if ((len -= copy) == 0) | |
525 | return 0; | |
526 | offset += copy; | |
527 | } | |
528 | ||
529 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
530 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
531 | int end; | |
532 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
533 | ||
534 | WARN_ON(start > offset + len); | |
535 | ||
536 | end = start + skb_frag_size(frag); | |
537 | if ((copy = end - offset) > 0) { | |
538 | size_t copied; | |
539 | ||
540 | if (copy > len) | |
541 | copy = len; | |
542 | copied = copy_page_from_iter(skb_frag_page(frag), | |
543 | frag->page_offset + offset - start, | |
544 | copy, from); | |
545 | if (copied != copy) | |
546 | goto fault; | |
547 | ||
548 | if (!(len -= copy)) | |
549 | return 0; | |
550 | offset += copy; | |
551 | } | |
552 | start = end; | |
553 | } | |
554 | ||
555 | skb_walk_frags(skb, frag_iter) { | |
556 | int end; | |
557 | ||
558 | WARN_ON(start > offset + len); | |
559 | ||
560 | end = start + frag_iter->len; | |
561 | if ((copy = end - offset) > 0) { | |
562 | if (copy > len) | |
563 | copy = len; | |
564 | if (skb_copy_datagram_from_iter(frag_iter, | |
565 | offset - start, | |
566 | from, copy)) | |
567 | goto fault; | |
568 | if ((len -= copy) == 0) | |
569 | return 0; | |
570 | offset += copy; | |
571 | } | |
572 | start = end; | |
573 | } | |
574 | if (!len) | |
575 | return 0; | |
576 | ||
577 | fault: | |
578 | return -EFAULT; | |
579 | } | |
580 | EXPORT_SYMBOL(skb_copy_datagram_from_iter); | |
581 | ||
c3bdeb5c | 582 | /** |
195e952d | 583 | * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter |
c3bdeb5c | 584 | * @skb: buffer to copy |
195e952d | 585 | * @from: the source to copy from |
c3bdeb5c JW |
586 | * |
587 | * The function will first copy up to headlen, and then pin the userspace | |
588 | * pages and build frags through them. | |
589 | * | |
590 | * Returns 0, -EFAULT or -EMSGSIZE. | |
c3bdeb5c | 591 | */ |
3a654f97 AV |
592 | int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from) |
593 | { | |
594 | int len = iov_iter_count(from); | |
595 | int copy = min_t(int, skb_headlen(skb), len); | |
596 | int frag = 0; | |
597 | ||
598 | /* copy up to skb headlen */ | |
599 | if (skb_copy_datagram_from_iter(skb, 0, from, copy)) | |
600 | return -EFAULT; | |
601 | ||
602 | while (iov_iter_count(from)) { | |
603 | struct page *pages[MAX_SKB_FRAGS]; | |
604 | size_t start; | |
605 | ssize_t copied; | |
606 | unsigned long truesize; | |
607 | int n = 0; | |
608 | ||
609 | if (frag == MAX_SKB_FRAGS) | |
610 | return -EMSGSIZE; | |
611 | ||
612 | copied = iov_iter_get_pages(from, pages, ~0U, | |
613 | MAX_SKB_FRAGS - frag, &start); | |
614 | if (copied < 0) | |
615 | return -EFAULT; | |
616 | ||
617 | iov_iter_advance(from, copied); | |
618 | ||
619 | truesize = PAGE_ALIGN(copied + start); | |
620 | skb->data_len += copied; | |
621 | skb->len += copied; | |
622 | skb->truesize += truesize; | |
14afee4b | 623 | refcount_add(truesize, &skb->sk->sk_wmem_alloc); |
3a654f97 AV |
624 | while (copied) { |
625 | int size = min_t(int, copied, PAGE_SIZE - start); | |
626 | skb_fill_page_desc(skb, frag++, pages[n], start, size); | |
627 | start = 0; | |
628 | copied -= size; | |
629 | n++; | |
630 | } | |
631 | } | |
632 | return 0; | |
633 | } | |
634 | EXPORT_SYMBOL(zerocopy_sg_from_iter); | |
635 | ||
1da177e4 | 636 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
e5a4b0bb | 637 | struct iov_iter *to, int len, |
5084205f | 638 | __wsum *csump) |
1da177e4 | 639 | { |
1a028e50 | 640 | int start = skb_headlen(skb); |
32786821 | 641 | int i, copy = start - offset, start_off = offset; |
5b1a002a DM |
642 | struct sk_buff *frag_iter; |
643 | int pos = 0; | |
e5a4b0bb | 644 | int n; |
1da177e4 LT |
645 | |
646 | /* Copy header. */ | |
647 | if (copy > 0) { | |
1da177e4 LT |
648 | if (copy > len) |
649 | copy = len; | |
e5a4b0bb | 650 | n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to); |
32786821 | 651 | offset += n; |
e5a4b0bb | 652 | if (n != copy) |
1da177e4 LT |
653 | goto fault; |
654 | if ((len -= copy) == 0) | |
655 | return 0; | |
1da177e4 LT |
656 | pos = copy; |
657 | } | |
658 | ||
659 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1a028e50 | 660 | int end; |
9e903e08 | 661 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
1da177e4 | 662 | |
547b792c | 663 | WARN_ON(start > offset + len); |
1a028e50 | 664 | |
9e903e08 | 665 | end = start + skb_frag_size(frag); |
1da177e4 | 666 | if ((copy = end - offset) > 0) { |
e5a4b0bb | 667 | __wsum csum2 = 0; |
ea2ab693 | 668 | struct page *page = skb_frag_page(frag); |
e5a4b0bb | 669 | u8 *vaddr = kmap(page); |
1da177e4 LT |
670 | |
671 | if (copy > len) | |
672 | copy = len; | |
e5a4b0bb AV |
673 | n = csum_and_copy_to_iter(vaddr + frag->page_offset + |
674 | offset - start, copy, | |
675 | &csum2, to); | |
1da177e4 | 676 | kunmap(page); |
32786821 | 677 | offset += n; |
e5a4b0bb | 678 | if (n != copy) |
1da177e4 LT |
679 | goto fault; |
680 | *csump = csum_block_add(*csump, csum2, pos); | |
681 | if (!(len -= copy)) | |
682 | return 0; | |
1da177e4 LT |
683 | pos += copy; |
684 | } | |
1a028e50 | 685 | start = end; |
1da177e4 LT |
686 | } |
687 | ||
5b1a002a DM |
688 | skb_walk_frags(skb, frag_iter) { |
689 | int end; | |
690 | ||
691 | WARN_ON(start > offset + len); | |
692 | ||
693 | end = start + frag_iter->len; | |
694 | if ((copy = end - offset) > 0) { | |
695 | __wsum csum2 = 0; | |
696 | if (copy > len) | |
697 | copy = len; | |
698 | if (skb_copy_and_csum_datagram(frag_iter, | |
699 | offset - start, | |
700 | to, copy, | |
701 | &csum2)) | |
702 | goto fault; | |
703 | *csump = csum_block_add(*csump, csum2, pos); | |
704 | if ((len -= copy) == 0) | |
705 | return 0; | |
706 | offset += copy; | |
5b1a002a | 707 | pos += copy; |
1da177e4 | 708 | } |
5b1a002a | 709 | start = end; |
1da177e4 LT |
710 | } |
711 | if (!len) | |
712 | return 0; | |
713 | ||
714 | fault: | |
32786821 | 715 | iov_iter_revert(to, offset - start_off); |
1da177e4 LT |
716 | return -EFAULT; |
717 | } | |
718 | ||
759e5d00 | 719 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
fb286bb2 | 720 | { |
d3bc23e7 | 721 | __sum16 sum; |
fb286bb2 | 722 | |
759e5d00 | 723 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
46fb51eb TH |
724 | if (likely(!sum)) { |
725 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
726 | !skb->csum_complete_sw) | |
727 | netdev_rx_csum_fault(skb->dev); | |
728 | } | |
89c22d8c HX |
729 | if (!skb_shared(skb)) |
730 | skb->csum_valid = !sum; | |
fb286bb2 HX |
731 | return sum; |
732 | } | |
759e5d00 HX |
733 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
734 | ||
735 | __sum16 __skb_checksum_complete(struct sk_buff *skb) | |
736 | { | |
46fb51eb TH |
737 | __wsum csum; |
738 | __sum16 sum; | |
739 | ||
740 | csum = skb_checksum(skb, 0, skb->len, 0); | |
741 | ||
742 | /* skb->csum holds pseudo checksum */ | |
743 | sum = csum_fold(csum_add(skb->csum, csum)); | |
744 | if (likely(!sum)) { | |
745 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
746 | !skb->csum_complete_sw) | |
747 | netdev_rx_csum_fault(skb->dev); | |
748 | } | |
749 | ||
89c22d8c HX |
750 | if (!skb_shared(skb)) { |
751 | /* Save full packet checksum */ | |
752 | skb->csum = csum; | |
753 | skb->ip_summed = CHECKSUM_COMPLETE; | |
754 | skb->csum_complete_sw = 1; | |
755 | skb->csum_valid = !sum; | |
756 | } | |
46fb51eb TH |
757 | |
758 | return sum; | |
759e5d00 | 759 | } |
fb286bb2 HX |
760 | EXPORT_SYMBOL(__skb_checksum_complete); |
761 | ||
1da177e4 | 762 | /** |
e5a4b0bb | 763 | * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec. |
4dc3b16b PP |
764 | * @skb: skbuff |
765 | * @hlen: hardware length | |
e5a4b0bb | 766 | * @msg: destination |
4ec93edb | 767 | * |
1da177e4 LT |
768 | * Caller _must_ check that skb will fit to this iovec. |
769 | * | |
770 | * Returns: 0 - success. | |
771 | * -EINVAL - checksum failure. | |
e5a4b0bb | 772 | * -EFAULT - fault during copy. |
1da177e4 | 773 | */ |
e5a4b0bb AV |
774 | int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, |
775 | int hlen, struct msghdr *msg) | |
1da177e4 | 776 | { |
d3bc23e7 | 777 | __wsum csum; |
1da177e4 LT |
778 | int chunk = skb->len - hlen; |
779 | ||
ef8aef55 HX |
780 | if (!chunk) |
781 | return 0; | |
782 | ||
01e97e65 | 783 | if (msg_data_left(msg) < chunk) { |
fb286bb2 | 784 | if (__skb_checksum_complete(skb)) |
a6a59932 | 785 | return -EINVAL; |
e5a4b0bb | 786 | if (skb_copy_datagram_msg(skb, hlen, msg, chunk)) |
1da177e4 LT |
787 | goto fault; |
788 | } else { | |
789 | csum = csum_partial(skb->data, hlen, skb->csum); | |
e5a4b0bb | 790 | if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter, |
1da177e4 LT |
791 | chunk, &csum)) |
792 | goto fault; | |
a6a59932 DT |
793 | |
794 | if (csum_fold(csum)) { | |
795 | iov_iter_revert(&msg->msg_iter, chunk); | |
796 | return -EINVAL; | |
797 | } | |
798 | ||
84fa7933 | 799 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
fb286bb2 | 800 | netdev_rx_csum_fault(skb->dev); |
1da177e4 LT |
801 | } |
802 | return 0; | |
1da177e4 LT |
803 | fault: |
804 | return -EFAULT; | |
805 | } | |
e5a4b0bb | 806 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg); |
1da177e4 LT |
807 | |
808 | /** | |
809 | * datagram_poll - generic datagram poll | |
4dc3b16b PP |
810 | * @file: file struct |
811 | * @sock: socket | |
812 | * @wait: poll table | |
1da177e4 LT |
813 | * |
814 | * Datagram poll: Again totally generic. This also handles | |
815 | * sequenced packet sockets providing the socket receive queue | |
816 | * is only ever holding data ready to receive. | |
817 | * | |
d3f6cd9e | 818 | * Note: when you *don't* use this routine for this protocol, |
1da177e4 LT |
819 | * and you use a different write policy from sock_writeable() |
820 | * then please supply your own write_space callback. | |
821 | */ | |
822 | unsigned int datagram_poll(struct file *file, struct socket *sock, | |
823 | poll_table *wait) | |
824 | { | |
825 | struct sock *sk = sock->sk; | |
826 | unsigned int mask; | |
827 | ||
aa395145 | 828 | sock_poll_wait(file, sk_sleep(sk), wait); |
1da177e4 LT |
829 | mask = 0; |
830 | ||
831 | /* exceptional events? */ | |
832 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | |
7d4c04fc | 833 | mask |= POLLERR | |
8facd5fb | 834 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
7d4c04fc | 835 | |
f348d70a | 836 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
db40980f | 837 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
1da177e4 LT |
838 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
839 | mask |= POLLHUP; | |
840 | ||
841 | /* readable? */ | |
db40980f | 842 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
1da177e4 LT |
843 | mask |= POLLIN | POLLRDNORM; |
844 | ||
845 | /* Connection-based need to check for termination and startup */ | |
846 | if (connection_based(sk)) { | |
847 | if (sk->sk_state == TCP_CLOSE) | |
848 | mask |= POLLHUP; | |
849 | /* connection hasn't started yet? */ | |
850 | if (sk->sk_state == TCP_SYN_SENT) | |
851 | return mask; | |
852 | } | |
853 | ||
854 | /* writable? */ | |
855 | if (sock_writeable(sk)) | |
856 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | |
857 | else | |
9cd3e072 | 858 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
1da177e4 LT |
859 | |
860 | return mask; | |
861 | } | |
1da177e4 | 862 | EXPORT_SYMBOL(datagram_poll); |