]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * SUCS NET3: | |
4 | * | |
5 | * Generic datagram handling routines. These are generic for all | |
6 | * protocols. Possibly a generic IP version on top of these would | |
7 | * make sense. Not tonight however 8-). | |
8 | * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and | |
9 | * NetROM layer all have identical poll code and mostly | |
10 | * identical recvmsg() code. So we share it here. The poll was | |
11 | * shared before but buried in udp.c so I moved it. | |
12 | * | |
113aa838 | 13 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old |
1da177e4 LT |
14 | * udp.c code) |
15 | * | |
16 | * Fixes: | |
17 | * Alan Cox : NULL return from skb_peek_copy() | |
18 | * understood | |
19 | * Alan Cox : Rewrote skb_read_datagram to avoid the | |
20 | * skb_peek_copy stuff. | |
21 | * Alan Cox : Added support for SOCK_SEQPACKET. | |
22 | * IPX can no longer use the SO_TYPE hack | |
23 | * but AX.25 now works right, and SPX is | |
24 | * feasible. | |
25 | * Alan Cox : Fixed write poll of non IP protocol | |
26 | * crash. | |
27 | * Florian La Roche: Changed for my new skbuff handling. | |
28 | * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET. | |
29 | * Linus Torvalds : BSD semantic fixes. | |
30 | * Alan Cox : Datagram iovec handling | |
31 | * Darryl Miles : Fixed non-blocking SOCK_STREAM. | |
32 | * Alan Cox : POSIXisms | |
33 | * Pete Wyckoff : Unconnected accept() fix. | |
34 | * | |
35 | */ | |
36 | ||
37 | #include <linux/module.h> | |
38 | #include <linux/types.h> | |
39 | #include <linux/kernel.h> | |
7c0f6ba6 | 40 | #include <linux/uaccess.h> |
1da177e4 LT |
41 | #include <linux/mm.h> |
42 | #include <linux/interrupt.h> | |
43 | #include <linux/errno.h> | |
44 | #include <linux/sched.h> | |
45 | #include <linux/inet.h> | |
1da177e4 LT |
46 | #include <linux/netdevice.h> |
47 | #include <linux/rtnetlink.h> | |
48 | #include <linux/poll.h> | |
49 | #include <linux/highmem.h> | |
3305b80c | 50 | #include <linux/spinlock.h> |
5a0e3ad6 | 51 | #include <linux/slab.h> |
0433547a | 52 | #include <linux/pagemap.h> |
a8f820aa | 53 | #include <linux/uio.h> |
1da177e4 LT |
54 | |
55 | #include <net/protocol.h> | |
56 | #include <linux/skbuff.h> | |
1da177e4 | 57 | |
c752f073 ACM |
58 | #include <net/checksum.h> |
59 | #include <net/sock.h> | |
60 | #include <net/tcp_states.h> | |
e9b3cc1b | 61 | #include <trace/events/skb.h> |
076bb0c8 | 62 | #include <net/busy_poll.h> |
1da177e4 LT |
63 | |
64 | /* | |
65 | * Is a socket 'connection oriented' ? | |
66 | */ | |
67 | static inline int connection_based(struct sock *sk) | |
68 | { | |
69 | return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM; | |
70 | } | |
71 | ||
ac6424b9 | 72 | static int receiver_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync, |
bf368e4e ED |
73 | void *key) |
74 | { | |
bf368e4e ED |
75 | /* |
76 | * Avoid a wakeup if event not interesting for us | |
77 | */ | |
a9a08845 | 78 | if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR))) |
bf368e4e ED |
79 | return 0; |
80 | return autoremove_wake_function(wait, mode, sync, key); | |
81 | } | |
1da177e4 | 82 | /* |
39cc8613 | 83 | * Wait for the last received packet to be different from skb |
1da177e4 | 84 | */ |
ea3793ee RW |
85 | int __skb_wait_for_more_packets(struct sock *sk, int *err, long *timeo_p, |
86 | const struct sk_buff *skb) | |
1da177e4 LT |
87 | { |
88 | int error; | |
bf368e4e | 89 | DEFINE_WAIT_FUNC(wait, receiver_wake_function); |
1da177e4 | 90 | |
aa395145 | 91 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
1da177e4 LT |
92 | |
93 | /* Socket errors? */ | |
94 | error = sock_error(sk); | |
95 | if (error) | |
96 | goto out_err; | |
97 | ||
39cc8613 | 98 | if (sk->sk_receive_queue.prev != skb) |
1da177e4 LT |
99 | goto out; |
100 | ||
101 | /* Socket shut down? */ | |
102 | if (sk->sk_shutdown & RCV_SHUTDOWN) | |
103 | goto out_noerr; | |
104 | ||
105 | /* Sequenced packets can come disconnected. | |
106 | * If so we report the problem | |
107 | */ | |
108 | error = -ENOTCONN; | |
109 | if (connection_based(sk) && | |
110 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) | |
111 | goto out_err; | |
112 | ||
113 | /* handle signals */ | |
114 | if (signal_pending(current)) | |
115 | goto interrupted; | |
116 | ||
117 | error = 0; | |
118 | *timeo_p = schedule_timeout(*timeo_p); | |
119 | out: | |
aa395145 | 120 | finish_wait(sk_sleep(sk), &wait); |
1da177e4 LT |
121 | return error; |
122 | interrupted: | |
123 | error = sock_intr_errno(*timeo_p); | |
124 | out_err: | |
125 | *err = error; | |
126 | goto out; | |
127 | out_noerr: | |
128 | *err = 0; | |
129 | error = 1; | |
130 | goto out; | |
131 | } | |
ea3793ee | 132 | EXPORT_SYMBOL(__skb_wait_for_more_packets); |
1da177e4 | 133 | |
a0a2a660 | 134 | static struct sk_buff *skb_set_peeked(struct sk_buff *skb) |
738ac1eb HX |
135 | { |
136 | struct sk_buff *nskb; | |
137 | ||
138 | if (skb->peeked) | |
a0a2a660 | 139 | return skb; |
738ac1eb HX |
140 | |
141 | /* We have to unshare an skb before modifying it. */ | |
142 | if (!skb_shared(skb)) | |
143 | goto done; | |
144 | ||
145 | nskb = skb_clone(skb, GFP_ATOMIC); | |
146 | if (!nskb) | |
a0a2a660 | 147 | return ERR_PTR(-ENOMEM); |
738ac1eb HX |
148 | |
149 | skb->prev->next = nskb; | |
150 | skb->next->prev = nskb; | |
151 | nskb->prev = skb->prev; | |
152 | nskb->next = skb->next; | |
153 | ||
154 | consume_skb(skb); | |
155 | skb = nskb; | |
156 | ||
157 | done: | |
158 | skb->peeked = 1; | |
159 | ||
a0a2a660 | 160 | return skb; |
738ac1eb HX |
161 | } |
162 | ||
65101aec PA |
163 | struct sk_buff *__skb_try_recv_from_queue(struct sock *sk, |
164 | struct sk_buff_head *queue, | |
165 | unsigned int flags, | |
166 | void (*destructor)(struct sock *sk, | |
167 | struct sk_buff *skb), | |
168 | int *peeked, int *off, int *err, | |
169 | struct sk_buff **last) | |
170 | { | |
a0917e0b | 171 | bool peek_at_off = false; |
65101aec | 172 | struct sk_buff *skb; |
a0917e0b MD |
173 | int _off = 0; |
174 | ||
175 | if (unlikely(flags & MSG_PEEK && *off >= 0)) { | |
176 | peek_at_off = true; | |
177 | _off = *off; | |
178 | } | |
65101aec PA |
179 | |
180 | *last = queue->prev; | |
181 | skb_queue_walk(queue, skb) { | |
182 | if (flags & MSG_PEEK) { | |
a0917e0b MD |
183 | if (peek_at_off && _off >= skb->len && |
184 | (_off || skb->peeked)) { | |
de321ed3 | 185 | _off -= skb->len; |
65101aec PA |
186 | continue; |
187 | } | |
188 | if (!skb->len) { | |
189 | skb = skb_set_peeked(skb); | |
98e4fcff | 190 | if (IS_ERR(skb)) { |
65101aec | 191 | *err = PTR_ERR(skb); |
de321ed3 | 192 | return NULL; |
65101aec PA |
193 | } |
194 | } | |
195 | *peeked = 1; | |
63354797 | 196 | refcount_inc(&skb->users); |
65101aec PA |
197 | } else { |
198 | __skb_unlink(skb, queue); | |
199 | if (destructor) | |
200 | destructor(sk, skb); | |
201 | } | |
de321ed3 | 202 | *off = _off; |
65101aec PA |
203 | return skb; |
204 | } | |
205 | return NULL; | |
206 | } | |
207 | ||
1da177e4 | 208 | /** |
ea3793ee | 209 | * __skb_try_recv_datagram - Receive a datagram skbuff |
4dc3b16b | 210 | * @sk: socket |
d3f6cd9e | 211 | * @flags: MSG\_ flags |
7c13f97f | 212 | * @destructor: invoked under the receive lock on successful dequeue |
39cc8613 | 213 | * @peeked: returns non-zero if this packet has been seen before |
3f518bf7 PE |
214 | * @off: an offset in bytes to peek skb from. Returns an offset |
215 | * within an skb where data actually starts | |
4dc3b16b | 216 | * @err: error code returned |
ea3793ee RW |
217 | * @last: set to last peeked message to inform the wait function |
218 | * what to look for when peeking | |
1da177e4 LT |
219 | * |
220 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups | |
221 | * and possible races. This replaces identical code in packet, raw and | |
222 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes | |
223 | * the long standing peek and read race for datagram sockets. If you | |
224 | * alter this routine remember it must be re-entrant. | |
225 | * | |
ea3793ee RW |
226 | * This function will lock the socket if a skb is returned, so |
227 | * the caller needs to unlock the socket in that case (usually by | |
d651983d | 228 | * calling skb_free_datagram). Returns NULL with @err set to |
ea3793ee RW |
229 | * -EAGAIN if no data was available or to some other value if an |
230 | * error was detected. | |
1da177e4 LT |
231 | * |
232 | * * It does not lock socket since today. This function is | |
233 | * * free of race conditions. This measure should/can improve | |
234 | * * significantly datagram socket latencies at high loads, | |
235 | * * when data copying to user space takes lots of time. | |
236 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet | |
237 | * * 8) Great win.) | |
238 | * * --ANK (980729) | |
239 | * | |
240 | * The order of the tests when we find no data waiting are specified | |
241 | * quite explicitly by POSIX 1003.1g, don't change them without having | |
242 | * the standard around please. | |
243 | */ | |
ea3793ee | 244 | struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags, |
7c13f97f PA |
245 | void (*destructor)(struct sock *sk, |
246 | struct sk_buff *skb), | |
ea3793ee RW |
247 | int *peeked, int *off, int *err, |
248 | struct sk_buff **last) | |
1da177e4 | 249 | { |
738ac1eb | 250 | struct sk_buff_head *queue = &sk->sk_receive_queue; |
ea3793ee | 251 | struct sk_buff *skb; |
738ac1eb | 252 | unsigned long cpu_flags; |
1da177e4 LT |
253 | /* |
254 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() | |
255 | */ | |
256 | int error = sock_error(sk); | |
257 | ||
258 | if (error) | |
259 | goto no_packet; | |
260 | ||
a297569f | 261 | *peeked = 0; |
1da177e4 LT |
262 | do { |
263 | /* Again only user level code calls this function, so nothing | |
264 | * interrupt level will suddenly eat the receive_queue. | |
265 | * | |
266 | * Look at current nfs client by the way... | |
8917a3c0 | 267 | * However, this function was correct in any case. 8) |
1da177e4 | 268 | */ |
4934b032 | 269 | spin_lock_irqsave(&queue->lock, cpu_flags); |
65101aec | 270 | skb = __skb_try_recv_from_queue(sk, queue, flags, destructor, |
de321ed3 | 271 | peeked, off, &error, last); |
3f518bf7 | 272 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
de321ed3 AV |
273 | if (error) |
274 | goto no_packet; | |
65101aec PA |
275 | if (skb) |
276 | return skb; | |
2b5cd0df AD |
277 | |
278 | if (!sk_can_busy_loop(sk)) | |
279 | break; | |
280 | ||
281 | sk_busy_loop(sk, flags & MSG_DONTWAIT); | |
282 | } while (!skb_queue_empty(&sk->sk_receive_queue)); | |
1da177e4 | 283 | |
ea3793ee | 284 | error = -EAGAIN; |
a5b50476 | 285 | |
ea3793ee RW |
286 | no_packet: |
287 | *err = error; | |
288 | return NULL; | |
289 | } | |
290 | EXPORT_SYMBOL(__skb_try_recv_datagram); | |
1da177e4 | 291 | |
ea3793ee | 292 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags, |
7c13f97f PA |
293 | void (*destructor)(struct sock *sk, |
294 | struct sk_buff *skb), | |
ea3793ee RW |
295 | int *peeked, int *off, int *err) |
296 | { | |
297 | struct sk_buff *skb, *last; | |
298 | long timeo; | |
1da177e4 | 299 | |
ea3793ee RW |
300 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
301 | ||
302 | do { | |
7c13f97f PA |
303 | skb = __skb_try_recv_datagram(sk, flags, destructor, peeked, |
304 | off, err, &last); | |
ea3793ee RW |
305 | if (skb) |
306 | return skb; | |
307 | ||
760a4322 | 308 | if (*err != -EAGAIN) |
ea3793ee RW |
309 | break; |
310 | } while (timeo && | |
311 | !__skb_wait_for_more_packets(sk, err, &timeo, last)); | |
1da177e4 | 312 | |
1da177e4 LT |
313 | return NULL; |
314 | } | |
a59322be HX |
315 | EXPORT_SYMBOL(__skb_recv_datagram); |
316 | ||
95c96174 | 317 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, |
a59322be HX |
318 | int noblock, int *err) |
319 | { | |
3f518bf7 | 320 | int peeked, off = 0; |
a59322be HX |
321 | |
322 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), | |
7c13f97f | 323 | NULL, &peeked, &off, err); |
a59322be | 324 | } |
9e34a5b5 | 325 | EXPORT_SYMBOL(skb_recv_datagram); |
1da177e4 LT |
326 | |
327 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) | |
328 | { | |
ead2ceb0 | 329 | consume_skb(skb); |
270acefa | 330 | sk_mem_reclaim_partial(sk); |
1da177e4 | 331 | } |
9d410c79 ED |
332 | EXPORT_SYMBOL(skb_free_datagram); |
333 | ||
627d2d6b | 334 | void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len) |
9d410c79 | 335 | { |
8a74ad60 ED |
336 | bool slow; |
337 | ||
3889a803 | 338 | if (!skb_unref(skb)) { |
627d2d6b | 339 | sk_peek_offset_bwd(sk, len); |
93bb64ea | 340 | return; |
627d2d6b | 341 | } |
93bb64ea | 342 | |
8a74ad60 | 343 | slow = lock_sock_fast(sk); |
627d2d6b | 344 | sk_peek_offset_bwd(sk, len); |
4b0b72f7 ED |
345 | skb_orphan(skb); |
346 | sk_mem_reclaim_partial(sk); | |
8a74ad60 | 347 | unlock_sock_fast(sk, slow); |
4b0b72f7 | 348 | |
93bb64ea ED |
349 | /* skb is now orphaned, can be freed outside of locked section */ |
350 | __kfree_skb(skb); | |
9d410c79 | 351 | } |
627d2d6b | 352 | EXPORT_SYMBOL(__skb_free_datagram_locked); |
1da177e4 | 353 | |
65101aec PA |
354 | int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue, |
355 | struct sk_buff *skb, unsigned int flags, | |
69629464 ED |
356 | void (*destructor)(struct sock *sk, |
357 | struct sk_buff *skb)) | |
f8c3bf00 PA |
358 | { |
359 | int err = 0; | |
360 | ||
361 | if (flags & MSG_PEEK) { | |
362 | err = -ENOENT; | |
65101aec | 363 | spin_lock_bh(&sk_queue->lock); |
fd6055a8 | 364 | if (skb->next) { |
65101aec | 365 | __skb_unlink(skb, sk_queue); |
63354797 | 366 | refcount_dec(&skb->users); |
69629464 ED |
367 | if (destructor) |
368 | destructor(sk, skb); | |
f8c3bf00 PA |
369 | err = 0; |
370 | } | |
65101aec | 371 | spin_unlock_bh(&sk_queue->lock); |
f8c3bf00 PA |
372 | } |
373 | ||
374 | atomic_inc(&sk->sk_drops); | |
375 | return err; | |
376 | } | |
377 | EXPORT_SYMBOL(__sk_queue_drop_skb); | |
378 | ||
3305b80c HX |
379 | /** |
380 | * skb_kill_datagram - Free a datagram skbuff forcibly | |
381 | * @sk: socket | |
382 | * @skb: datagram skbuff | |
d3f6cd9e | 383 | * @flags: MSG\_ flags |
3305b80c HX |
384 | * |
385 | * This function frees a datagram skbuff that was received by | |
386 | * skb_recv_datagram. The flags argument must match the one | |
387 | * used for skb_recv_datagram. | |
388 | * | |
389 | * If the MSG_PEEK flag is set, and the packet is still on the | |
390 | * receive queue of the socket, it will be taken off the queue | |
391 | * before it is freed. | |
392 | * | |
393 | * This function currently only disables BH when acquiring the | |
394 | * sk_receive_queue lock. Therefore it must not be used in a | |
395 | * context where that lock is acquired in an IRQ context. | |
27ab2568 HX |
396 | * |
397 | * It returns 0 if the packet was removed by us. | |
3305b80c HX |
398 | */ |
399 | ||
27ab2568 | 400 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) |
3305b80c | 401 | { |
65101aec PA |
402 | int err = __sk_queue_drop_skb(sk, &sk->sk_receive_queue, skb, flags, |
403 | NULL); | |
3305b80c | 404 | |
61de71c6 JD |
405 | kfree_skb(skb); |
406 | sk_mem_reclaim_partial(sk); | |
27ab2568 | 407 | return err; |
3305b80c | 408 | } |
3305b80c HX |
409 | EXPORT_SYMBOL(skb_kill_datagram); |
410 | ||
950fcaec SG |
411 | int __skb_datagram_iter(const struct sk_buff *skb, int offset, |
412 | struct iov_iter *to, int len, bool fault_short, | |
413 | size_t (*cb)(const void *, size_t, void *, struct iov_iter *), | |
414 | void *data) | |
a8f820aa HX |
415 | { |
416 | int start = skb_headlen(skb); | |
32786821 | 417 | int i, copy = start - offset, start_off = offset, n; |
a8f820aa HX |
418 | struct sk_buff *frag_iter; |
419 | ||
a8f820aa HX |
420 | /* Copy header. */ |
421 | if (copy > 0) { | |
422 | if (copy > len) | |
423 | copy = len; | |
950fcaec | 424 | n = cb(skb->data + offset, copy, data, to); |
32786821 AV |
425 | offset += n; |
426 | if (n != copy) | |
a8f820aa HX |
427 | goto short_copy; |
428 | if ((len -= copy) == 0) | |
429 | return 0; | |
a8f820aa HX |
430 | } |
431 | ||
432 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
433 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
434 | int end; | |
435 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
436 | ||
437 | WARN_ON(start > offset + len); | |
438 | ||
439 | end = start + skb_frag_size(frag); | |
440 | if ((copy = end - offset) > 0) { | |
0fc07791 SG |
441 | struct page *page = skb_frag_page(frag); |
442 | u8 *vaddr = kmap(page); | |
443 | ||
a8f820aa HX |
444 | if (copy > len) |
445 | copy = len; | |
950fcaec SG |
446 | n = cb(vaddr + frag->page_offset + |
447 | offset - start, copy, data, to); | |
0fc07791 | 448 | kunmap(page); |
32786821 AV |
449 | offset += n; |
450 | if (n != copy) | |
a8f820aa HX |
451 | goto short_copy; |
452 | if (!(len -= copy)) | |
453 | return 0; | |
a8f820aa HX |
454 | } |
455 | start = end; | |
456 | } | |
457 | ||
458 | skb_walk_frags(skb, frag_iter) { | |
459 | int end; | |
460 | ||
461 | WARN_ON(start > offset + len); | |
462 | ||
463 | end = start + frag_iter->len; | |
464 | if ((copy = end - offset) > 0) { | |
465 | if (copy > len) | |
466 | copy = len; | |
950fcaec | 467 | if (__skb_datagram_iter(frag_iter, offset - start, |
65d69e25 | 468 | to, copy, fault_short, cb, data)) |
a8f820aa HX |
469 | goto fault; |
470 | if ((len -= copy) == 0) | |
471 | return 0; | |
472 | offset += copy; | |
473 | } | |
474 | start = end; | |
475 | } | |
476 | if (!len) | |
477 | return 0; | |
478 | ||
479 | /* This is not really a user copy fault, but rather someone | |
480 | * gave us a bogus length on the skb. We should probably | |
481 | * print a warning here as it may indicate a kernel bug. | |
482 | */ | |
483 | ||
484 | fault: | |
32786821 | 485 | iov_iter_revert(to, offset - start_off); |
a8f820aa HX |
486 | return -EFAULT; |
487 | ||
488 | short_copy: | |
950fcaec | 489 | if (fault_short || iov_iter_count(to)) |
a8f820aa HX |
490 | goto fault; |
491 | ||
492 | return 0; | |
493 | } | |
950fcaec | 494 | |
65d69e25 SG |
495 | /** |
496 | * skb_copy_and_hash_datagram_iter - Copy datagram to an iovec iterator | |
497 | * and update a hash. | |
498 | * @skb: buffer to copy | |
499 | * @offset: offset in the buffer to start copying from | |
500 | * @to: iovec iterator to copy to | |
501 | * @len: amount of data to copy from buffer to iovec | |
502 | * @hash: hash request to update | |
503 | */ | |
504 | int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset, | |
505 | struct iov_iter *to, int len, | |
506 | struct ahash_request *hash) | |
507 | { | |
508 | return __skb_datagram_iter(skb, offset, to, len, true, | |
509 | hash_and_copy_to_iter, hash); | |
510 | } | |
511 | EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter); | |
512 | ||
950fcaec SG |
513 | static size_t simple_copy_to_iter(const void *addr, size_t bytes, |
514 | void *data __always_unused, struct iov_iter *i) | |
515 | { | |
516 | return copy_to_iter(addr, bytes, i); | |
517 | } | |
518 | ||
519 | /** | |
520 | * skb_copy_datagram_iter - Copy a datagram to an iovec iterator. | |
521 | * @skb: buffer to copy | |
522 | * @offset: offset in the buffer to start copying from | |
523 | * @to: iovec iterator to copy to | |
524 | * @len: amount of data to copy from buffer to iovec | |
525 | */ | |
526 | int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, | |
527 | struct iov_iter *to, int len) | |
528 | { | |
529 | trace_skb_copy_datagram_iovec(skb, len); | |
530 | return __skb_datagram_iter(skb, offset, to, len, false, | |
531 | simple_copy_to_iter, NULL); | |
532 | } | |
a8f820aa HX |
533 | EXPORT_SYMBOL(skb_copy_datagram_iter); |
534 | ||
db543c1f | 535 | /** |
8feb2fb2 | 536 | * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter. |
db543c1f RR |
537 | * @skb: buffer to copy |
538 | * @offset: offset in the buffer to start copying to | |
8feb2fb2 | 539 | * @from: the copy source |
db543c1f RR |
540 | * @len: amount of data to copy to buffer from iovec |
541 | * | |
542 | * Returns 0 or -EFAULT. | |
db543c1f | 543 | */ |
3a654f97 AV |
544 | int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset, |
545 | struct iov_iter *from, | |
546 | int len) | |
547 | { | |
548 | int start = skb_headlen(skb); | |
549 | int i, copy = start - offset; | |
550 | struct sk_buff *frag_iter; | |
551 | ||
552 | /* Copy header. */ | |
553 | if (copy > 0) { | |
554 | if (copy > len) | |
555 | copy = len; | |
556 | if (copy_from_iter(skb->data + offset, copy, from) != copy) | |
557 | goto fault; | |
558 | if ((len -= copy) == 0) | |
559 | return 0; | |
560 | offset += copy; | |
561 | } | |
562 | ||
563 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | |
564 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
565 | int end; | |
566 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
567 | ||
568 | WARN_ON(start > offset + len); | |
569 | ||
570 | end = start + skb_frag_size(frag); | |
571 | if ((copy = end - offset) > 0) { | |
572 | size_t copied; | |
573 | ||
574 | if (copy > len) | |
575 | copy = len; | |
576 | copied = copy_page_from_iter(skb_frag_page(frag), | |
577 | frag->page_offset + offset - start, | |
578 | copy, from); | |
579 | if (copied != copy) | |
580 | goto fault; | |
581 | ||
582 | if (!(len -= copy)) | |
583 | return 0; | |
584 | offset += copy; | |
585 | } | |
586 | start = end; | |
587 | } | |
588 | ||
589 | skb_walk_frags(skb, frag_iter) { | |
590 | int end; | |
591 | ||
592 | WARN_ON(start > offset + len); | |
593 | ||
594 | end = start + frag_iter->len; | |
595 | if ((copy = end - offset) > 0) { | |
596 | if (copy > len) | |
597 | copy = len; | |
598 | if (skb_copy_datagram_from_iter(frag_iter, | |
599 | offset - start, | |
600 | from, copy)) | |
601 | goto fault; | |
602 | if ((len -= copy) == 0) | |
603 | return 0; | |
604 | offset += copy; | |
605 | } | |
606 | start = end; | |
607 | } | |
608 | if (!len) | |
609 | return 0; | |
610 | ||
611 | fault: | |
612 | return -EFAULT; | |
613 | } | |
614 | EXPORT_SYMBOL(skb_copy_datagram_from_iter); | |
615 | ||
52267790 WB |
616 | int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb, |
617 | struct iov_iter *from, size_t length) | |
3a654f97 | 618 | { |
52267790 | 619 | int frag = skb_shinfo(skb)->nr_frags; |
3a654f97 | 620 | |
52267790 | 621 | while (length && iov_iter_count(from)) { |
3a654f97 AV |
622 | struct page *pages[MAX_SKB_FRAGS]; |
623 | size_t start; | |
624 | ssize_t copied; | |
625 | unsigned long truesize; | |
626 | int n = 0; | |
627 | ||
628 | if (frag == MAX_SKB_FRAGS) | |
629 | return -EMSGSIZE; | |
630 | ||
52267790 | 631 | copied = iov_iter_get_pages(from, pages, length, |
3a654f97 AV |
632 | MAX_SKB_FRAGS - frag, &start); |
633 | if (copied < 0) | |
634 | return -EFAULT; | |
635 | ||
636 | iov_iter_advance(from, copied); | |
52267790 | 637 | length -= copied; |
3a654f97 AV |
638 | |
639 | truesize = PAGE_ALIGN(copied + start); | |
640 | skb->data_len += copied; | |
641 | skb->len += copied; | |
642 | skb->truesize += truesize; | |
52267790 WB |
643 | if (sk && sk->sk_type == SOCK_STREAM) { |
644 | sk->sk_wmem_queued += truesize; | |
645 | sk_mem_charge(sk, truesize); | |
646 | } else { | |
647 | refcount_add(truesize, &skb->sk->sk_wmem_alloc); | |
648 | } | |
3a654f97 AV |
649 | while (copied) { |
650 | int size = min_t(int, copied, PAGE_SIZE - start); | |
651 | skb_fill_page_desc(skb, frag++, pages[n], start, size); | |
652 | start = 0; | |
653 | copied -= size; | |
654 | n++; | |
655 | } | |
656 | } | |
657 | return 0; | |
658 | } | |
52267790 WB |
659 | EXPORT_SYMBOL(__zerocopy_sg_from_iter); |
660 | ||
661 | /** | |
662 | * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter | |
663 | * @skb: buffer to copy | |
664 | * @from: the source to copy from | |
665 | * | |
666 | * The function will first copy up to headlen, and then pin the userspace | |
667 | * pages and build frags through them. | |
668 | * | |
669 | * Returns 0, -EFAULT or -EMSGSIZE. | |
670 | */ | |
671 | int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from) | |
672 | { | |
673 | int copy = min_t(int, skb_headlen(skb), iov_iter_count(from)); | |
674 | ||
675 | /* copy up to skb headlen */ | |
676 | if (skb_copy_datagram_from_iter(skb, 0, from, copy)) | |
677 | return -EFAULT; | |
678 | ||
679 | return __zerocopy_sg_from_iter(NULL, skb, from, ~0U); | |
680 | } | |
3a654f97 AV |
681 | EXPORT_SYMBOL(zerocopy_sg_from_iter); |
682 | ||
950fcaec SG |
683 | /** |
684 | * skb_copy_and_csum_datagram_iter - Copy datagram to an iovec iterator | |
685 | * and update a checksum. | |
686 | * @skb: buffer to copy | |
687 | * @offset: offset in the buffer to start copying from | |
688 | * @to: iovec iterator to copy to | |
689 | * @len: amount of data to copy from buffer to iovec | |
690 | * @csump: checksum pointer | |
691 | */ | |
1da177e4 | 692 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
e5a4b0bb | 693 | struct iov_iter *to, int len, |
5084205f | 694 | __wsum *csump) |
1da177e4 | 695 | { |
950fcaec SG |
696 | return __skb_datagram_iter(skb, offset, to, len, true, |
697 | csum_and_copy_to_iter, csump); | |
1da177e4 LT |
698 | } |
699 | ||
700 | /** | |
e5a4b0bb | 701 | * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec. |
4dc3b16b PP |
702 | * @skb: skbuff |
703 | * @hlen: hardware length | |
e5a4b0bb | 704 | * @msg: destination |
4ec93edb | 705 | * |
1da177e4 LT |
706 | * Caller _must_ check that skb will fit to this iovec. |
707 | * | |
708 | * Returns: 0 - success. | |
709 | * -EINVAL - checksum failure. | |
e5a4b0bb | 710 | * -EFAULT - fault during copy. |
1da177e4 | 711 | */ |
e5a4b0bb AV |
712 | int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, |
713 | int hlen, struct msghdr *msg) | |
1da177e4 | 714 | { |
d3bc23e7 | 715 | __wsum csum; |
1da177e4 LT |
716 | int chunk = skb->len - hlen; |
717 | ||
ef8aef55 HX |
718 | if (!chunk) |
719 | return 0; | |
720 | ||
01e97e65 | 721 | if (msg_data_left(msg) < chunk) { |
fb286bb2 | 722 | if (__skb_checksum_complete(skb)) |
a6a59932 | 723 | return -EINVAL; |
e5a4b0bb | 724 | if (skb_copy_datagram_msg(skb, hlen, msg, chunk)) |
1da177e4 LT |
725 | goto fault; |
726 | } else { | |
727 | csum = csum_partial(skb->data, hlen, skb->csum); | |
e5a4b0bb | 728 | if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter, |
1da177e4 LT |
729 | chunk, &csum)) |
730 | goto fault; | |
a6a59932 DT |
731 | |
732 | if (csum_fold(csum)) { | |
733 | iov_iter_revert(&msg->msg_iter, chunk); | |
734 | return -EINVAL; | |
735 | } | |
736 | ||
db4f1be3 ST |
737 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
738 | !skb->csum_complete_sw) | |
7fe50ac8 | 739 | netdev_rx_csum_fault(NULL, skb); |
1da177e4 LT |
740 | } |
741 | return 0; | |
1da177e4 LT |
742 | fault: |
743 | return -EFAULT; | |
744 | } | |
e5a4b0bb | 745 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg); |
1da177e4 LT |
746 | |
747 | /** | |
748 | * datagram_poll - generic datagram poll | |
a11e1d43 | 749 | * @file: file struct |
4dc3b16b | 750 | * @sock: socket |
a11e1d43 | 751 | * @wait: poll table |
1da177e4 LT |
752 | * |
753 | * Datagram poll: Again totally generic. This also handles | |
754 | * sequenced packet sockets providing the socket receive queue | |
755 | * is only ever holding data ready to receive. | |
756 | * | |
d3f6cd9e | 757 | * Note: when you *don't* use this routine for this protocol, |
1da177e4 LT |
758 | * and you use a different write policy from sock_writeable() |
759 | * then please supply your own write_space callback. | |
760 | */ | |
a11e1d43 LT |
761 | __poll_t datagram_poll(struct file *file, struct socket *sock, |
762 | poll_table *wait) | |
1da177e4 LT |
763 | { |
764 | struct sock *sk = sock->sk; | |
a11e1d43 LT |
765 | __poll_t mask; |
766 | ||
89ab066d | 767 | sock_poll_wait(file, sock, wait); |
a11e1d43 | 768 | mask = 0; |
1da177e4 LT |
769 | |
770 | /* exceptional events? */ | |
771 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | |
a9a08845 LT |
772 | mask |= EPOLLERR | |
773 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); | |
7d4c04fc | 774 | |
f348d70a | 775 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
a9a08845 | 776 | mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
1da177e4 | 777 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
a9a08845 | 778 | mask |= EPOLLHUP; |
1da177e4 LT |
779 | |
780 | /* readable? */ | |
db40980f | 781 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
a9a08845 | 782 | mask |= EPOLLIN | EPOLLRDNORM; |
1da177e4 LT |
783 | |
784 | /* Connection-based need to check for termination and startup */ | |
785 | if (connection_based(sk)) { | |
786 | if (sk->sk_state == TCP_CLOSE) | |
a9a08845 | 787 | mask |= EPOLLHUP; |
1da177e4 LT |
788 | /* connection hasn't started yet? */ |
789 | if (sk->sk_state == TCP_SYN_SENT) | |
790 | return mask; | |
791 | } | |
792 | ||
793 | /* writable? */ | |
794 | if (sock_writeable(sk)) | |
a9a08845 | 795 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
1da177e4 | 796 | else |
9cd3e072 | 797 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
1da177e4 LT |
798 | |
799 | return mask; | |
800 | } | |
a11e1d43 | 801 | EXPORT_SYMBOL(datagram_poll); |