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