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