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) { |
Eric Dumazet | 77c1090 | 2013-02-12 06:16:53 +0000 | [diff] [blame^] | 190 | if (*off >= skb->len && skb->len) { |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 191 | *off -= skb->len; |
| 192 | continue; |
| 193 | } |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 194 | skb->peeked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | atomic_inc(&skb->users); |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 196 | } else |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 197 | __skb_unlink(skb, queue); |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 198 | |
| 199 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
| 200 | return skb; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 201 | } |
Pavel Emelyanov | 4934b03 | 2012-02-21 07:30:33 +0000 | [diff] [blame] | 202 | spin_unlock_irqrestore(&queue->lock, cpu_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* User doesn't want to wait */ |
| 205 | error = -EAGAIN; |
| 206 | if (!timeo) |
| 207 | goto no_packet; |
| 208 | |
| 209 | } while (!wait_for_packet(sk, err, &timeo)); |
| 210 | |
| 211 | return NULL; |
| 212 | |
| 213 | no_packet: |
| 214 | *err = error; |
| 215 | return NULL; |
| 216 | } |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 217 | EXPORT_SYMBOL(__skb_recv_datagram); |
| 218 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 219 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags, |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 220 | int noblock, int *err) |
| 221 | { |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 222 | int peeked, off = 0; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 223 | |
| 224 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
Pavel Emelyanov | 3f518bf | 2012-02-21 07:30:58 +0000 | [diff] [blame] | 225 | &peeked, &off, err); |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 226 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 227 | EXPORT_SYMBOL(skb_recv_datagram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) |
| 230 | { |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 231 | consume_skb(skb); |
Eric Dumazet | 270acef | 2008-11-05 01:38:06 -0800 | [diff] [blame] | 232 | sk_mem_reclaim_partial(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 234 | EXPORT_SYMBOL(skb_free_datagram); |
| 235 | |
| 236 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb) |
| 237 | { |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 238 | bool slow; |
| 239 | |
Eric Dumazet | 93bb64e | 2010-05-03 23:18:14 -0700 | [diff] [blame] | 240 | if (likely(atomic_read(&skb->users) == 1)) |
| 241 | smp_rmb(); |
| 242 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 243 | return; |
| 244 | |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 245 | slow = lock_sock_fast(sk); |
Eric Dumazet | 4b0b72f | 2010-04-28 14:35:48 -0700 | [diff] [blame] | 246 | skb_orphan(skb); |
| 247 | sk_mem_reclaim_partial(sk); |
Eric Dumazet | 8a74ad6 | 2010-05-26 19:20:18 +0000 | [diff] [blame] | 248 | unlock_sock_fast(sk, slow); |
Eric Dumazet | 4b0b72f | 2010-04-28 14:35:48 -0700 | [diff] [blame] | 249 | |
Eric Dumazet | 93bb64e | 2010-05-03 23:18:14 -0700 | [diff] [blame] | 250 | /* skb is now orphaned, can be freed outside of locked section */ |
| 251 | __kfree_skb(skb); |
Eric Dumazet | 9d410c7 | 2009-10-30 05:03:53 +0000 | [diff] [blame] | 252 | } |
| 253 | EXPORT_SYMBOL(skb_free_datagram_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /** |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 256 | * skb_kill_datagram - Free a datagram skbuff forcibly |
| 257 | * @sk: socket |
| 258 | * @skb: datagram skbuff |
| 259 | * @flags: MSG_ flags |
| 260 | * |
| 261 | * This function frees a datagram skbuff that was received by |
| 262 | * skb_recv_datagram. The flags argument must match the one |
| 263 | * used for skb_recv_datagram. |
| 264 | * |
| 265 | * If the MSG_PEEK flag is set, and the packet is still on the |
| 266 | * receive queue of the socket, it will be taken off the queue |
| 267 | * before it is freed. |
| 268 | * |
| 269 | * This function currently only disables BH when acquiring the |
| 270 | * sk_receive_queue lock. Therefore it must not be used in a |
| 271 | * context where that lock is acquired in an IRQ context. |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 272 | * |
| 273 | * It returns 0 if the packet was removed by us. |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 274 | */ |
| 275 | |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 276 | 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] | 277 | { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 278 | int err = 0; |
| 279 | |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 280 | if (flags & MSG_PEEK) { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 281 | err = -ENOENT; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 282 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 283 | if (skb == skb_peek(&sk->sk_receive_queue)) { |
| 284 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 285 | atomic_dec(&skb->users); |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 286 | err = 0; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 287 | } |
| 288 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 289 | } |
| 290 | |
John Dykstra | 61de71c | 2009-05-08 14:57:01 -0700 | [diff] [blame] | 291 | kfree_skb(skb); |
Eric Dumazet | 8edf19c | 2009-10-15 00:12:40 +0000 | [diff] [blame] | 292 | atomic_inc(&sk->sk_drops); |
John Dykstra | 61de71c | 2009-05-08 14:57:01 -0700 | [diff] [blame] | 293 | sk_mem_reclaim_partial(sk); |
| 294 | |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 295 | return err; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 296 | } |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 297 | EXPORT_SYMBOL(skb_kill_datagram); |
| 298 | |
| 299 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 301 | * @skb: buffer to copy |
| 302 | * @offset: offset in the buffer to start copying from |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 303 | * @to: io vector to copy to |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 304 | * @len: amount of data to copy from buffer to iovec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * |
| 306 | * Note: the iovec is modified during the copy. |
| 307 | */ |
| 308 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, |
| 309 | struct iovec *to, int len) |
| 310 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 311 | int start = skb_headlen(skb); |
| 312 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 313 | struct sk_buff *frag_iter; |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 314 | |
Neil Horman | e9b3cc1 | 2009-08-13 05:19:44 +0000 | [diff] [blame] | 315 | trace_skb_copy_datagram_iovec(skb, len); |
| 316 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 317 | /* Copy header. */ |
| 318 | if (copy > 0) { |
| 319 | if (copy > len) |
| 320 | copy = len; |
| 321 | if (memcpy_toiovec(to, skb->data + offset, copy)) |
| 322 | goto fault; |
| 323 | if ((len -= copy) == 0) |
| 324 | return 0; |
| 325 | offset += copy; |
| 326 | } |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 327 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 328 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 329 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 330 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 331 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 333 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 334 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 335 | end = start + skb_frag_size(frag); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 336 | if ((copy = end - offset) > 0) { |
| 337 | int err; |
| 338 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 339 | struct page *page = skb_frag_page(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | if (copy > len) |
| 342 | copy = len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 343 | vaddr = kmap(page); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 344 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
| 345 | offset - start, copy); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 346 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (err) |
| 348 | goto fault; |
| 349 | if (!(len -= copy)) |
| 350 | return 0; |
| 351 | offset += copy; |
| 352 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 353 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 355 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 356 | skb_walk_frags(skb, frag_iter) { |
| 357 | int end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 358 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 359 | WARN_ON(start > offset + len); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 360 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 361 | end = start + frag_iter->len; |
| 362 | if ((copy = end - offset) > 0) { |
| 363 | if (copy > len) |
| 364 | copy = len; |
| 365 | if (skb_copy_datagram_iovec(frag_iter, |
| 366 | offset - start, |
| 367 | to, copy)) |
| 368 | goto fault; |
| 369 | if ((len -= copy) == 0) |
| 370 | return 0; |
| 371 | offset += copy; |
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 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 375 | if (!len) |
| 376 | return 0; |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | fault: |
| 379 | return -EFAULT; |
| 380 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 381 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 383 | /** |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 384 | * skb_copy_datagram_const_iovec - Copy a datagram to an iovec. |
| 385 | * @skb: buffer to copy |
| 386 | * @offset: offset in the buffer to start copying from |
| 387 | * @to: io vector to copy to |
| 388 | * @to_offset: offset in the io vector to start copying to |
| 389 | * @len: amount of data to copy from buffer to iovec |
| 390 | * |
| 391 | * Returns 0 or -EFAULT. |
| 392 | * Note: the iovec is not modified during the copy. |
| 393 | */ |
| 394 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, |
| 395 | const struct iovec *to, int to_offset, |
| 396 | int len) |
| 397 | { |
| 398 | int start = skb_headlen(skb); |
| 399 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 400 | struct sk_buff *frag_iter; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 401 | |
| 402 | /* Copy header. */ |
| 403 | if (copy > 0) { |
| 404 | if (copy > len) |
| 405 | copy = len; |
| 406 | if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) |
| 407 | goto fault; |
| 408 | if ((len -= copy) == 0) |
| 409 | return 0; |
| 410 | offset += copy; |
| 411 | to_offset += copy; |
| 412 | } |
| 413 | |
| 414 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 415 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 416 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 417 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 418 | |
| 419 | WARN_ON(start > offset + len); |
| 420 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 421 | end = start + skb_frag_size(frag); |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 422 | if ((copy = end - offset) > 0) { |
| 423 | int err; |
| 424 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 425 | struct page *page = skb_frag_page(frag); |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 426 | |
| 427 | if (copy > len) |
| 428 | copy = len; |
| 429 | vaddr = kmap(page); |
| 430 | err = memcpy_toiovecend(to, vaddr + frag->page_offset + |
| 431 | offset - start, to_offset, copy); |
| 432 | kunmap(page); |
| 433 | if (err) |
| 434 | goto fault; |
| 435 | if (!(len -= copy)) |
| 436 | return 0; |
| 437 | offset += copy; |
| 438 | to_offset += copy; |
| 439 | } |
| 440 | start = end; |
| 441 | } |
| 442 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 443 | skb_walk_frags(skb, frag_iter) { |
| 444 | int end; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 445 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 446 | WARN_ON(start > offset + len); |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 447 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 448 | end = start + frag_iter->len; |
| 449 | if ((copy = end - offset) > 0) { |
| 450 | if (copy > len) |
| 451 | copy = len; |
| 452 | if (skb_copy_datagram_const_iovec(frag_iter, |
| 453 | offset - start, |
| 454 | to, to_offset, |
| 455 | copy)) |
| 456 | goto fault; |
| 457 | if ((len -= copy) == 0) |
| 458 | return 0; |
| 459 | offset += copy; |
| 460 | to_offset += copy; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 461 | } |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 462 | start = end; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 463 | } |
| 464 | if (!len) |
| 465 | return 0; |
| 466 | |
| 467 | fault: |
| 468 | return -EFAULT; |
| 469 | } |
| 470 | EXPORT_SYMBOL(skb_copy_datagram_const_iovec); |
| 471 | |
| 472 | /** |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 473 | * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. |
| 474 | * @skb: buffer to copy |
| 475 | * @offset: offset in the buffer to start copying to |
| 476 | * @from: io vector to copy to |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 477 | * @from_offset: offset in the io vector to start copying from |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 478 | * @len: amount of data to copy to buffer from iovec |
| 479 | * |
| 480 | * Returns 0 or -EFAULT. |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 481 | * Note: the iovec is not modified during the copy. |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 482 | */ |
| 483 | 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] | 484 | const struct iovec *from, int from_offset, |
| 485 | int len) |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 486 | { |
| 487 | int start = skb_headlen(skb); |
| 488 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 489 | struct sk_buff *frag_iter; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 490 | |
| 491 | /* Copy header. */ |
| 492 | if (copy > 0) { |
| 493 | if (copy > len) |
| 494 | copy = len; |
Sridhar Samudrala | d2d27bf | 2009-06-05 09:35:40 +0000 | [diff] [blame] | 495 | if (memcpy_fromiovecend(skb->data + offset, from, from_offset, |
| 496 | copy)) |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 497 | goto fault; |
| 498 | if ((len -= copy) == 0) |
| 499 | return 0; |
| 500 | offset += copy; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 501 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 505 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 506 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 507 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 508 | |
| 509 | WARN_ON(start > offset + len); |
| 510 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 511 | end = start + skb_frag_size(frag); |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 512 | if ((copy = end - offset) > 0) { |
| 513 | int err; |
| 514 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 515 | struct page *page = skb_frag_page(frag); |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 516 | |
| 517 | if (copy > len) |
| 518 | copy = len; |
| 519 | vaddr = kmap(page); |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 520 | err = memcpy_fromiovecend(vaddr + frag->page_offset + |
| 521 | offset - start, |
| 522 | from, from_offset, copy); |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 523 | kunmap(page); |
| 524 | if (err) |
| 525 | goto fault; |
| 526 | |
| 527 | if (!(len -= copy)) |
| 528 | return 0; |
| 529 | offset += copy; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 530 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 531 | } |
| 532 | start = end; |
| 533 | } |
| 534 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 535 | skb_walk_frags(skb, frag_iter) { |
| 536 | int end; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 537 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 538 | WARN_ON(start > offset + len); |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 539 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 540 | end = start + frag_iter->len; |
| 541 | if ((copy = end - offset) > 0) { |
| 542 | if (copy > len) |
| 543 | copy = len; |
| 544 | if (skb_copy_datagram_from_iovec(frag_iter, |
| 545 | offset - start, |
| 546 | from, |
| 547 | from_offset, |
| 548 | copy)) |
| 549 | goto fault; |
| 550 | if ((len -= copy) == 0) |
| 551 | return 0; |
| 552 | offset += copy; |
| 553 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 554 | } |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 555 | start = end; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 556 | } |
| 557 | if (!len) |
| 558 | return 0; |
| 559 | |
| 560 | fault: |
| 561 | return -EFAULT; |
| 562 | } |
| 563 | EXPORT_SYMBOL(skb_copy_datagram_from_iovec); |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
| 566 | u8 __user *to, int len, |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 567 | __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 569 | int start = skb_headlen(skb); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 570 | int i, copy = start - offset; |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 571 | struct sk_buff *frag_iter; |
| 572 | int pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* Copy header. */ |
| 575 | if (copy > 0) { |
| 576 | int err = 0; |
| 577 | if (copy > len) |
| 578 | copy = len; |
| 579 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, |
| 580 | *csump, &err); |
| 581 | if (err) |
| 582 | goto fault; |
| 583 | if ((len -= copy) == 0) |
| 584 | return 0; |
| 585 | offset += copy; |
| 586 | to += copy; |
| 587 | pos = copy; |
| 588 | } |
| 589 | |
| 590 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 591 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 592 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 594 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 595 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 596 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 598 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | int err = 0; |
| 600 | u8 *vaddr; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 601 | struct page *page = skb_frag_page(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | if (copy > len) |
| 604 | copy = len; |
| 605 | vaddr = kmap(page); |
| 606 | csum2 = csum_and_copy_to_user(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 607 | frag->page_offset + |
| 608 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | to, copy, 0, &err); |
| 610 | kunmap(page); |
| 611 | if (err) |
| 612 | goto fault; |
| 613 | *csump = csum_block_add(*csump, csum2, pos); |
| 614 | if (!(len -= copy)) |
| 615 | return 0; |
| 616 | offset += copy; |
| 617 | to += copy; |
| 618 | pos += copy; |
| 619 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 620 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 623 | skb_walk_frags(skb, frag_iter) { |
| 624 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 626 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 628 | end = start + frag_iter->len; |
| 629 | if ((copy = end - offset) > 0) { |
| 630 | __wsum csum2 = 0; |
| 631 | if (copy > len) |
| 632 | copy = len; |
| 633 | if (skb_copy_and_csum_datagram(frag_iter, |
| 634 | offset - start, |
| 635 | to, copy, |
| 636 | &csum2)) |
| 637 | goto fault; |
| 638 | *csump = csum_block_add(*csump, csum2, pos); |
| 639 | if ((len -= copy) == 0) |
| 640 | return 0; |
| 641 | offset += copy; |
| 642 | to += copy; |
| 643 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
David S. Miller | 5b1a002 | 2009-06-09 00:18:15 -0700 | [diff] [blame] | 645 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | if (!len) |
| 648 | return 0; |
| 649 | |
| 650 | fault: |
| 651 | return -EFAULT; |
| 652 | } |
| 653 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 654 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 655 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 656 | __sum16 sum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 657 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 658 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 659 | if (likely(!sum)) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 660 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 661 | netdev_rx_csum_fault(skb->dev); |
| 662 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 663 | } |
| 664 | return sum; |
| 665 | } |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 666 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 667 | |
| 668 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 669 | { |
| 670 | return __skb_checksum_complete_head(skb, skb->len); |
| 671 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 672 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /** |
| 675 | * 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] | 676 | * @skb: skbuff |
| 677 | * @hlen: hardware length |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 678 | * @iov: io vector |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 679 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | * Caller _must_ check that skb will fit to this iovec. |
| 681 | * |
| 682 | * Returns: 0 - success. |
| 683 | * -EINVAL - checksum failure. |
| 684 | * -EFAULT - fault during copy. Beware, in this case iovec |
| 685 | * can be modified! |
| 686 | */ |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 687 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | int hlen, struct iovec *iov) |
| 689 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 690 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | int chunk = skb->len - hlen; |
| 692 | |
Herbert Xu | ef8aef5 | 2007-09-06 14:06:35 +0100 | [diff] [blame] | 693 | if (!chunk) |
| 694 | return 0; |
| 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | /* Skip filled elements. |
| 697 | * Pretty silly, look at memcpy_toiovec, though 8) |
| 698 | */ |
| 699 | while (!iov->iov_len) |
| 700 | iov++; |
| 701 | |
| 702 | if (iov->iov_len < chunk) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 703 | if (__skb_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | goto csum_error; |
| 705 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) |
| 706 | goto fault; |
| 707 | } else { |
| 708 | csum = csum_partial(skb->data, hlen, skb->csum); |
| 709 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, |
| 710 | chunk, &csum)) |
| 711 | goto fault; |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 712 | if (csum_fold(csum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | goto csum_error; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 714 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 715 | netdev_rx_csum_fault(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | iov->iov_len -= chunk; |
| 717 | iov->iov_base += chunk; |
| 718 | } |
| 719 | return 0; |
| 720 | csum_error: |
| 721 | return -EINVAL; |
| 722 | fault: |
| 723 | return -EFAULT; |
| 724 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 725 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | /** |
| 728 | * datagram_poll - generic datagram poll |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 729 | * @file: file struct |
| 730 | * @sock: socket |
| 731 | * @wait: poll table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | * |
| 733 | * Datagram poll: Again totally generic. This also handles |
| 734 | * sequenced packet sockets providing the socket receive queue |
| 735 | * is only ever holding data ready to receive. |
| 736 | * |
| 737 | * Note: when you _don't_ use this routine for this protocol, |
| 738 | * and you use a different write policy from sock_writeable() |
| 739 | * then please supply your own write_space callback. |
| 740 | */ |
| 741 | unsigned int datagram_poll(struct file *file, struct socket *sock, |
| 742 | poll_table *wait) |
| 743 | { |
| 744 | struct sock *sk = sock->sk; |
| 745 | unsigned int mask; |
| 746 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 747 | sock_poll_wait(file, sk_sleep(sk), wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | mask = 0; |
| 749 | |
| 750 | /* exceptional events? */ |
| 751 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 752 | mask |= POLLERR; |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 753 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 754 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 756 | mask |= POLLHUP; |
| 757 | |
| 758 | /* readable? */ |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 759 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | mask |= POLLIN | POLLRDNORM; |
| 761 | |
| 762 | /* Connection-based need to check for termination and startup */ |
| 763 | if (connection_based(sk)) { |
| 764 | if (sk->sk_state == TCP_CLOSE) |
| 765 | mask |= POLLHUP; |
| 766 | /* connection hasn't started yet? */ |
| 767 | if (sk->sk_state == TCP_SYN_SENT) |
| 768 | return mask; |
| 769 | } |
| 770 | |
| 771 | /* writable? */ |
| 772 | if (sock_writeable(sk)) |
| 773 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 774 | else |
| 775 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 776 | |
| 777 | return mask; |
| 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | EXPORT_SYMBOL(datagram_poll); |