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