Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 12 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * udp.c code) |
| 14 | * |
| 15 | * Fixes: |
| 16 | * Alan Cox : NULL return from skb_peek_copy() |
| 17 | * understood |
| 18 | * Alan Cox : Rewrote skb_read_datagram to avoid the |
| 19 | * skb_peek_copy stuff. |
| 20 | * Alan Cox : Added support for SOCK_SEQPACKET. |
| 21 | * IPX can no longer use the SO_TYPE hack |
| 22 | * but AX.25 now works right, and SPX is |
| 23 | * feasible. |
| 24 | * Alan Cox : Fixed write poll of non IP protocol |
| 25 | * crash. |
| 26 | * Florian La Roche: Changed for my new skbuff handling. |
| 27 | * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET. |
| 28 | * Linus Torvalds : BSD semantic fixes. |
| 29 | * Alan Cox : Datagram iovec handling |
| 30 | * Darryl Miles : Fixed non-blocking SOCK_STREAM. |
| 31 | * Alan Cox : POSIXisms |
| 32 | * Pete Wyckoff : Unconnected accept() fix. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/types.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/netdevice.h> |
| 46 | #include <linux/rtnetlink.h> |
| 47 | #include <linux/poll.h> |
| 48 | #include <linux/highmem.h> |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 49 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Jason Wang | 0433547 | 2013-08-06 17:45:07 +0800 | [diff] [blame] | 51 | #include <linux/pagemap.h> |
Herbert Xu | a8f820aa | 2014-11-07 21:22:22 +0800 | [diff] [blame] | 52 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #include <net/protocol.h> |
| 55 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 57 | #include <net/checksum.h> |
| 58 | #include <net/sock.h> |
| 59 | #include <net/tcp_states.h> |
Neil Horman | e9b3cc1 | 2009-08-13 05:19:44 +0000 | [diff] [blame] | 60 | #include <trace/events/skb.h> |
Eliezer Tamir | 076bb0c | 2013-07-10 17:13:17 +0300 | [diff] [blame] | 61 | #include <net/busy_poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 71 | static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync, |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 84 | * Wait for the last received packet to be different from skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | */ |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 86 | static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p, |
| 87 | const struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | int error; |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 90 | DEFINE_WAIT_FUNC(wait, receiver_wake_function); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 92 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* Socket errors? */ |
| 95 | error = sock_error(sk); |
| 96 | if (error) |
| 97 | goto out_err; |
| 98 | |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 99 | if (sk->sk_receive_queue.prev != skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 121 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return error; |
| 123 | interrupted: |
| 124 | error = sock_intr_errno(*timeo_p); |
| 125 | out_err: |
| 126 | *err = error; |
| 127 | goto out; |
| 128 | out_noerr: |
| 129 | *err = 0; |
| 130 | error = 1; |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | /** |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 135 | * __skb_recv_datagram - Receive a datagram skbuff |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 136 | * @sk: socket |
| 137 | * @flags: MSG_ flags |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 138 | * @peeked: returns non-zero if this packet has been seen before |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 139 | * @off: an offset in bytes to peek skb from. Returns an offset |
| 140 | * within an skb where data actually starts |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 141 | * @err: error code returned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * |
| 143 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups |
| 144 | * and possible races. This replaces identical code in packet, raw and |
| 145 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes |
| 146 | * the long standing peek and read race for datagram sockets. If you |
| 147 | * alter this routine remember it must be re-entrant. |
| 148 | * |
| 149 | * This function will lock the socket if a skb is returned, so the caller |
| 150 | * needs to unlock the socket in that case (usually by calling |
| 151 | * skb_free_datagram) |
| 152 | * |
| 153 | * * It does not lock socket since today. This function is |
| 154 | * * free of race conditions. This measure should/can improve |
| 155 | * * significantly datagram socket latencies at high loads, |
| 156 | * * when data copying to user space takes lots of time. |
| 157 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet |
| 158 | * * 8) Great win.) |
| 159 | * * --ANK (980729) |
| 160 | * |
| 161 | * The order of the tests when we find no data waiting are specified |
| 162 | * quite explicitly by POSIX 1003.1g, don't change them without having |
| 163 | * the standard around please. |
| 164 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 165 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags, |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 166 | int *peeked, int *off, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 168 | struct sk_buff *skb, *last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | long timeo; |
| 170 | /* |
| 171 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() |
| 172 | */ |
| 173 | int error = sock_error(sk); |
| 174 | |
| 175 | if (error) |
| 176 | goto no_packet; |
| 177 | |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 178 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | do { |
| 181 | /* Again only user level code calls this function, so nothing |
| 182 | * interrupt level will suddenly eat the receive_queue. |
| 183 | * |
| 184 | * Look at current nfs client by the way... |
David Shwatrz | 8917a3c | 2010-12-02 09:01:55 +0000 | [diff] [blame] | 185 | * However, this function was correct in any case. 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | */ |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 187 | unsigned long cpu_flags; |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 188 | struct sk_buff_head *queue = &sk->sk_receive_queue; |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 189 | int _off = *off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 191 | last = (struct sk_buff *)queue; |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 192 | spin_lock_irqsave(&queue->lock, cpu_flags); |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 193 | skb_queue_walk(queue, skb) { |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 194 | last = skb; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 195 | *peeked = skb->peeked; |
| 196 | if (flags & MSG_PEEK) { |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 197 | if (_off >= skb->len && (skb->len || _off || |
Benjamin Poirier | add05ad | 2013-04-29 11:42:12 +0000 | [diff] [blame] | 198 | skb->peeked)) { |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 199 | _off -= skb->len; |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 200 | continue; |
| 201 | } |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 202 | skb->peeked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | atomic_inc(&skb->users); |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 204 | } else |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 205 | __skb_unlink(skb, queue); |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 206 | |
| 207 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 208 | *off = _off; |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 209 | return skb; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 210 | } |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 211 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Eliezer Tamir | cbf5500 | 2013-07-08 16:20:34 +0300 | [diff] [blame] | 213 | if (sk_can_busy_loop(sk) && |
| 214 | sk_busy_loop(sk, flags & MSG_DONTWAIT)) |
Eliezer Tamir | a5b5047 | 2013-06-10 11:40:00 +0300 | [diff] [blame] | 215 | continue; |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* User doesn't want to wait */ |
| 218 | error = -EAGAIN; |
| 219 | if (!timeo) |
| 220 | goto no_packet; |
| 221 | |
Benjamin Poirier | 39cc861 | 2013-04-29 11:42:13 +0000 | [diff] [blame] | 222 | } while (!wait_for_more_packets(sk, err, &timeo, last)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | return NULL; |
| 225 | |
| 226 | no_packet: |
| 227 | *err = error; |
| 228 | return NULL; |
| 229 | } |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 230 | EXPORT_SYMBOL(__skb_recv_datagram); |
| 231 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 232 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 233 | int noblock, int *err) |
| 234 | { |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 235 | int peeked, off = 0; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 236 | |
| 237 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 238 | &peeked, &off, err); |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 239 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 240 | EXPORT_SYMBOL(skb_recv_datagram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) |
| 243 | { |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 244 | consume_skb(skb); |
Eric Dumazet | 270acef | 2008-11-05 01:38:06 -0800 | [diff] [blame] | 245 | sk_mem_reclaim_partial(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 247 | EXPORT_SYMBOL(skb_free_datagram); |
| 248 | |
| 249 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb) |
| 250 | { |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 251 | bool slow; |
| 252 | |
Eric Dumazet | 93bb64e | 2010-05-03 23:18:14 -0700 | [diff] [blame] | 253 | if (likely(atomic_read(&skb->users) == 1)) |
| 254 | smp_rmb(); |
| 255 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 256 | return; |
| 257 | |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 258 | slow = lock_sock_fast(sk); |
Eric Dumazet | 4b0b72f | 2010-04-28 14:35:48 -0700 | [diff] [blame] | 259 | skb_orphan(skb); |
| 260 | sk_mem_reclaim_partial(sk); |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 261 | unlock_sock_fast(sk, slow); |
Eric Dumazet | 4b0b72f | 2010-04-28 14:35:48 -0700 | [diff] [blame] | 262 | |
Eric Dumazet | 93bb64e | 2010-05-03 23:18:14 -0700 | [diff] [blame] | 263 | /* skb is now orphaned, can be freed outside of locked section */ |
| 264 | __kfree_skb(skb); |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 265 | } |
| 266 | EXPORT_SYMBOL(skb_free_datagram_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | /** |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 269 | * skb_kill_datagram - Free a datagram skbuff forcibly |
| 270 | * @sk: socket |
| 271 | * @skb: datagram skbuff |
| 272 | * @flags: MSG_ flags |
| 273 | * |
| 274 | * This function frees a datagram skbuff that was received by |
| 275 | * skb_recv_datagram. The flags argument must match the one |
| 276 | * used for skb_recv_datagram. |
| 277 | * |
| 278 | * If the MSG_PEEK flag is set, and the packet is still on the |
| 279 | * receive queue of the socket, it will be taken off the queue |
| 280 | * before it is freed. |
| 281 | * |
| 282 | * This function currently only disables BH when acquiring the |
| 283 | * sk_receive_queue lock. Therefore it must not be used in a |
| 284 | * context where that lock is acquired in an IRQ context. |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 285 | * |
| 286 | * It returns 0 if the packet was removed by us. |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 287 | */ |
| 288 | |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 289 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 290 | { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 291 | int err = 0; |
| 292 | |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 293 | if (flags & MSG_PEEK) { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 294 | err = -ENOENT; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 295 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 296 | if (skb == skb_peek(&sk->sk_receive_queue)) { |
| 297 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 298 | atomic_dec(&skb->users); |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 299 | err = 0; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 300 | } |
| 301 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 302 | } |
| 303 | |
John Dykstra | 61de71c | 2009-05-08 14:57:01 -0700 | [diff] [blame] | 304 | kfree_skb(skb); |
Eric Dumazet | 8edf19c | 2009-10-15 00:12:40 +0000 | [diff] [blame] | 305 | atomic_inc(&sk->sk_drops); |
John Dykstra | 61de71c | 2009-05-08 14:57:01 -0700 | [diff] [blame] | 306 | sk_mem_reclaim_partial(sk); |
| 307 | |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 308 | return err; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 309 | } |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 310 | EXPORT_SYMBOL(skb_kill_datagram); |
| 311 | |
| 312 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 314 | * @skb: buffer to copy |
| 315 | * @offset: offset in the buffer to start copying from |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 316 | * @to: io vector to copy to |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 317 | * @len: amount of data to copy from buffer to iovec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * |
| 319 | * Note: the iovec is modified during the copy. |
| 320 | */ |
| 321 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, |
| 322 | struct iovec *to, int len) |
| 323 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 324 | int start = skb_headlen(skb); |
| 325 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 326 | struct sk_buff *frag_iter; |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 327 | |
Neil Horman | e9b3cc1 | 2009-08-13 05:19:44 +0000 | [diff] [blame] | 328 | trace_skb_copy_datagram_iovec(skb, len); |
| 329 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 330 | /* Copy header. */ |
| 331 | if (copy > 0) { |
| 332 | if (copy > len) |
| 333 | copy = len; |
| 334 | if (memcpy_toiovec(to, skb->data + offset, copy)) |
| 335 | goto fault; |
| 336 | if ((len -= copy) == 0) |
| 337 | return 0; |
| 338 | offset += copy; |
| 339 | } |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 340 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 341 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 342 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 343 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 344 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 346 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 347 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 348 | end = start + skb_frag_size(frag); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 349 | if ((copy = end - offset) > 0) { |
| 350 | int err; |
| 351 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 352 | struct page *page = skb_frag_page(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | if (copy > len) |
| 355 | copy = len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 356 | vaddr = kmap(page); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 357 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
| 358 | offset - start, copy); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 359 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if (err) |
| 361 | goto fault; |
| 362 | if (!(len -= copy)) |
| 363 | return 0; |
| 364 | offset += copy; |
| 365 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 366 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 368 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 369 | skb_walk_frags(skb, frag_iter) { |
| 370 | int end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 371 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 372 | WARN_ON(start > offset + len); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 373 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 374 | end = start + frag_iter->len; |
| 375 | if ((copy = end - offset) > 0) { |
| 376 | if (copy > len) |
| 377 | copy = len; |
| 378 | if (skb_copy_datagram_iovec(frag_iter, |
| 379 | offset - start, |
| 380 | to, copy)) |
| 381 | goto fault; |
| 382 | if ((len -= copy) == 0) |
| 383 | return 0; |
| 384 | offset += copy; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 385 | } |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 386 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 388 | if (!len) |
| 389 | return 0; |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | fault: |
| 392 | return -EFAULT; |
| 393 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 394 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 396 | /** |
Herbert Xu | a8f820aa | 2014-11-07 21:22:22 +0800 | [diff] [blame] | 397 | * skb_copy_datagram_iter - Copy a datagram to an iovec iterator. |
| 398 | * @skb: buffer to copy |
| 399 | * @offset: offset in the buffer to start copying from |
| 400 | * @to: iovec iterator to copy to |
| 401 | * @len: amount of data to copy from buffer to iovec |
| 402 | */ |
| 403 | int skb_copy_datagram_iter(const struct sk_buff *skb, int offset, |
| 404 | struct iov_iter *to, int len) |
| 405 | { |
| 406 | int start = skb_headlen(skb); |
| 407 | int i, copy = start - offset; |
| 408 | struct sk_buff *frag_iter; |
| 409 | |
| 410 | trace_skb_copy_datagram_iovec(skb, len); |
| 411 | |
| 412 | /* Copy header. */ |
| 413 | if (copy > 0) { |
| 414 | if (copy > len) |
| 415 | copy = len; |
| 416 | if (copy_to_iter(skb->data + offset, copy, to) != copy) |
| 417 | goto short_copy; |
| 418 | if ((len -= copy) == 0) |
| 419 | return 0; |
| 420 | offset += copy; |
| 421 | } |
| 422 | |
| 423 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 424 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 425 | int end; |
| 426 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 427 | |
| 428 | WARN_ON(start > offset + len); |
| 429 | |
| 430 | end = start + skb_frag_size(frag); |
| 431 | if ((copy = end - offset) > 0) { |
| 432 | if (copy > len) |
| 433 | copy = len; |
| 434 | if (copy_page_to_iter(skb_frag_page(frag), |
| 435 | frag->page_offset + offset - |
| 436 | start, copy, to) != copy) |
| 437 | goto short_copy; |
| 438 | if (!(len -= copy)) |
| 439 | return 0; |
| 440 | offset += copy; |
| 441 | } |
| 442 | start = end; |
| 443 | } |
| 444 | |
| 445 | skb_walk_frags(skb, frag_iter) { |
| 446 | int end; |
| 447 | |
| 448 | WARN_ON(start > offset + len); |
| 449 | |
| 450 | end = start + frag_iter->len; |
| 451 | if ((copy = end - offset) > 0) { |
| 452 | if (copy > len) |
| 453 | copy = len; |
| 454 | if (skb_copy_datagram_iter(frag_iter, offset - start, |
| 455 | to, copy)) |
| 456 | goto fault; |
| 457 | if ((len -= copy) == 0) |
| 458 | return 0; |
| 459 | offset += copy; |
| 460 | } |
| 461 | start = end; |
| 462 | } |
| 463 | if (!len) |
| 464 | return 0; |
| 465 | |
| 466 | /* This is not really a user copy fault, but rather someone |
| 467 | * gave us a bogus length on the skb. We should probably |
| 468 | * print a warning here as it may indicate a kernel bug. |
| 469 | */ |
| 470 | |
| 471 | fault: |
| 472 | return -EFAULT; |
| 473 | |
| 474 | short_copy: |
| 475 | if (iov_iter_count(to)) |
| 476 | goto fault; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | EXPORT_SYMBOL(skb_copy_datagram_iter); |
| 481 | |
| 482 | /** |
Al Viro | 8feb2fb | 2014-11-06 01:10:59 -0500 | [diff] [blame] | 483 | * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter. |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 484 | * @skb: buffer to copy |
| 485 | * @offset: offset in the buffer to start copying to |
Al Viro | 8feb2fb | 2014-11-06 01:10:59 -0500 | [diff] [blame] | 486 | * @from: the copy source |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 487 | * @len: amount of data to copy to buffer from iovec |
| 488 | * |
| 489 | * Returns 0 or -EFAULT. |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 490 | */ |
Al Viro | 3a654f9 | 2014-06-19 14:15:22 -0400 | [diff] [blame] | 491 | int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset, |
| 492 | struct iov_iter *from, |
| 493 | int len) |
| 494 | { |
| 495 | int start = skb_headlen(skb); |
| 496 | int i, copy = start - offset; |
| 497 | struct sk_buff *frag_iter; |
| 498 | |
| 499 | /* Copy header. */ |
| 500 | if (copy > 0) { |
| 501 | if (copy > len) |
| 502 | copy = len; |
| 503 | if (copy_from_iter(skb->data + offset, copy, from) != copy) |
| 504 | goto fault; |
| 505 | if ((len -= copy) == 0) |
| 506 | return 0; |
| 507 | offset += copy; |
| 508 | } |
| 509 | |
| 510 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 511 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 512 | int end; |
| 513 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 514 | |
| 515 | WARN_ON(start > offset + len); |
| 516 | |
| 517 | end = start + skb_frag_size(frag); |
| 518 | if ((copy = end - offset) > 0) { |
| 519 | size_t copied; |
| 520 | |
| 521 | if (copy > len) |
| 522 | copy = len; |
| 523 | copied = copy_page_from_iter(skb_frag_page(frag), |
| 524 | frag->page_offset + offset - start, |
| 525 | copy, from); |
| 526 | if (copied != copy) |
| 527 | goto fault; |
| 528 | |
| 529 | if (!(len -= copy)) |
| 530 | return 0; |
| 531 | offset += copy; |
| 532 | } |
| 533 | start = end; |
| 534 | } |
| 535 | |
| 536 | skb_walk_frags(skb, frag_iter) { |
| 537 | int end; |
| 538 | |
| 539 | WARN_ON(start > offset + len); |
| 540 | |
| 541 | end = start + frag_iter->len; |
| 542 | if ((copy = end - offset) > 0) { |
| 543 | if (copy > len) |
| 544 | copy = len; |
| 545 | if (skb_copy_datagram_from_iter(frag_iter, |
| 546 | offset - start, |
| 547 | from, copy)) |
| 548 | goto fault; |
| 549 | if ((len -= copy) == 0) |
| 550 | return 0; |
| 551 | offset += copy; |
| 552 | } |
| 553 | start = end; |
| 554 | } |
| 555 | if (!len) |
| 556 | return 0; |
| 557 | |
| 558 | fault: |
| 559 | return -EFAULT; |
| 560 | } |
| 561 | EXPORT_SYMBOL(skb_copy_datagram_from_iter); |
| 562 | |
Jason Wang | c3bdeb5 | 2013-08-06 17:45:04 +0800 | [diff] [blame] | 563 | /** |
Al Viro | 195e952 | 2014-11-06 00:56:48 -0500 | [diff] [blame] | 564 | * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter |
Jason Wang | c3bdeb5 | 2013-08-06 17:45:04 +0800 | [diff] [blame] | 565 | * @skb: buffer to copy |
Al Viro | 195e952 | 2014-11-06 00:56:48 -0500 | [diff] [blame] | 566 | * @from: the source to copy from |
Jason Wang | c3bdeb5 | 2013-08-06 17:45:04 +0800 | [diff] [blame] | 567 | * |
| 568 | * The function will first copy up to headlen, and then pin the userspace |
| 569 | * pages and build frags through them. |
| 570 | * |
| 571 | * Returns 0, -EFAULT or -EMSGSIZE. |
Jason Wang | c3bdeb5 | 2013-08-06 17:45:04 +0800 | [diff] [blame] | 572 | */ |
Al Viro | 3a654f9 | 2014-06-19 14:15:22 -0400 | [diff] [blame] | 573 | int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from) |
| 574 | { |
| 575 | int len = iov_iter_count(from); |
| 576 | int copy = min_t(int, skb_headlen(skb), len); |
| 577 | int frag = 0; |
| 578 | |
| 579 | /* copy up to skb headlen */ |
| 580 | if (skb_copy_datagram_from_iter(skb, 0, from, copy)) |
| 581 | return -EFAULT; |
| 582 | |
| 583 | while (iov_iter_count(from)) { |
| 584 | struct page *pages[MAX_SKB_FRAGS]; |
| 585 | size_t start; |
| 586 | ssize_t copied; |
| 587 | unsigned long truesize; |
| 588 | int n = 0; |
| 589 | |
| 590 | if (frag == MAX_SKB_FRAGS) |
| 591 | return -EMSGSIZE; |
| 592 | |
| 593 | copied = iov_iter_get_pages(from, pages, ~0U, |
| 594 | MAX_SKB_FRAGS - frag, &start); |
| 595 | if (copied < 0) |
| 596 | return -EFAULT; |
| 597 | |
| 598 | iov_iter_advance(from, copied); |
| 599 | |
| 600 | truesize = PAGE_ALIGN(copied + start); |
| 601 | skb->data_len += copied; |
| 602 | skb->len += copied; |
| 603 | skb->truesize += truesize; |
| 604 | atomic_add(truesize, &skb->sk->sk_wmem_alloc); |
| 605 | while (copied) { |
| 606 | int size = min_t(int, copied, PAGE_SIZE - start); |
| 607 | skb_fill_page_desc(skb, frag++, pages[n], start, size); |
| 608 | start = 0; |
| 609 | copied -= size; |
| 610 | n++; |
| 611 | } |
| 612 | } |
| 613 | return 0; |
| 614 | } |
| 615 | EXPORT_SYMBOL(zerocopy_sg_from_iter); |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
| 618 | u8 __user *to, int len, |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 619 | __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 621 | int start = skb_headlen(skb); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 622 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 623 | struct sk_buff *frag_iter; |
| 624 | int pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | /* Copy header. */ |
| 627 | if (copy > 0) { |
| 628 | int err = 0; |
| 629 | if (copy > len) |
| 630 | copy = len; |
| 631 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, |
| 632 | *csump, &err); |
| 633 | if (err) |
| 634 | goto fault; |
| 635 | if ((len -= copy) == 0) |
| 636 | return 0; |
| 637 | offset += copy; |
| 638 | to += copy; |
| 639 | pos = copy; |
| 640 | } |
| 641 | |
| 642 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 643 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 644 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 646 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 647 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 648 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 650 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | int err = 0; |
| 652 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 653 | struct page *page = skb_frag_page(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
| 655 | if (copy > len) |
| 656 | copy = len; |
| 657 | vaddr = kmap(page); |
| 658 | csum2 = csum_and_copy_to_user(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 659 | frag->page_offset + |
| 660 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | to, copy, 0, &err); |
| 662 | kunmap(page); |
| 663 | if (err) |
| 664 | goto fault; |
| 665 | *csump = csum_block_add(*csump, csum2, pos); |
| 666 | if (!(len -= copy)) |
| 667 | return 0; |
| 668 | offset += copy; |
| 669 | to += copy; |
| 670 | pos += copy; |
| 671 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 672 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 675 | skb_walk_frags(skb, frag_iter) { |
| 676 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 678 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 680 | end = start + frag_iter->len; |
| 681 | if ((copy = end - offset) > 0) { |
| 682 | __wsum csum2 = 0; |
| 683 | if (copy > len) |
| 684 | copy = len; |
| 685 | if (skb_copy_and_csum_datagram(frag_iter, |
| 686 | offset - start, |
| 687 | to, copy, |
| 688 | &csum2)) |
| 689 | goto fault; |
| 690 | *csump = csum_block_add(*csump, csum2, pos); |
| 691 | if ((len -= copy) == 0) |
| 692 | return 0; |
| 693 | offset += copy; |
| 694 | to += copy; |
| 695 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 697 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | if (!len) |
| 700 | return 0; |
| 701 | |
| 702 | fault: |
| 703 | return -EFAULT; |
| 704 | } |
| 705 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 706 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 707 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 708 | __sum16 sum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 709 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 710 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Tom Herbert | 46fb51e | 2014-06-14 23:24:03 -0700 | [diff] [blame] | 711 | if (likely(!sum)) { |
| 712 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 713 | !skb->csum_complete_sw) |
| 714 | netdev_rx_csum_fault(skb->dev); |
| 715 | } |
| 716 | skb->csum_valid = !sum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 717 | return sum; |
| 718 | } |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 719 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 720 | |
| 721 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 722 | { |
Tom Herbert | 46fb51e | 2014-06-14 23:24:03 -0700 | [diff] [blame] | 723 | __wsum csum; |
| 724 | __sum16 sum; |
| 725 | |
| 726 | csum = skb_checksum(skb, 0, skb->len, 0); |
| 727 | |
| 728 | /* skb->csum holds pseudo checksum */ |
| 729 | sum = csum_fold(csum_add(skb->csum, csum)); |
| 730 | if (likely(!sum)) { |
| 731 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 732 | !skb->csum_complete_sw) |
| 733 | netdev_rx_csum_fault(skb->dev); |
| 734 | } |
| 735 | |
| 736 | /* Save full packet checksum */ |
| 737 | skb->csum = csum; |
| 738 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 739 | skb->csum_complete_sw = 1; |
| 740 | skb->csum_valid = !sum; |
| 741 | |
| 742 | return sum; |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 743 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 744 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /** |
Masanari Iida | e793c0f | 2014-09-04 23:44:36 +0900 | [diff] [blame] | 747 | * skb_copy_and_csum_datagram_iovec - Copy and checksum skb to user iovec. |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 748 | * @skb: skbuff |
| 749 | * @hlen: hardware length |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 750 | * @iov: io vector |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 751 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | * Caller _must_ check that skb will fit to this iovec. |
| 753 | * |
| 754 | * Returns: 0 - success. |
| 755 | * -EINVAL - checksum failure. |
| 756 | * -EFAULT - fault during copy. Beware, in this case iovec |
| 757 | * can be modified! |
| 758 | */ |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 759 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | int hlen, struct iovec *iov) |
| 761 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 762 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | int chunk = skb->len - hlen; |
| 764 | |
Herbert Xu | ef8aef5 | 2007-09-06 14:06:35 +0100 | [diff] [blame] | 765 | if (!chunk) |
| 766 | return 0; |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* Skip filled elements. |
| 769 | * Pretty silly, look at memcpy_toiovec, though 8) |
| 770 | */ |
| 771 | while (!iov->iov_len) |
| 772 | iov++; |
| 773 | |
| 774 | if (iov->iov_len < chunk) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 775 | if (__skb_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | goto csum_error; |
| 777 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) |
| 778 | goto fault; |
| 779 | } else { |
| 780 | csum = csum_partial(skb->data, hlen, skb->csum); |
| 781 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, |
| 782 | chunk, &csum)) |
| 783 | goto fault; |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 784 | if (csum_fold(csum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | goto csum_error; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 786 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 787 | netdev_rx_csum_fault(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | iov->iov_len -= chunk; |
| 789 | iov->iov_base += chunk; |
| 790 | } |
| 791 | return 0; |
| 792 | csum_error: |
| 793 | return -EINVAL; |
| 794 | fault: |
| 795 | return -EFAULT; |
| 796 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 797 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | /** |
| 800 | * datagram_poll - generic datagram poll |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 801 | * @file: file struct |
| 802 | * @sock: socket |
| 803 | * @wait: poll table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 813 | unsigned int datagram_poll(struct file *file, struct socket *sock, |
| 814 | poll_table *wait) |
| 815 | { |
| 816 | struct sock *sk = sock->sk; |
| 817 | unsigned int mask; |
| 818 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 819 | sock_poll_wait(file, sk_sleep(sk), wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | mask = 0; |
| 821 | |
| 822 | /* exceptional events? */ |
| 823 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 824 | mask |= POLLERR | |
Jacob Keller | 8facd5f | 2013-04-02 13:55:40 -0700 | [diff] [blame] | 825 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 826 | |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 827 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 828 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 830 | mask |= POLLHUP; |
| 831 | |
| 832 | /* readable? */ |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 833 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | mask |= POLLIN | POLLRDNORM; |
| 835 | |
| 836 | /* Connection-based need to check for termination and startup */ |
| 837 | if (connection_based(sk)) { |
| 838 | if (sk->sk_state == TCP_CLOSE) |
| 839 | mask |= POLLHUP; |
| 840 | /* connection hasn't started yet? */ |
| 841 | if (sk->sk_state == TCP_SYN_SENT) |
| 842 | return mask; |
| 843 | } |
| 844 | |
| 845 | /* writable? */ |
| 846 | if (sock_writeable(sk)) |
| 847 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 848 | else |
| 849 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 850 | |
| 851 | return mask; |
| 852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | EXPORT_SYMBOL(datagram_poll); |