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 | * |
| 12 | * Authors: Alan Cox <alan@redhat.com>. (datagram_poll() from old |
| 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 | |
| 67 | /* |
| 68 | * Wait for a packet.. |
| 69 | */ |
| 70 | static int wait_for_packet(struct sock *sk, int *err, long *timeo_p) |
| 71 | { |
| 72 | int error; |
| 73 | DEFINE_WAIT(wait); |
| 74 | |
| 75 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); |
| 76 | |
| 77 | /* Socket errors? */ |
| 78 | error = sock_error(sk); |
| 79 | if (error) |
| 80 | goto out_err; |
| 81 | |
| 82 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 83 | goto out; |
| 84 | |
| 85 | /* Socket shut down? */ |
| 86 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 87 | goto out_noerr; |
| 88 | |
| 89 | /* Sequenced packets can come disconnected. |
| 90 | * If so we report the problem |
| 91 | */ |
| 92 | error = -ENOTCONN; |
| 93 | if (connection_based(sk) && |
| 94 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) |
| 95 | goto out_err; |
| 96 | |
| 97 | /* handle signals */ |
| 98 | if (signal_pending(current)) |
| 99 | goto interrupted; |
| 100 | |
| 101 | error = 0; |
| 102 | *timeo_p = schedule_timeout(*timeo_p); |
| 103 | out: |
| 104 | finish_wait(sk->sk_sleep, &wait); |
| 105 | return error; |
| 106 | interrupted: |
| 107 | error = sock_intr_errno(*timeo_p); |
| 108 | out_err: |
| 109 | *err = error; |
| 110 | goto out; |
| 111 | out_noerr: |
| 112 | *err = 0; |
| 113 | error = 1; |
| 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | /** |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 118 | * __skb_recv_datagram - Receive a datagram skbuff |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 119 | * @sk: socket |
| 120 | * @flags: MSG_ flags |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 121 | * @peeked: returns non-zero if this packet has been seen before |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 122 | * @err: error code returned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | * |
| 124 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups |
| 125 | * and possible races. This replaces identical code in packet, raw and |
| 126 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes |
| 127 | * the long standing peek and read race for datagram sockets. If you |
| 128 | * alter this routine remember it must be re-entrant. |
| 129 | * |
| 130 | * This function will lock the socket if a skb is returned, so the caller |
| 131 | * needs to unlock the socket in that case (usually by calling |
| 132 | * skb_free_datagram) |
| 133 | * |
| 134 | * * It does not lock socket since today. This function is |
| 135 | * * free of race conditions. This measure should/can improve |
| 136 | * * significantly datagram socket latencies at high loads, |
| 137 | * * when data copying to user space takes lots of time. |
| 138 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet |
| 139 | * * 8) Great win.) |
| 140 | * * --ANK (980729) |
| 141 | * |
| 142 | * The order of the tests when we find no data waiting are specified |
| 143 | * quite explicitly by POSIX 1003.1g, don't change them without having |
| 144 | * the standard around please. |
| 145 | */ |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 146 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags, |
| 147 | int *peeked, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | struct sk_buff *skb; |
| 150 | long timeo; |
| 151 | /* |
| 152 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() |
| 153 | */ |
| 154 | int error = sock_error(sk); |
| 155 | |
| 156 | if (error) |
| 157 | goto no_packet; |
| 158 | |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 159 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | do { |
| 162 | /* Again only user level code calls this function, so nothing |
| 163 | * interrupt level will suddenly eat the receive_queue. |
| 164 | * |
| 165 | * Look at current nfs client by the way... |
| 166 | * However, this function was corrent in any case. 8) |
| 167 | */ |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 168 | unsigned long cpu_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 170 | spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags); |
| 171 | skb = skb_peek(&sk->sk_receive_queue); |
| 172 | if (skb) { |
| 173 | *peeked = skb->peeked; |
| 174 | if (flags & MSG_PEEK) { |
| 175 | skb->peeked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | atomic_inc(&skb->users); |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 177 | } else |
| 178 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 179 | } |
| 180 | spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | if (skb) |
| 183 | return skb; |
| 184 | |
| 185 | /* User doesn't want to wait */ |
| 186 | error = -EAGAIN; |
| 187 | if (!timeo) |
| 188 | goto no_packet; |
| 189 | |
| 190 | } while (!wait_for_packet(sk, err, &timeo)); |
| 191 | |
| 192 | return NULL; |
| 193 | |
| 194 | no_packet: |
| 195 | *err = error; |
| 196 | return NULL; |
| 197 | } |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 198 | EXPORT_SYMBOL(__skb_recv_datagram); |
| 199 | |
| 200 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, |
| 201 | int noblock, int *err) |
| 202 | { |
| 203 | int peeked; |
| 204 | |
| 205 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
| 206 | &peeked, err); |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) |
| 210 | { |
| 211 | kfree_skb(skb); |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 212 | sk_mem_reclaim(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 216 | * skb_kill_datagram - Free a datagram skbuff forcibly |
| 217 | * @sk: socket |
| 218 | * @skb: datagram skbuff |
| 219 | * @flags: MSG_ flags |
| 220 | * |
| 221 | * This function frees a datagram skbuff that was received by |
| 222 | * skb_recv_datagram. The flags argument must match the one |
| 223 | * used for skb_recv_datagram. |
| 224 | * |
| 225 | * If the MSG_PEEK flag is set, and the packet is still on the |
| 226 | * receive queue of the socket, it will be taken off the queue |
| 227 | * before it is freed. |
| 228 | * |
| 229 | * This function currently only disables BH when acquiring the |
| 230 | * sk_receive_queue lock. Therefore it must not be used in a |
| 231 | * context where that lock is acquired in an IRQ context. |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 232 | * |
| 233 | * It returns 0 if the packet was removed by us. |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 234 | */ |
| 235 | |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 236 | 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] | 237 | { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 238 | int err = 0; |
| 239 | |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 240 | if (flags & MSG_PEEK) { |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 241 | err = -ENOENT; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 242 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 243 | if (skb == skb_peek(&sk->sk_receive_queue)) { |
| 244 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 245 | atomic_dec(&skb->users); |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 246 | err = 0; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 247 | } |
| 248 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 249 | } |
| 250 | |
| 251 | kfree_skb(skb); |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 252 | sk_mem_reclaim(sk); |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 253 | return err; |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | EXPORT_SYMBOL(skb_kill_datagram); |
| 257 | |
| 258 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 260 | * @skb: buffer to copy |
| 261 | * @offset: offset in the buffer to start copying from |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 262 | * @to: io vector to copy to |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 263 | * @len: amount of data to copy from buffer to iovec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | * |
| 265 | * Note: the iovec is modified during the copy. |
| 266 | */ |
| 267 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, |
| 268 | struct iovec *to, int len) |
| 269 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 270 | int start = skb_headlen(skb); |
| 271 | int i, copy = start - offset; |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 272 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 273 | /* Copy header. */ |
| 274 | if (copy > 0) { |
| 275 | if (copy > len) |
| 276 | copy = len; |
| 277 | if (memcpy_toiovec(to, skb->data + offset, copy)) |
| 278 | goto fault; |
| 279 | if ((len -= copy) == 0) |
| 280 | return 0; |
| 281 | offset += copy; |
| 282 | } |
Herbert Xu | c75d721 | 2005-11-02 18:55:00 +1100 | [diff] [blame] | 283 | |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 284 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
| 285 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 286 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 288 | BUG_TRAP(start <= offset + len); |
| 289 | |
| 290 | end = start + skb_shinfo(skb)->frags[i].size; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 291 | if ((copy = end - offset) > 0) { |
| 292 | int err; |
| 293 | u8 *vaddr; |
| 294 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 295 | struct page *page = frag->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | if (copy > len) |
| 298 | copy = len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 299 | vaddr = kmap(page); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 300 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
| 301 | offset - start, copy); |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 302 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (err) |
| 304 | goto fault; |
| 305 | if (!(len -= copy)) |
| 306 | return 0; |
| 307 | offset += copy; |
| 308 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 309 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 311 | |
| 312 | if (skb_shinfo(skb)->frag_list) { |
| 313 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 314 | |
| 315 | for (; list; list = list->next) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 316 | int end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 317 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 318 | BUG_TRAP(start <= offset + len); |
| 319 | |
| 320 | end = start + list->len; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 321 | if ((copy = end - offset) > 0) { |
| 322 | if (copy > len) |
| 323 | copy = len; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 324 | if (skb_copy_datagram_iovec(list, |
| 325 | offset - start, |
| 326 | to, copy)) |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 327 | goto fault; |
| 328 | if ((len -= copy) == 0) |
| 329 | return 0; |
| 330 | offset += copy; |
| 331 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 332 | start = end; |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
David S. Miller | b4d9eda | 2006-02-13 16:06:10 -0800 | [diff] [blame] | 335 | if (!len) |
| 336 | return 0; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | fault: |
| 339 | return -EFAULT; |
| 340 | } |
| 341 | |
| 342 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
| 343 | u8 __user *to, int len, |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 344 | __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 346 | int start = skb_headlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | int pos = 0; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 348 | int i, copy = start - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | /* Copy header. */ |
| 351 | if (copy > 0) { |
| 352 | int err = 0; |
| 353 | if (copy > len) |
| 354 | copy = len; |
| 355 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, |
| 356 | *csump, &err); |
| 357 | if (err) |
| 358 | goto fault; |
| 359 | if ((len -= copy) == 0) |
| 360 | return 0; |
| 361 | offset += copy; |
| 362 | to += copy; |
| 363 | pos = copy; |
| 364 | } |
| 365 | |
| 366 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 367 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 369 | BUG_TRAP(start <= offset + len); |
| 370 | |
| 371 | end = start + skb_shinfo(skb)->frags[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 373 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | int err = 0; |
| 375 | u8 *vaddr; |
| 376 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 377 | struct page *page = frag->page; |
| 378 | |
| 379 | if (copy > len) |
| 380 | copy = len; |
| 381 | vaddr = kmap(page); |
| 382 | csum2 = csum_and_copy_to_user(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 383 | frag->page_offset + |
| 384 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | to, copy, 0, &err); |
| 386 | kunmap(page); |
| 387 | if (err) |
| 388 | goto fault; |
| 389 | *csump = csum_block_add(*csump, csum2, pos); |
| 390 | if (!(len -= copy)) |
| 391 | return 0; |
| 392 | offset += copy; |
| 393 | to += copy; |
| 394 | pos += copy; |
| 395 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 396 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | if (skb_shinfo(skb)->frag_list) { |
| 400 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 401 | |
| 402 | for (; list; list=list->next) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 403 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 405 | BUG_TRAP(start <= offset + len); |
| 406 | |
| 407 | end = start + list->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 409 | __wsum csum2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (copy > len) |
| 411 | copy = len; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 412 | if (skb_copy_and_csum_datagram(list, |
| 413 | offset - start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | to, copy, |
| 415 | &csum2)) |
| 416 | goto fault; |
| 417 | *csump = csum_block_add(*csump, csum2, pos); |
| 418 | if ((len -= copy) == 0) |
| 419 | return 0; |
| 420 | offset += copy; |
| 421 | to += copy; |
| 422 | pos += copy; |
| 423 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 424 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | if (!len) |
| 428 | return 0; |
| 429 | |
| 430 | fault: |
| 431 | return -EFAULT; |
| 432 | } |
| 433 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 434 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 435 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 436 | __sum16 sum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 437 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 438 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 439 | if (likely(!sum)) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 440 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 441 | netdev_rx_csum_fault(skb->dev); |
| 442 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 443 | } |
| 444 | return sum; |
| 445 | } |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 446 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
| 447 | |
| 448 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
| 449 | { |
| 450 | return __skb_checksum_complete_head(skb, skb->len); |
| 451 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 452 | EXPORT_SYMBOL(__skb_checksum_complete); |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /** |
| 455 | * 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] | 456 | * @skb: skbuff |
| 457 | * @hlen: hardware length |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 458 | * @iov: io vector |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 459 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | * Caller _must_ check that skb will fit to this iovec. |
| 461 | * |
| 462 | * Returns: 0 - success. |
| 463 | * -EINVAL - checksum failure. |
| 464 | * -EFAULT - fault during copy. Beware, in this case iovec |
| 465 | * can be modified! |
| 466 | */ |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 467 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | int hlen, struct iovec *iov) |
| 469 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 470 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | int chunk = skb->len - hlen; |
| 472 | |
Herbert Xu | ef8aef5 | 2007-09-06 14:06:35 +0100 | [diff] [blame] | 473 | if (!chunk) |
| 474 | return 0; |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | /* Skip filled elements. |
| 477 | * Pretty silly, look at memcpy_toiovec, though 8) |
| 478 | */ |
| 479 | while (!iov->iov_len) |
| 480 | iov++; |
| 481 | |
| 482 | if (iov->iov_len < chunk) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 483 | if (__skb_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | goto csum_error; |
| 485 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) |
| 486 | goto fault; |
| 487 | } else { |
| 488 | csum = csum_partial(skb->data, hlen, skb->csum); |
| 489 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, |
| 490 | chunk, &csum)) |
| 491 | goto fault; |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 492 | if (csum_fold(csum)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | goto csum_error; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 494 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 495 | netdev_rx_csum_fault(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | iov->iov_len -= chunk; |
| 497 | iov->iov_base += chunk; |
| 498 | } |
| 499 | return 0; |
| 500 | csum_error: |
| 501 | return -EINVAL; |
| 502 | fault: |
| 503 | return -EFAULT; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * datagram_poll - generic datagram poll |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 508 | * @file: file struct |
| 509 | * @sock: socket |
| 510 | * @wait: poll table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * |
| 512 | * Datagram poll: Again totally generic. This also handles |
| 513 | * sequenced packet sockets providing the socket receive queue |
| 514 | * is only ever holding data ready to receive. |
| 515 | * |
| 516 | * Note: when you _don't_ use this routine for this protocol, |
| 517 | * and you use a different write policy from sock_writeable() |
| 518 | * then please supply your own write_space callback. |
| 519 | */ |
| 520 | unsigned int datagram_poll(struct file *file, struct socket *sock, |
| 521 | poll_table *wait) |
| 522 | { |
| 523 | struct sock *sk = sock->sk; |
| 524 | unsigned int mask; |
| 525 | |
| 526 | poll_wait(file, sk->sk_sleep, wait); |
| 527 | mask = 0; |
| 528 | |
| 529 | /* exceptional events? */ |
| 530 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 531 | mask |= POLLERR; |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 532 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 533 | mask |= POLLRDHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 535 | mask |= POLLHUP; |
| 536 | |
| 537 | /* readable? */ |
| 538 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
| 539 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 540 | mask |= POLLIN | POLLRDNORM; |
| 541 | |
| 542 | /* Connection-based need to check for termination and startup */ |
| 543 | if (connection_based(sk)) { |
| 544 | if (sk->sk_state == TCP_CLOSE) |
| 545 | mask |= POLLHUP; |
| 546 | /* connection hasn't started yet? */ |
| 547 | if (sk->sk_state == TCP_SYN_SENT) |
| 548 | return mask; |
| 549 | } |
| 550 | |
| 551 | /* writable? */ |
| 552 | if (sock_writeable(sk)) |
| 553 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 554 | else |
| 555 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 556 | |
| 557 | return mask; |
| 558 | } |
| 559 | |
| 560 | EXPORT_SYMBOL(datagram_poll); |
| 561 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
| 562 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
| 563 | EXPORT_SYMBOL(skb_free_datagram); |
| 564 | EXPORT_SYMBOL(skb_recv_datagram); |