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