1 // SPDX-License-Identifier: GPL-2.0
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 *
13 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
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>
40 #include <linux/uaccess.h>
41 #include <linux/mm.h>
42 #include <linux/interrupt.h>
43 #include <linux/errno.h>
44 #include <linux/sched.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/rtnetlink.h>
48 #include <linux/poll.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/slab.h>
52 #include <linux/pagemap.h>
53 #include <linux/uio.h>
54 #include <linux/indirect_call_wrapper.h>
55
56 #include <net/protocol.h>
57 #include <linux/skbuff.h>
58
59 #include <net/checksum.h>
60 #include <net/sock.h>
61 #include <net/tcp_states.h>
62 #include <trace/events/skb.h>
63 #include <net/busy_poll.h>
64
65 /*
66 * Is a socket 'connection oriented' ?
67 */
connection_based(struct sock * sk)68 static inline int connection_based(struct sock *sk)
69 {
70 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
71 }
72
receiver_wake_function(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)73 static int receiver_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync,
74 void *key)
75 {
76 /*
77 * Avoid a wakeup if event not interesting for us
78 */
79 if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR)))
80 return 0;
81 return autoremove_wake_function(wait, mode, sync, key);
82 }
83 /*
84 * Wait for the last received packet to be different from skb
85 */
__skb_wait_for_more_packets(struct sock * sk,struct sk_buff_head * queue,int * err,long * timeo_p,const struct sk_buff * skb)86 int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue,
87 int *err, long *timeo_p,
88 const struct sk_buff *skb)
89 {
90 int error;
91 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
92
93 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
94
95 /* Socket errors? */
96 error = sock_error(sk);
97 if (error)
98 goto out_err;
99
100 if (READ_ONCE(queue->prev) != skb)
101 goto out;
102
103 /* Socket shut down? */
104 if (sk->sk_shutdown & RCV_SHUTDOWN)
105 goto out_noerr;
106
107 /* Sequenced packets can come disconnected.
108 * If so we report the problem
109 */
110 error = -ENOTCONN;
111 if (connection_based(sk) &&
112 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
113 goto out_err;
114
115 /* handle signals */
116 if (signal_pending(current))
117 goto interrupted;
118
119 error = 0;
120 *timeo_p = schedule_timeout(*timeo_p);
121 out:
122 finish_wait(sk_sleep(sk), &wait);
123 return error;
124 interrupted:
125 error = sock_intr_errno(*timeo_p);
126 out_err:
127 *err = error;
128 goto out;
129 out_noerr:
130 *err = 0;
131 error = 1;
132 goto out;
133 }
134 EXPORT_SYMBOL(__skb_wait_for_more_packets);
135
skb_set_peeked(struct sk_buff * skb)136 static struct sk_buff *skb_set_peeked(struct sk_buff *skb)
137 {
138 struct sk_buff *nskb;
139
140 if (skb->peeked)
141 return skb;
142
143 /* We have to unshare an skb before modifying it. */
144 if (!skb_shared(skb))
145 goto done;
146
147 nskb = skb_clone(skb, GFP_ATOMIC);
148 if (!nskb)
149 return ERR_PTR(-ENOMEM);
150
151 skb->prev->next = nskb;
152 skb->next->prev = nskb;
153 nskb->prev = skb->prev;
154 nskb->next = skb->next;
155
156 consume_skb(skb);
157 skb = nskb;
158
159 done:
160 skb->peeked = 1;
161
162 return skb;
163 }
164
__skb_try_recv_from_queue(struct sock * sk,struct sk_buff_head * queue,unsigned int flags,int * off,int * err,struct sk_buff ** last)165 struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
166 struct sk_buff_head *queue,
167 unsigned int flags,
168 int *off, int *err,
169 struct sk_buff **last)
170 {
171 bool peek_at_off = false;
172 struct sk_buff *skb;
173 int _off = 0;
174
175 if (unlikely(flags & MSG_PEEK && *off >= 0)) {
176 peek_at_off = true;
177 _off = *off;
178 }
179
180 *last = queue->prev;
181 skb_queue_walk(queue, skb) {
182 if (flags & MSG_PEEK) {
183 if (peek_at_off && _off >= skb->len &&
184 (_off || skb->peeked)) {
185 _off -= skb->len;
186 continue;
187 }
188 if (!skb->len) {
189 skb = skb_set_peeked(skb);
190 if (IS_ERR(skb)) {
191 *err = PTR_ERR(skb);
192 return NULL;
193 }
194 }
195 refcount_inc(&skb->users);
196 } else {
197 __skb_unlink(skb, queue);
198 }
199 *off = _off;
200 return skb;
201 }
202 return NULL;
203 }
204
205 /**
206 * __skb_try_recv_datagram - Receive a datagram skbuff
207 * @sk: socket
208 * @queue: socket queue from which to receive
209 * @flags: MSG\_ flags
210 * @off: an offset in bytes to peek skb from. Returns an offset
211 * within an skb where data actually starts
212 * @err: error code returned
213 * @last: set to last peeked message to inform the wait function
214 * what to look for when peeking
215 *
216 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
217 * and possible races. This replaces identical code in packet, raw and
218 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
219 * the long standing peek and read race for datagram sockets. If you
220 * alter this routine remember it must be re-entrant.
221 *
222 * This function will lock the socket if a skb is returned, so
223 * the caller needs to unlock the socket in that case (usually by
224 * calling skb_free_datagram). Returns NULL with @err set to
225 * -EAGAIN if no data was available or to some other value if an
226 * error was detected.
227 *
228 * * It does not lock socket since today. This function is
229 * * free of race conditions. This measure should/can improve
230 * * significantly datagram socket latencies at high loads,
231 * * when data copying to user space takes lots of time.
232 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
233 * * 8) Great win.)
234 * * --ANK (980729)
235 *
236 * The order of the tests when we find no data waiting are specified
237 * quite explicitly by POSIX 1003.1g, don't change them without having
238 * the standard around please.
239 */
__skb_try_recv_datagram(struct sock * sk,struct sk_buff_head * queue,unsigned int flags,int * off,int * err,struct sk_buff ** last)240 struct sk_buff *__skb_try_recv_datagram(struct sock *sk,
241 struct sk_buff_head *queue,
242 unsigned int flags, int *off, int *err,
243 struct sk_buff **last)
244 {
245 struct sk_buff *skb;
246 unsigned long cpu_flags;
247 /*
248 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
249 */
250 int error = sock_error(sk);
251
252 if (error)
253 goto no_packet;
254
255 do {
256 /* Again only user level code calls this function, so nothing
257 * interrupt level will suddenly eat the receive_queue.
258 *
259 * Look at current nfs client by the way...
260 * However, this function was correct in any case. 8)
261 */
262 spin_lock_irqsave(&queue->lock, cpu_flags);
263 skb = __skb_try_recv_from_queue(sk, queue, flags, off, &error,
264 last);
265 spin_unlock_irqrestore(&queue->lock, cpu_flags);
266 if (error)
267 goto no_packet;
268 if (skb)
269 return skb;
270
271 if (!sk_can_busy_loop(sk))
272 break;
273
274 sk_busy_loop(sk, flags & MSG_DONTWAIT);
275 } while (READ_ONCE(queue->prev) != *last);
276
277 error = -EAGAIN;
278
279 no_packet:
280 *err = error;
281 return NULL;
282 }
283 EXPORT_SYMBOL(__skb_try_recv_datagram);
284
__skb_recv_datagram(struct sock * sk,struct sk_buff_head * sk_queue,unsigned int flags,int * off,int * err)285 struct sk_buff *__skb_recv_datagram(struct sock *sk,
286 struct sk_buff_head *sk_queue,
287 unsigned int flags, int *off, int *err)
288 {
289 struct sk_buff *skb, *last;
290 long timeo;
291
292 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
293
294 do {
295 skb = __skb_try_recv_datagram(sk, sk_queue, flags, off, err,
296 &last);
297 if (skb)
298 return skb;
299
300 if (*err != -EAGAIN)
301 break;
302 } while (timeo &&
303 !__skb_wait_for_more_packets(sk, sk_queue, err,
304 &timeo, last));
305
306 return NULL;
307 }
308 EXPORT_SYMBOL(__skb_recv_datagram);
309
skb_recv_datagram(struct sock * sk,unsigned int flags,int * err)310 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
311 int *err)
312 {
313 int off = 0;
314
315 return __skb_recv_datagram(sk, &sk->sk_receive_queue, flags,
316 &off, err);
317 }
318 EXPORT_SYMBOL(skb_recv_datagram);
319
skb_free_datagram(struct sock * sk,struct sk_buff * skb)320 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
321 {
322 consume_skb(skb);
323 }
324 EXPORT_SYMBOL(skb_free_datagram);
325
__skb_free_datagram_locked(struct sock * sk,struct sk_buff * skb,int len)326 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
327 {
328 bool slow;
329
330 if (!skb_unref(skb)) {
331 sk_peek_offset_bwd(sk, len);
332 return;
333 }
334
335 slow = lock_sock_fast(sk);
336 sk_peek_offset_bwd(sk, len);
337 skb_orphan(skb);
338 unlock_sock_fast(sk, slow);
339
340 /* skb is now orphaned, can be freed outside of locked section */
341 __kfree_skb(skb);
342 }
343 EXPORT_SYMBOL(__skb_free_datagram_locked);
344
__sk_queue_drop_skb(struct sock * sk,struct sk_buff_head * sk_queue,struct sk_buff * skb,unsigned int flags,void (* destructor)(struct sock * sk,struct sk_buff * skb))345 int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
346 struct sk_buff *skb, unsigned int flags,
347 void (*destructor)(struct sock *sk,
348 struct sk_buff *skb))
349 {
350 int err = 0;
351
352 if (flags & MSG_PEEK) {
353 err = -ENOENT;
354 spin_lock_bh(&sk_queue->lock);
355 if (skb->next) {
356 __skb_unlink(skb, sk_queue);
357 refcount_dec(&skb->users);
358 if (destructor)
359 destructor(sk, skb);
360 err = 0;
361 }
362 spin_unlock_bh(&sk_queue->lock);
363 }
364
365 atomic_inc(&sk->sk_drops);
366 return err;
367 }
368 EXPORT_SYMBOL(__sk_queue_drop_skb);
369
370 /**
371 * skb_kill_datagram - Free a datagram skbuff forcibly
372 * @sk: socket
373 * @skb: datagram skbuff
374 * @flags: MSG\_ flags
375 *
376 * This function frees a datagram skbuff that was received by
377 * skb_recv_datagram. The flags argument must match the one
378 * used for skb_recv_datagram.
379 *
380 * If the MSG_PEEK flag is set, and the packet is still on the
381 * receive queue of the socket, it will be taken off the queue
382 * before it is freed.
383 *
384 * This function currently only disables BH when acquiring the
385 * sk_receive_queue lock. Therefore it must not be used in a
386 * context where that lock is acquired in an IRQ context.
387 *
388 * It returns 0 if the packet was removed by us.
389 */
390
skb_kill_datagram(struct sock * sk,struct sk_buff * skb,unsigned int flags)391 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
392 {
393 int err = __sk_queue_drop_skb(sk, &sk->sk_receive_queue, skb, flags,
394 NULL);
395
396 kfree_skb(skb);
397 return err;
398 }
399 EXPORT_SYMBOL(skb_kill_datagram);
400
401 INDIRECT_CALLABLE_DECLARE(static size_t simple_copy_to_iter(const void *addr,
402 size_t bytes,
403 void *data __always_unused,
404 struct iov_iter *i));
405
__skb_datagram_iter(const struct sk_buff * skb,int offset,struct iov_iter * to,int len,bool fault_short,size_t (* cb)(const void *,size_t,void *,struct iov_iter *),void * data)406 static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
407 struct iov_iter *to, int len, bool fault_short,
408 size_t (*cb)(const void *, size_t, void *,
409 struct iov_iter *), void *data)
410 {
411 int start = skb_headlen(skb);
412 int i, copy = start - offset, start_off = offset, n;
413 struct sk_buff *frag_iter;
414
415 /* Copy header. */
416 if (copy > 0) {
417 if (copy > len)
418 copy = len;
419 n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
420 skb->data + offset, copy, data, to);
421 offset += n;
422 if (n != copy)
423 goto short_copy;
424 if ((len -= copy) == 0)
425 return 0;
426 }
427
428 /* Copy paged appendix. Hmm... why does this look so complicated? */
429 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
430 int end;
431 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
432
433 WARN_ON(start > offset + len);
434
435 end = start + skb_frag_size(frag);
436 if ((copy = end - offset) > 0) {
437 u32 p_off, p_len, copied;
438 struct page *p;
439 u8 *vaddr;
440
441 if (copy > len)
442 copy = len;
443
444 n = 0;
445 skb_frag_foreach_page(frag,
446 skb_frag_off(frag) + offset - start,
447 copy, p, p_off, p_len, copied) {
448 vaddr = kmap_local_page(p);
449 n += INDIRECT_CALL_1(cb, simple_copy_to_iter,
450 vaddr + p_off, p_len, data, to);
451 kunmap_local(vaddr);
452 }
453
454 offset += n;
455 if (n != copy)
456 goto short_copy;
457 if (!(len -= copy))
458 return 0;
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_datagram_iter(frag_iter, offset - start,
473 to, copy, fault_short, cb, data))
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:
490 iov_iter_revert(to, offset - start_off);
491 return -EFAULT;
492
493 short_copy:
494 if (fault_short || iov_iter_count(to))
495 goto fault;
496
497 return 0;
498 }
499
500 /**
501 * skb_copy_and_hash_datagram_iter - Copy datagram to an iovec iterator
502 * and update a hash.
503 * @skb: buffer to copy
504 * @offset: offset in the buffer to start copying from
505 * @to: iovec iterator to copy to
506 * @len: amount of data to copy from buffer to iovec
507 * @hash: hash request to update
508 */
skb_copy_and_hash_datagram_iter(const struct sk_buff * skb,int offset,struct iov_iter * to,int len,struct ahash_request * hash)509 int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
510 struct iov_iter *to, int len,
511 struct ahash_request *hash)
512 {
513 return __skb_datagram_iter(skb, offset, to, len, true,
514 hash_and_copy_to_iter, hash);
515 }
516 EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter);
517
simple_copy_to_iter(const void * addr,size_t bytes,void * data __always_unused,struct iov_iter * i)518 static size_t simple_copy_to_iter(const void *addr, size_t bytes,
519 void *data __always_unused, struct iov_iter *i)
520 {
521 return copy_to_iter(addr, bytes, i);
522 }
523
524 /**
525 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
526 * @skb: buffer to copy
527 * @offset: offset in the buffer to start copying from
528 * @to: iovec iterator to copy to
529 * @len: amount of data to copy from buffer to iovec
530 */
skb_copy_datagram_iter(const struct sk_buff * skb,int offset,struct iov_iter * to,int len)531 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
532 struct iov_iter *to, int len)
533 {
534 trace_skb_copy_datagram_iovec(skb, len);
535 return __skb_datagram_iter(skb, offset, to, len, false,
536 simple_copy_to_iter, NULL);
537 }
538 EXPORT_SYMBOL(skb_copy_datagram_iter);
539
540 /**
541 * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
542 * @skb: buffer to copy
543 * @offset: offset in the buffer to start copying to
544 * @from: the copy source
545 * @len: amount of data to copy to buffer from iovec
546 *
547 * Returns 0 or -EFAULT.
548 */
skb_copy_datagram_from_iter(struct sk_buff * skb,int offset,struct iov_iter * from,int len)549 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
550 struct iov_iter *from,
551 int len)
552 {
553 int start = skb_headlen(skb);
554 int i, copy = start - offset;
555 struct sk_buff *frag_iter;
556
557 /* Copy header. */
558 if (copy > 0) {
559 if (copy > len)
560 copy = len;
561 if (copy_from_iter(skb->data + offset, copy, from) != copy)
562 goto fault;
563 if ((len -= copy) == 0)
564 return 0;
565 offset += copy;
566 }
567
568 /* Copy paged appendix. Hmm... why does this look so complicated? */
569 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
570 int end;
571 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
572
573 WARN_ON(start > offset + len);
574
575 end = start + skb_frag_size(frag);
576 if ((copy = end - offset) > 0) {
577 size_t copied;
578
579 if (copy > len)
580 copy = len;
581 copied = copy_page_from_iter(skb_frag_page(frag),
582 skb_frag_off(frag) + offset - start,
583 copy, from);
584 if (copied != copy)
585 goto fault;
586
587 if (!(len -= copy))
588 return 0;
589 offset += copy;
590 }
591 start = end;
592 }
593
594 skb_walk_frags(skb, frag_iter) {
595 int end;
596
597 WARN_ON(start > offset + len);
598
599 end = start + frag_iter->len;
600 if ((copy = end - offset) > 0) {
601 if (copy > len)
602 copy = len;
603 if (skb_copy_datagram_from_iter(frag_iter,
604 offset - start,
605 from, copy))
606 goto fault;
607 if ((len -= copy) == 0)
608 return 0;
609 offset += copy;
610 }
611 start = end;
612 }
613 if (!len)
614 return 0;
615
616 fault:
617 return -EFAULT;
618 }
619 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
620
__zerocopy_sg_from_iter(struct msghdr * msg,struct sock * sk,struct sk_buff * skb,struct iov_iter * from,size_t length)621 int __zerocopy_sg_from_iter(struct msghdr *msg, struct sock *sk,
622 struct sk_buff *skb, struct iov_iter *from,
623 size_t length)
624 {
625 int frag;
626
627 if (msg && msg->msg_ubuf && msg->sg_from_iter)
628 return msg->sg_from_iter(sk, skb, from, length);
629
630 frag = skb_shinfo(skb)->nr_frags;
631
632 while (length && iov_iter_count(from)) {
633 struct page *head, *last_head = NULL;
634 struct page *pages[MAX_SKB_FRAGS];
635 int refs, order, n = 0;
636 size_t start;
637 ssize_t copied;
638 unsigned long truesize;
639
640 if (frag == MAX_SKB_FRAGS)
641 return -EMSGSIZE;
642
643 copied = iov_iter_get_pages2(from, pages, length,
644 MAX_SKB_FRAGS - frag, &start);
645 if (copied < 0)
646 return -EFAULT;
647
648 length -= copied;
649
650 truesize = PAGE_ALIGN(copied + start);
651 skb->data_len += copied;
652 skb->len += copied;
653 skb->truesize += truesize;
654 if (sk && sk->sk_type == SOCK_STREAM) {
655 sk_wmem_queued_add(sk, truesize);
656 if (!skb_zcopy_pure(skb))
657 sk_mem_charge(sk, truesize);
658 } else {
659 refcount_add(truesize, &skb->sk->sk_wmem_alloc);
660 }
661
662 head = compound_head(pages[n]);
663 order = compound_order(head);
664
665 for (refs = 0; copied != 0; start = 0) {
666 int size = min_t(int, copied, PAGE_SIZE - start);
667
668 if (pages[n] - head > (1UL << order) - 1) {
669 head = compound_head(pages[n]);
670 order = compound_order(head);
671 }
672
673 start += (pages[n] - head) << PAGE_SHIFT;
674 copied -= size;
675 n++;
676 if (frag) {
677 skb_frag_t *last = &skb_shinfo(skb)->frags[frag - 1];
678
679 if (head == skb_frag_page(last) &&
680 start == skb_frag_off(last) + skb_frag_size(last)) {
681 skb_frag_size_add(last, size);
682 /* We combined this page, we need to release
683 * a reference. Since compound pages refcount
684 * is shared among many pages, batch the refcount
685 * adjustments to limit false sharing.
686 */
687 last_head = head;
688 refs++;
689 continue;
690 }
691 }
692 if (refs) {
693 page_ref_sub(last_head, refs);
694 refs = 0;
695 }
696 skb_fill_page_desc_noacc(skb, frag++, head, start, size);
697 }
698 if (refs)
699 page_ref_sub(last_head, refs);
700 }
701 return 0;
702 }
703 EXPORT_SYMBOL(__zerocopy_sg_from_iter);
704
705 /**
706 * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
707 * @skb: buffer to copy
708 * @from: the source to copy from
709 *
710 * The function will first copy up to headlen, and then pin the userspace
711 * pages and build frags through them.
712 *
713 * Returns 0, -EFAULT or -EMSGSIZE.
714 */
zerocopy_sg_from_iter(struct sk_buff * skb,struct iov_iter * from)715 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
716 {
717 int copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
718
719 /* copy up to skb headlen */
720 if (skb_copy_datagram_from_iter(skb, 0, from, copy))
721 return -EFAULT;
722
723 return __zerocopy_sg_from_iter(NULL, NULL, skb, from, ~0U);
724 }
725 EXPORT_SYMBOL(zerocopy_sg_from_iter);
726
727 /**
728 * skb_copy_and_csum_datagram - Copy datagram to an iovec iterator
729 * and update a checksum.
730 * @skb: buffer to copy
731 * @offset: offset in the buffer to start copying from
732 * @to: iovec iterator to copy to
733 * @len: amount of data to copy from buffer to iovec
734 * @csump: checksum pointer
735 */
skb_copy_and_csum_datagram(const struct sk_buff * skb,int offset,struct iov_iter * to,int len,__wsum * csump)736 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
737 struct iov_iter *to, int len,
738 __wsum *csump)
739 {
740 struct csum_state csdata = { .csum = *csump };
741 int ret;
742
743 ret = __skb_datagram_iter(skb, offset, to, len, true,
744 csum_and_copy_to_iter, &csdata);
745 if (ret)
746 return ret;
747
748 *csump = csdata.csum;
749 return 0;
750 }
751
752 /**
753 * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
754 * @skb: skbuff
755 * @hlen: hardware length
756 * @msg: destination
757 *
758 * Caller _must_ check that skb will fit to this iovec.
759 *
760 * Returns: 0 - success.
761 * -EINVAL - checksum failure.
762 * -EFAULT - fault during copy.
763 */
skb_copy_and_csum_datagram_msg(struct sk_buff * skb,int hlen,struct msghdr * msg)764 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
765 int hlen, struct msghdr *msg)
766 {
767 __wsum csum;
768 int chunk = skb->len - hlen;
769
770 if (!chunk)
771 return 0;
772
773 if (msg_data_left(msg) < chunk) {
774 if (__skb_checksum_complete(skb))
775 return -EINVAL;
776 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
777 goto fault;
778 } else {
779 csum = csum_partial(skb->data, hlen, skb->csum);
780 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
781 chunk, &csum))
782 goto fault;
783
784 if (csum_fold(csum)) {
785 iov_iter_revert(&msg->msg_iter, chunk);
786 return -EINVAL;
787 }
788
789 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
790 !skb->csum_complete_sw)
791 netdev_rx_csum_fault(NULL, skb);
792 }
793 return 0;
794 fault:
795 return -EFAULT;
796 }
797 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
798
799 /**
800 * datagram_poll - generic datagram poll
801 * @file: file struct
802 * @sock: socket
803 * @wait: poll table
804 *
805 * Datagram poll: Again totally generic. This also handles
806 * sequenced packet sockets providing the socket receive queue
807 * is only ever holding data ready to receive.
808 *
809 * Note: when you *don't* use this routine for this protocol,
810 * and you use a different write policy from sock_writeable()
811 * then please supply your own write_space callback.
812 */
datagram_poll(struct file * file,struct socket * sock,poll_table * wait)813 __poll_t datagram_poll(struct file *file, struct socket *sock,
814 poll_table *wait)
815 {
816 struct sock *sk = sock->sk;
817 __poll_t mask;
818 u8 shutdown;
819
820 sock_poll_wait(file, sock, wait);
821 mask = 0;
822
823 /* exceptional events? */
824 if (READ_ONCE(sk->sk_err) ||
825 !skb_queue_empty_lockless(&sk->sk_error_queue))
826 mask |= EPOLLERR |
827 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
828
829 shutdown = READ_ONCE(sk->sk_shutdown);
830 if (shutdown & RCV_SHUTDOWN)
831 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
832 if (shutdown == SHUTDOWN_MASK)
833 mask |= EPOLLHUP;
834
835 /* readable? */
836 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
837 mask |= EPOLLIN | EPOLLRDNORM;
838
839 /* Connection-based need to check for termination and startup */
840 if (connection_based(sk)) {
841 int state = READ_ONCE(sk->sk_state);
842
843 if (state == TCP_CLOSE)
844 mask |= EPOLLHUP;
845 /* connection hasn't started yet? */
846 if (state == TCP_SYN_SENT)
847 return mask;
848 }
849
850 /* writable? */
851 if (sock_writeable(sk))
852 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
853 else
854 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
855
856 return mask;
857 }
858 EXPORT_SYMBOL(datagram_poll);
859