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