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; |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 285 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 286 | /* Copy header. */ |
| 287 | if (copy > 0) { |
| 288 | if (copy > len) |
| 289 | copy = len; |
| 290 | if (memcpy_toiovec(to, skb->data + offset, copy)) |
| 291 | goto fault; |
| 292 | if ((len -= copy) == 0) |
| 293 | return 0; |
| 294 | offset += copy; |
| 295 | } |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 296 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 297 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 298 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 299 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 301 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 302 | |
| 303 | end = start + skb_shinfo(skb)->frags[i].size; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 304 | if ((copy = end - offset) > 0) { |
| 305 | int err; |
| 306 | u8 *vaddr; |
| 307 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 308 | struct page *page = frag->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | if (copy > len) |
| 311 | copy = len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 312 | vaddr = kmap(page); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 313 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
| 314 | offset - start, copy); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 315 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (err) |
| 317 | goto fault; |
| 318 | if (!(len -= copy)) |
| 319 | return 0; |
| 320 | offset += copy; |
| 321 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 322 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 324 | |
| 325 | if (skb_shinfo(skb)->frag_list) { |
| 326 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 327 | |
| 328 | for (; list; list = list->next) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 329 | int end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 330 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 331 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 332 | |
| 333 | end = start + list->len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 334 | if ((copy = end - offset) > 0) { |
| 335 | if (copy > len) |
| 336 | copy = len; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 337 | if (skb_copy_datagram_iovec(list, |
| 338 | offset - start, |
| 339 | to, copy)) |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 340 | goto fault; |
| 341 | if ((len -= copy) == 0) |
| 342 | return 0; |
| 343 | offset += copy; |
| 344 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 345 | start = end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 348 | if (!len) |
| 349 | return 0; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | fault: |
| 352 | return -EFAULT; |
| 353 | } |
| 354 | |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 355 | /** |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 356 | * skb_copy_datagram_const_iovec - Copy a datagram to an iovec. |
| 357 | * @skb: buffer to copy |
| 358 | * @offset: offset in the buffer to start copying from |
| 359 | * @to: io vector to copy to |
| 360 | * @to_offset: offset in the io vector to start copying to |
| 361 | * @len: amount of data to copy from buffer to iovec |
| 362 | * |
| 363 | * Returns 0 or -EFAULT. |
| 364 | * Note: the iovec is not modified during the copy. |
| 365 | */ |
| 366 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, |
| 367 | const struct iovec *to, int to_offset, |
| 368 | int len) |
| 369 | { |
| 370 | int start = skb_headlen(skb); |
| 371 | int i, copy = start - offset; |
| 372 | |
| 373 | /* Copy header. */ |
| 374 | if (copy > 0) { |
| 375 | if (copy > len) |
| 376 | copy = len; |
| 377 | if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) |
| 378 | goto fault; |
| 379 | if ((len -= copy) == 0) |
| 380 | return 0; |
| 381 | offset += copy; |
| 382 | to_offset += copy; |
| 383 | } |
| 384 | |
| 385 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 386 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 387 | int end; |
| 388 | |
| 389 | WARN_ON(start > offset + len); |
| 390 | |
| 391 | end = start + skb_shinfo(skb)->frags[i].size; |
| 392 | if ((copy = end - offset) > 0) { |
| 393 | int err; |
| 394 | u8 *vaddr; |
| 395 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 396 | struct page *page = frag->page; |
| 397 | |
| 398 | if (copy > len) |
| 399 | copy = len; |
| 400 | vaddr = kmap(page); |
| 401 | err = memcpy_toiovecend(to, vaddr + frag->page_offset + |
| 402 | offset - start, to_offset, copy); |
| 403 | kunmap(page); |
| 404 | if (err) |
| 405 | goto fault; |
| 406 | if (!(len -= copy)) |
| 407 | return 0; |
| 408 | offset += copy; |
| 409 | to_offset += copy; |
| 410 | } |
| 411 | start = end; |
| 412 | } |
| 413 | |
| 414 | if (skb_shinfo(skb)->frag_list) { |
| 415 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 416 | |
| 417 | for (; list; list = list->next) { |
| 418 | int end; |
| 419 | |
| 420 | WARN_ON(start > offset + len); |
| 421 | |
| 422 | end = start + list->len; |
| 423 | if ((copy = end - offset) > 0) { |
| 424 | if (copy > len) |
| 425 | copy = len; |
| 426 | if (skb_copy_datagram_const_iovec(list, |
| 427 | offset - start, |
| 428 | to, to_offset, |
| 429 | copy)) |
| 430 | goto fault; |
| 431 | if ((len -= copy) == 0) |
| 432 | return 0; |
| 433 | offset += copy; |
| 434 | to_offset += copy; |
| 435 | } |
| 436 | start = end; |
| 437 | } |
| 438 | } |
| 439 | if (!len) |
| 440 | return 0; |
| 441 | |
| 442 | fault: |
| 443 | return -EFAULT; |
| 444 | } |
| 445 | EXPORT_SYMBOL(skb_copy_datagram_const_iovec); |
| 446 | |
| 447 | /** |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 448 | * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. |
| 449 | * @skb: buffer to copy |
| 450 | * @offset: offset in the buffer to start copying to |
| 451 | * @from: io vector to copy to |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 452 | * @from_offset: offset in the io vector to start copying from |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 453 | * @len: amount of data to copy to buffer from iovec |
| 454 | * |
| 455 | * Returns 0 or -EFAULT. |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 456 | * Note: the iovec is not modified during the copy. |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 457 | */ |
| 458 | 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] | 459 | const struct iovec *from, int from_offset, |
| 460 | int len) |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 461 | { |
| 462 | int start = skb_headlen(skb); |
| 463 | int i, copy = start - offset; |
| 464 | |
| 465 | /* Copy header. */ |
| 466 | if (copy > 0) { |
| 467 | if (copy > len) |
| 468 | copy = len; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 469 | if (memcpy_fromiovecend(skb->data + offset, from, 0, copy)) |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 470 | goto fault; |
| 471 | if ((len -= copy) == 0) |
| 472 | return 0; |
| 473 | offset += copy; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 474 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 478 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 479 | int end; |
| 480 | |
| 481 | WARN_ON(start > offset + len); |
| 482 | |
| 483 | end = start + skb_shinfo(skb)->frags[i].size; |
| 484 | if ((copy = end - offset) > 0) { |
| 485 | int err; |
| 486 | u8 *vaddr; |
| 487 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 488 | struct page *page = frag->page; |
| 489 | |
| 490 | if (copy > len) |
| 491 | copy = len; |
| 492 | vaddr = kmap(page); |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 493 | err = memcpy_fromiovecend(vaddr + frag->page_offset + |
| 494 | offset - start, |
| 495 | from, from_offset, copy); |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 496 | kunmap(page); |
| 497 | if (err) |
| 498 | goto fault; |
| 499 | |
| 500 | if (!(len -= copy)) |
| 501 | return 0; |
| 502 | offset += copy; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 503 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 504 | } |
| 505 | start = end; |
| 506 | } |
| 507 | |
| 508 | if (skb_shinfo(skb)->frag_list) { |
| 509 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 510 | |
| 511 | for (; list; list = list->next) { |
| 512 | int end; |
| 513 | |
| 514 | WARN_ON(start > offset + len); |
| 515 | |
| 516 | end = start + list->len; |
| 517 | if ((copy = end - offset) > 0) { |
| 518 | if (copy > len) |
| 519 | copy = len; |
| 520 | if (skb_copy_datagram_from_iovec(list, |
| 521 | offset - start, |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 522 | from, |
| 523 | from_offset, |
| 524 | copy)) |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 525 | goto fault; |
| 526 | if ((len -= copy) == 0) |
| 527 | return 0; |
| 528 | offset += copy; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 529 | from_offset += copy; |
Rusty Russell | db543c1 | 2008-08-15 15:13:53 -0700 | [diff] [blame] | 530 | } |
| 531 | start = end; |
| 532 | } |
| 533 | } |
| 534 | if (!len) |
| 535 | return 0; |
| 536 | |
| 537 | fault: |
| 538 | return -EFAULT; |
| 539 | } |
| 540 | EXPORT_SYMBOL(skb_copy_datagram_from_iovec); |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
| 543 | u8 __user *to, int len, |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 544 | __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 546 | int start = skb_headlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | int pos = 0; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 548 | int i, copy = start - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* Copy header. */ |
| 551 | if (copy > 0) { |
| 552 | int err = 0; |
| 553 | if (copy > len) |
| 554 | copy = len; |
| 555 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, |
| 556 | *csump, &err); |
| 557 | if (err) |
| 558 | goto fault; |
| 559 | if ((len -= copy) == 0) |
| 560 | return 0; |
| 561 | offset += copy; |
| 562 | to += copy; |
| 563 | pos = copy; |
| 564 | } |
| 565 | |
| 566 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 567 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 569 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 570 | |
| 571 | end = start + skb_shinfo(skb)->frags[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 573 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | int err = 0; |
| 575 | u8 *vaddr; |
| 576 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 577 | struct page *page = frag->page; |
| 578 | |
| 579 | if (copy > len) |
| 580 | copy = len; |
| 581 | vaddr = kmap(page); |
| 582 | csum2 = csum_and_copy_to_user(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 583 | frag->page_offset + |
| 584 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | to, copy, 0, &err); |
| 586 | kunmap(page); |
| 587 | if (err) |
| 588 | goto fault; |
| 589 | *csump = csum_block_add(*csump, csum2, pos); |
| 590 | if (!(len -= copy)) |
| 591 | return 0; |
| 592 | offset += copy; |
| 593 | to += copy; |
| 594 | pos += copy; |
| 595 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 596 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (skb_shinfo(skb)->frag_list) { |
| 600 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 601 | |
| 602 | for (; list; list=list->next) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 603 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 605 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 606 | |
| 607 | end = start + list->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 609 | __wsum csum2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (copy > len) |
| 611 | copy = len; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 612 | if (skb_copy_and_csum_datagram(list, |
| 613 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | to, copy, |
| 615 | &csum2)) |
| 616 | goto fault; |
| 617 | *csump = csum_block_add(*csump, csum2, pos); |
| 618 | if ((len -= copy) == 0) |
| 619 | return 0; |
| 620 | offset += copy; |
| 621 | to += copy; |
| 622 | pos += copy; |
| 623 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 624 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | if (!len) |
| 628 | return 0; |
| 629 | |
| 630 | fault: |
| 631 | return -EFAULT; |
| 632 | } |
| 633 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 634 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 635 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 636 | __sum16 sum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 637 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 638 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 639 | if (likely(!sum)) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 640 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 641 | netdev_rx_csum_fault(skb->dev); |
| 642 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 643 | } |
| 644 | return sum; |
| 645 | } |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 646 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 647 | |
| 648 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 649 | { |
| 650 | return __skb_checksum_complete_head(skb, skb->len); |
| 651 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 652 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /** |
| 655 | * 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] | 656 | * @skb: skbuff |
| 657 | * @hlen: hardware length |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 658 | * @iov: io vector |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 659 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * Caller _must_ check that skb will fit to this iovec. |
| 661 | * |
| 662 | * Returns: 0 - success. |
| 663 | * -EINVAL - checksum failure. |
| 664 | * -EFAULT - fault during copy. Beware, in this case iovec |
| 665 | * can be modified! |
| 666 | */ |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 667 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | int hlen, struct iovec *iov) |
| 669 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 670 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | int chunk = skb->len - hlen; |
| 672 | |
Herbert Xu | ef8aef5 | 2007-09-06 14:06:35 +0100 | [diff] [blame] | 673 | if (!chunk) |
| 674 | return 0; |
| 675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | /* Skip filled elements. |
| 677 | * Pretty silly, look at memcpy_toiovec, though 8) |
| 678 | */ |
| 679 | while (!iov->iov_len) |
| 680 | iov++; |
| 681 | |
| 682 | if (iov->iov_len < chunk) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 683 | if (__skb_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | goto csum_error; |
| 685 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) |
| 686 | goto fault; |
| 687 | } else { |
| 688 | csum = csum_partial(skb->data, hlen, skb->csum); |
| 689 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, |
| 690 | chunk, &csum)) |
| 691 | goto fault; |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 692 | if (csum_fold(csum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | goto csum_error; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 694 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 695 | netdev_rx_csum_fault(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | iov->iov_len -= chunk; |
| 697 | iov->iov_base += chunk; |
| 698 | } |
| 699 | return 0; |
| 700 | csum_error: |
| 701 | return -EINVAL; |
| 702 | fault: |
| 703 | return -EFAULT; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * datagram_poll - generic datagram poll |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 708 | * @file: file struct |
| 709 | * @sock: socket |
| 710 | * @wait: poll table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | * |
| 712 | * Datagram poll: Again totally generic. This also handles |
| 713 | * sequenced packet sockets providing the socket receive queue |
| 714 | * is only ever holding data ready to receive. |
| 715 | * |
| 716 | * Note: when you _don't_ use this routine for this protocol, |
| 717 | * and you use a different write policy from sock_writeable() |
| 718 | * then please supply your own write_space callback. |
| 719 | */ |
| 720 | unsigned int datagram_poll(struct file *file, struct socket *sock, |
| 721 | poll_table *wait) |
| 722 | { |
| 723 | struct sock *sk = sock->sk; |
| 724 | unsigned int mask; |
| 725 | |
| 726 | poll_wait(file, sk->sk_sleep, wait); |
| 727 | mask = 0; |
| 728 | |
| 729 | /* exceptional events? */ |
| 730 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 731 | mask |= POLLERR; |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 732 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 733 | mask |= POLLRDHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 735 | mask |= POLLHUP; |
| 736 | |
| 737 | /* readable? */ |
| 738 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
| 739 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 740 | mask |= POLLIN | POLLRDNORM; |
| 741 | |
| 742 | /* Connection-based need to check for termination and startup */ |
| 743 | if (connection_based(sk)) { |
| 744 | if (sk->sk_state == TCP_CLOSE) |
| 745 | mask |= POLLHUP; |
| 746 | /* connection hasn't started yet? */ |
| 747 | if (sk->sk_state == TCP_SYN_SENT) |
| 748 | return mask; |
| 749 | } |
| 750 | |
| 751 | /* writable? */ |
| 752 | if (sock_writeable(sk)) |
| 753 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 754 | else |
| 755 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 756 | |
| 757 | return mask; |
| 758 | } |
| 759 | |
| 760 | EXPORT_SYMBOL(datagram_poll); |
| 761 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
| 762 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
| 763 | EXPORT_SYMBOL(skb_free_datagram); |
| 764 | EXPORT_SYMBOL(skb_recv_datagram); |