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