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