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