Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1 | /* |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2 | * IUCV protocol stack for Linux on zSeries |
| 3 | * |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2006, 2009 |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 5 | * |
| 6 | * Author(s): Jennifer Hunt <jenhunt@us.ibm.com> |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 7 | * Hendrik Brueckner <brueckner@linux.vnet.ibm.com> |
| 8 | * PM functions: |
| 9 | * Ursula Braun <ursula.braun@de.ibm.com> |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 12 | #define KMSG_COMPONENT "af_iucv" |
| 13 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 14 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/poll.h> |
| 25 | #include <net/sock.h> |
| 26 | #include <asm/ebcdic.h> |
| 27 | #include <asm/cpcmd.h> |
| 28 | #include <linux/kmod.h> |
| 29 | |
| 30 | #include <net/iucv/iucv.h> |
| 31 | #include <net/iucv/af_iucv.h> |
| 32 | |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 33 | #define VERSION "1.1" |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 34 | |
| 35 | static char iucv_userid[80]; |
| 36 | |
Alexey Dobriyan | 5708e86 | 2009-09-14 12:23:23 +0000 | [diff] [blame] | 37 | static const struct proto_ops iucv_sock_ops; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 38 | |
| 39 | static struct proto iucv_proto = { |
| 40 | .name = "AF_IUCV", |
| 41 | .owner = THIS_MODULE, |
| 42 | .obj_size = sizeof(struct iucv_sock), |
| 43 | }; |
| 44 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 45 | /* special AF_IUCV IPRM messages */ |
| 46 | static const u8 iprm_shutdown[8] = |
| 47 | {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
| 48 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 49 | #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class)) |
| 50 | |
| 51 | /* macros to set/get socket control buffer at correct offset */ |
| 52 | #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */ |
| 53 | #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag)) |
| 54 | #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */ |
| 55 | #define CB_TRGCLS_LEN (TRGCLS_SIZE) |
| 56 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 57 | #define __iucv_sock_wait(sk, condition, timeo, ret) \ |
| 58 | do { \ |
| 59 | DEFINE_WAIT(__wait); \ |
| 60 | long __timeo = timeo; \ |
| 61 | ret = 0; \ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 62 | prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 63 | while (!(condition)) { \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 64 | if (!__timeo) { \ |
| 65 | ret = -EAGAIN; \ |
| 66 | break; \ |
| 67 | } \ |
| 68 | if (signal_pending(current)) { \ |
| 69 | ret = sock_intr_errno(__timeo); \ |
| 70 | break; \ |
| 71 | } \ |
| 72 | release_sock(sk); \ |
| 73 | __timeo = schedule_timeout(__timeo); \ |
| 74 | lock_sock(sk); \ |
| 75 | ret = sock_error(sk); \ |
| 76 | if (ret) \ |
| 77 | break; \ |
| 78 | } \ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 79 | finish_wait(sk_sleep(sk), &__wait); \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 80 | } while (0) |
| 81 | |
| 82 | #define iucv_sock_wait(sk, condition, timeo) \ |
| 83 | ({ \ |
| 84 | int __ret = 0; \ |
| 85 | if (!(condition)) \ |
| 86 | __iucv_sock_wait(sk, condition, timeo, __ret); \ |
| 87 | __ret; \ |
| 88 | }) |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 89 | |
Heiko Carstens | 57f2044 | 2007-10-08 02:02:52 -0700 | [diff] [blame] | 90 | static void iucv_sock_kill(struct sock *sk); |
| 91 | static void iucv_sock_close(struct sock *sk); |
| 92 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 93 | /* Call Back functions */ |
| 94 | static void iucv_callback_rx(struct iucv_path *, struct iucv_message *); |
| 95 | static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *); |
| 96 | static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 97 | static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8], |
| 98 | u8 ipuser[16]); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 99 | static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]); |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 100 | static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 101 | |
| 102 | static struct iucv_sock_list iucv_sk_list = { |
Robert P. J. Day | 3db8ce3 | 2008-04-10 02:11:24 -0700 | [diff] [blame] | 103 | .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock), |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 104 | .autobind_name = ATOMIC_INIT(0) |
| 105 | }; |
| 106 | |
| 107 | static struct iucv_handler af_iucv_handler = { |
| 108 | .path_pending = iucv_callback_connreq, |
| 109 | .path_complete = iucv_callback_connack, |
| 110 | .path_severed = iucv_callback_connrej, |
| 111 | .message_pending = iucv_callback_rx, |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 112 | .message_complete = iucv_callback_txdone, |
| 113 | .path_quiesced = iucv_callback_shutdown, |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static inline void high_nmcpy(unsigned char *dst, char *src) |
| 117 | { |
| 118 | memcpy(dst, src, 8); |
| 119 | } |
| 120 | |
| 121 | static inline void low_nmcpy(unsigned char *dst, char *src) |
| 122 | { |
| 123 | memcpy(&dst[8], src, 8); |
| 124 | } |
| 125 | |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 126 | static int afiucv_pm_prepare(struct device *dev) |
| 127 | { |
| 128 | #ifdef CONFIG_PM_DEBUG |
| 129 | printk(KERN_WARNING "afiucv_pm_prepare\n"); |
| 130 | #endif |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static void afiucv_pm_complete(struct device *dev) |
| 135 | { |
| 136 | #ifdef CONFIG_PM_DEBUG |
| 137 | printk(KERN_WARNING "afiucv_pm_complete\n"); |
| 138 | #endif |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * afiucv_pm_freeze() - Freeze PM callback |
| 144 | * @dev: AFIUCV dummy device |
| 145 | * |
| 146 | * Sever all established IUCV communication pathes |
| 147 | */ |
| 148 | static int afiucv_pm_freeze(struct device *dev) |
| 149 | { |
| 150 | struct iucv_sock *iucv; |
| 151 | struct sock *sk; |
| 152 | struct hlist_node *node; |
| 153 | int err = 0; |
| 154 | |
| 155 | #ifdef CONFIG_PM_DEBUG |
| 156 | printk(KERN_WARNING "afiucv_pm_freeze\n"); |
| 157 | #endif |
| 158 | read_lock(&iucv_sk_list.lock); |
| 159 | sk_for_each(sk, node, &iucv_sk_list.head) { |
| 160 | iucv = iucv_sk(sk); |
| 161 | skb_queue_purge(&iucv->send_skb_q); |
| 162 | skb_queue_purge(&iucv->backlog_skb_q); |
| 163 | switch (sk->sk_state) { |
| 164 | case IUCV_SEVERED: |
| 165 | case IUCV_DISCONN: |
| 166 | case IUCV_CLOSING: |
| 167 | case IUCV_CONNECTED: |
| 168 | if (iucv->path) { |
| 169 | err = iucv_path_sever(iucv->path, NULL); |
| 170 | iucv_path_free(iucv->path); |
| 171 | iucv->path = NULL; |
| 172 | } |
| 173 | break; |
| 174 | case IUCV_OPEN: |
| 175 | case IUCV_BOUND: |
| 176 | case IUCV_LISTEN: |
| 177 | case IUCV_CLOSED: |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | read_unlock(&iucv_sk_list.lock); |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * afiucv_pm_restore_thaw() - Thaw and restore PM callback |
| 188 | * @dev: AFIUCV dummy device |
| 189 | * |
| 190 | * socket clean up after freeze |
| 191 | */ |
| 192 | static int afiucv_pm_restore_thaw(struct device *dev) |
| 193 | { |
| 194 | struct iucv_sock *iucv; |
| 195 | struct sock *sk; |
| 196 | struct hlist_node *node; |
| 197 | |
| 198 | #ifdef CONFIG_PM_DEBUG |
| 199 | printk(KERN_WARNING "afiucv_pm_restore_thaw\n"); |
| 200 | #endif |
| 201 | read_lock(&iucv_sk_list.lock); |
| 202 | sk_for_each(sk, node, &iucv_sk_list.head) { |
| 203 | iucv = iucv_sk(sk); |
| 204 | switch (sk->sk_state) { |
| 205 | case IUCV_CONNECTED: |
| 206 | sk->sk_err = EPIPE; |
| 207 | sk->sk_state = IUCV_DISCONN; |
| 208 | sk->sk_state_change(sk); |
| 209 | break; |
| 210 | case IUCV_DISCONN: |
| 211 | case IUCV_SEVERED: |
| 212 | case IUCV_CLOSING: |
| 213 | case IUCV_LISTEN: |
| 214 | case IUCV_BOUND: |
| 215 | case IUCV_OPEN: |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | read_unlock(&iucv_sk_list.lock); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 224 | static const struct dev_pm_ops afiucv_pm_ops = { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 225 | .prepare = afiucv_pm_prepare, |
| 226 | .complete = afiucv_pm_complete, |
| 227 | .freeze = afiucv_pm_freeze, |
| 228 | .thaw = afiucv_pm_restore_thaw, |
| 229 | .restore = afiucv_pm_restore_thaw, |
| 230 | }; |
| 231 | |
| 232 | static struct device_driver af_iucv_driver = { |
| 233 | .owner = THIS_MODULE, |
| 234 | .name = "afiucv", |
| 235 | .bus = &iucv_bus, |
| 236 | .pm = &afiucv_pm_ops, |
| 237 | }; |
| 238 | |
| 239 | /* dummy device used as trigger for PM functions */ |
| 240 | static struct device *af_iucv_dev; |
| 241 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 242 | /** |
| 243 | * iucv_msg_length() - Returns the length of an iucv message. |
| 244 | * @msg: Pointer to struct iucv_message, MUST NOT be NULL |
| 245 | * |
| 246 | * The function returns the length of the specified iucv message @msg of data |
| 247 | * stored in a buffer and of data stored in the parameter list (PRMDATA). |
| 248 | * |
| 249 | * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket |
| 250 | * data: |
| 251 | * PRMDATA[0..6] socket data (max 7 bytes); |
| 252 | * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7]) |
| 253 | * |
| 254 | * The socket data length is computed by substracting the socket data length |
| 255 | * value from 0xFF. |
| 256 | * If the socket data len is greater 7, then PRMDATA can be used for special |
| 257 | * notifications (see iucv_sock_shutdown); and further, |
| 258 | * if the socket data len is > 7, the function returns 8. |
| 259 | * |
| 260 | * Use this function to allocate socket buffers to store iucv message data. |
| 261 | */ |
| 262 | static inline size_t iucv_msg_length(struct iucv_message *msg) |
| 263 | { |
| 264 | size_t datalen; |
| 265 | |
| 266 | if (msg->flags & IUCV_IPRMDATA) { |
| 267 | datalen = 0xff - msg->rmmsg[7]; |
| 268 | return (datalen < 8) ? datalen : 8; |
| 269 | } |
| 270 | return msg->length; |
| 271 | } |
| 272 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 273 | /** |
| 274 | * iucv_sock_in_state() - check for specific states |
| 275 | * @sk: sock structure |
| 276 | * @state: first iucv sk state |
| 277 | * @state: second iucv sk state |
| 278 | * |
| 279 | * Returns true if the socket in either in the first or second state. |
| 280 | */ |
| 281 | static int iucv_sock_in_state(struct sock *sk, int state, int state2) |
| 282 | { |
| 283 | return (sk->sk_state == state || sk->sk_state == state2); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * iucv_below_msglim() - function to check if messages can be sent |
| 288 | * @sk: sock structure |
| 289 | * |
| 290 | * Returns true if the send queue length is lower than the message limit. |
| 291 | * Always returns true if the socket is not connected (no iucv path for |
| 292 | * checking the message limit). |
| 293 | */ |
| 294 | static inline int iucv_below_msglim(struct sock *sk) |
| 295 | { |
| 296 | struct iucv_sock *iucv = iucv_sk(sk); |
| 297 | |
| 298 | if (sk->sk_state != IUCV_CONNECTED) |
| 299 | return 1; |
| 300 | return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit |
| 305 | */ |
| 306 | static void iucv_sock_wake_msglim(struct sock *sk) |
| 307 | { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame^] | 308 | struct socket_wq *wq; |
| 309 | |
| 310 | rcu_read_lock(); |
| 311 | wq = rcu_dereference(sk->sk_wq); |
| 312 | if (wq_has_sleeper(wq)) |
| 313 | wake_up_interruptible_all(&wq->wait); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 314 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame^] | 315 | rcu_read_unlock(); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 318 | /* Timers */ |
| 319 | static void iucv_sock_timeout(unsigned long arg) |
| 320 | { |
| 321 | struct sock *sk = (struct sock *)arg; |
| 322 | |
| 323 | bh_lock_sock(sk); |
| 324 | sk->sk_err = ETIMEDOUT; |
| 325 | sk->sk_state_change(sk); |
| 326 | bh_unlock_sock(sk); |
| 327 | |
| 328 | iucv_sock_kill(sk); |
| 329 | sock_put(sk); |
| 330 | } |
| 331 | |
| 332 | static void iucv_sock_clear_timer(struct sock *sk) |
| 333 | { |
| 334 | sk_stop_timer(sk, &sk->sk_timer); |
| 335 | } |
| 336 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 337 | static struct sock *__iucv_get_sock_by_name(char *nm) |
| 338 | { |
| 339 | struct sock *sk; |
| 340 | struct hlist_node *node; |
| 341 | |
| 342 | sk_for_each(sk, node, &iucv_sk_list.head) |
| 343 | if (!memcmp(&iucv_sk(sk)->src_name, nm, 8)) |
| 344 | return sk; |
| 345 | |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | static void iucv_sock_destruct(struct sock *sk) |
| 350 | { |
| 351 | skb_queue_purge(&sk->sk_receive_queue); |
| 352 | skb_queue_purge(&sk->sk_write_queue); |
| 353 | } |
| 354 | |
| 355 | /* Cleanup Listen */ |
| 356 | static void iucv_sock_cleanup_listen(struct sock *parent) |
| 357 | { |
| 358 | struct sock *sk; |
| 359 | |
| 360 | /* Close non-accepted connections */ |
| 361 | while ((sk = iucv_accept_dequeue(parent, NULL))) { |
| 362 | iucv_sock_close(sk); |
| 363 | iucv_sock_kill(sk); |
| 364 | } |
| 365 | |
| 366 | parent->sk_state = IUCV_CLOSED; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 369 | /* Kill socket (only if zapped and orphaned) */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 370 | static void iucv_sock_kill(struct sock *sk) |
| 371 | { |
| 372 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 373 | return; |
| 374 | |
| 375 | iucv_sock_unlink(&iucv_sk_list, sk); |
| 376 | sock_set_flag(sk, SOCK_DEAD); |
| 377 | sock_put(sk); |
| 378 | } |
| 379 | |
| 380 | /* Close an IUCV socket */ |
| 381 | static void iucv_sock_close(struct sock *sk) |
| 382 | { |
| 383 | unsigned char user_data[16]; |
| 384 | struct iucv_sock *iucv = iucv_sk(sk); |
| 385 | int err; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 386 | unsigned long timeo; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 387 | |
| 388 | iucv_sock_clear_timer(sk); |
| 389 | lock_sock(sk); |
| 390 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 391 | switch (sk->sk_state) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 392 | case IUCV_LISTEN: |
| 393 | iucv_sock_cleanup_listen(sk); |
| 394 | break; |
| 395 | |
| 396 | case IUCV_CONNECTED: |
| 397 | case IUCV_DISCONN: |
| 398 | err = 0; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 399 | |
| 400 | sk->sk_state = IUCV_CLOSING; |
| 401 | sk->sk_state_change(sk); |
| 402 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 403 | if (!skb_queue_empty(&iucv->send_skb_q)) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 404 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 405 | timeo = sk->sk_lingertime; |
| 406 | else |
| 407 | timeo = IUCV_DISCONN_TIMEOUT; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 408 | err = iucv_sock_wait(sk, |
| 409 | iucv_sock_in_state(sk, IUCV_CLOSED, 0), |
| 410 | timeo); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Ursula Braun | bbe188c | 2009-04-21 06:04:20 +0000 | [diff] [blame] | 413 | case IUCV_CLOSING: /* fall through */ |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 414 | sk->sk_state = IUCV_CLOSED; |
| 415 | sk->sk_state_change(sk); |
| 416 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 417 | if (iucv->path) { |
| 418 | low_nmcpy(user_data, iucv->src_name); |
| 419 | high_nmcpy(user_data, iucv->dst_name); |
| 420 | ASCEBC(user_data, sizeof(user_data)); |
| 421 | err = iucv_path_sever(iucv->path, user_data); |
| 422 | iucv_path_free(iucv->path); |
| 423 | iucv->path = NULL; |
| 424 | } |
| 425 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 426 | sk->sk_err = ECONNRESET; |
| 427 | sk->sk_state_change(sk); |
| 428 | |
| 429 | skb_queue_purge(&iucv->send_skb_q); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 430 | skb_queue_purge(&iucv->backlog_skb_q); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 431 | break; |
| 432 | |
| 433 | default: |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 434 | /* nothing to do here */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 435 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 436 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 437 | |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 438 | /* mark socket for deletion by iucv_sock_kill() */ |
| 439 | sock_set_flag(sk, SOCK_ZAPPED); |
| 440 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 441 | release_sock(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void iucv_sock_init(struct sock *sk, struct sock *parent) |
| 445 | { |
| 446 | if (parent) |
| 447 | sk->sk_type = parent->sk_type; |
| 448 | } |
| 449 | |
| 450 | static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio) |
| 451 | { |
| 452 | struct sock *sk; |
| 453 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 454 | sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 455 | if (!sk) |
| 456 | return NULL; |
| 457 | |
| 458 | sock_init_data(sock, sk); |
| 459 | INIT_LIST_HEAD(&iucv_sk(sk)->accept_q); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 460 | spin_lock_init(&iucv_sk(sk)->accept_q_lock); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 461 | skb_queue_head_init(&iucv_sk(sk)->send_skb_q); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 462 | INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list); |
| 463 | spin_lock_init(&iucv_sk(sk)->message_q.lock); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 464 | skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 465 | iucv_sk(sk)->send_tag = 0; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 466 | iucv_sk(sk)->flags = 0; |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 467 | iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT; |
Ursula Braun | bbe188c | 2009-04-21 06:04:20 +0000 | [diff] [blame] | 468 | iucv_sk(sk)->path = NULL; |
| 469 | memset(&iucv_sk(sk)->src_user_id , 0, 32); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 470 | |
| 471 | sk->sk_destruct = iucv_sock_destruct; |
| 472 | sk->sk_sndtimeo = IUCV_CONN_TIMEOUT; |
| 473 | sk->sk_allocation = GFP_DMA; |
| 474 | |
| 475 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 476 | |
| 477 | sk->sk_protocol = proto; |
| 478 | sk->sk_state = IUCV_OPEN; |
| 479 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 480 | setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 481 | |
| 482 | iucv_sock_link(&iucv_sk_list, sk); |
| 483 | return sk; |
| 484 | } |
| 485 | |
| 486 | /* Create an IUCV socket */ |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 487 | static int iucv_sock_create(struct net *net, struct socket *sock, int protocol, |
| 488 | int kern) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 489 | { |
| 490 | struct sock *sk; |
| 491 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 492 | if (protocol && protocol != PF_IUCV) |
| 493 | return -EPROTONOSUPPORT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 494 | |
| 495 | sock->state = SS_UNCONNECTED; |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 496 | |
| 497 | switch (sock->type) { |
| 498 | case SOCK_STREAM: |
| 499 | sock->ops = &iucv_sock_ops; |
| 500 | break; |
| 501 | case SOCK_SEQPACKET: |
| 502 | /* currently, proto ops can handle both sk types */ |
| 503 | sock->ops = &iucv_sock_ops; |
| 504 | break; |
| 505 | default: |
| 506 | return -ESOCKTNOSUPPORT; |
| 507 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 508 | |
| 509 | sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL); |
| 510 | if (!sk) |
| 511 | return -ENOMEM; |
| 512 | |
| 513 | iucv_sock_init(sk, NULL); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk) |
| 519 | { |
| 520 | write_lock_bh(&l->lock); |
| 521 | sk_add_node(sk, &l->head); |
| 522 | write_unlock_bh(&l->lock); |
| 523 | } |
| 524 | |
| 525 | void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk) |
| 526 | { |
| 527 | write_lock_bh(&l->lock); |
| 528 | sk_del_node_init(sk); |
| 529 | write_unlock_bh(&l->lock); |
| 530 | } |
| 531 | |
| 532 | void iucv_accept_enqueue(struct sock *parent, struct sock *sk) |
| 533 | { |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 534 | unsigned long flags; |
| 535 | struct iucv_sock *par = iucv_sk(parent); |
| 536 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 537 | sock_hold(sk); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 538 | spin_lock_irqsave(&par->accept_q_lock, flags); |
| 539 | list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q); |
| 540 | spin_unlock_irqrestore(&par->accept_q_lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 541 | iucv_sk(sk)->parent = parent; |
Hendrik Brueckner | 49f5eba | 2009-10-14 22:54:55 +0000 | [diff] [blame] | 542 | sk_acceptq_added(parent); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void iucv_accept_unlink(struct sock *sk) |
| 546 | { |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 547 | unsigned long flags; |
| 548 | struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent); |
| 549 | |
| 550 | spin_lock_irqsave(&par->accept_q_lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 551 | list_del_init(&iucv_sk(sk)->accept_q); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 552 | spin_unlock_irqrestore(&par->accept_q_lock, flags); |
Hendrik Brueckner | 49f5eba | 2009-10-14 22:54:55 +0000 | [diff] [blame] | 553 | sk_acceptq_removed(iucv_sk(sk)->parent); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 554 | iucv_sk(sk)->parent = NULL; |
| 555 | sock_put(sk); |
| 556 | } |
| 557 | |
| 558 | struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock) |
| 559 | { |
| 560 | struct iucv_sock *isk, *n; |
| 561 | struct sock *sk; |
| 562 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 563 | list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 564 | sk = (struct sock *) isk; |
| 565 | lock_sock(sk); |
| 566 | |
| 567 | if (sk->sk_state == IUCV_CLOSED) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 568 | iucv_accept_unlink(sk); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 569 | release_sock(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 570 | continue; |
| 571 | } |
| 572 | |
| 573 | if (sk->sk_state == IUCV_CONNECTED || |
| 574 | sk->sk_state == IUCV_SEVERED || |
Hendrik Brueckner | 56a73de | 2009-09-16 04:37:26 +0000 | [diff] [blame] | 575 | sk->sk_state == IUCV_DISCONN || /* due to PM restore */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 576 | !newsock) { |
| 577 | iucv_accept_unlink(sk); |
| 578 | if (newsock) |
| 579 | sock_graft(sk, newsock); |
| 580 | |
| 581 | if (sk->sk_state == IUCV_SEVERED) |
| 582 | sk->sk_state = IUCV_DISCONN; |
| 583 | |
| 584 | release_sock(sk); |
| 585 | return sk; |
| 586 | } |
| 587 | |
| 588 | release_sock(sk); |
| 589 | } |
| 590 | return NULL; |
| 591 | } |
| 592 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 593 | /* Bind an unbound socket */ |
| 594 | static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 595 | int addr_len) |
| 596 | { |
| 597 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 598 | struct sock *sk = sock->sk; |
| 599 | struct iucv_sock *iucv; |
| 600 | int err; |
| 601 | |
| 602 | /* Verify the input sockaddr */ |
| 603 | if (!addr || addr->sa_family != AF_IUCV) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | lock_sock(sk); |
| 607 | if (sk->sk_state != IUCV_OPEN) { |
| 608 | err = -EBADFD; |
| 609 | goto done; |
| 610 | } |
| 611 | |
| 612 | write_lock_bh(&iucv_sk_list.lock); |
| 613 | |
| 614 | iucv = iucv_sk(sk); |
| 615 | if (__iucv_get_sock_by_name(sa->siucv_name)) { |
| 616 | err = -EADDRINUSE; |
| 617 | goto done_unlock; |
| 618 | } |
| 619 | if (iucv->path) { |
| 620 | err = 0; |
| 621 | goto done_unlock; |
| 622 | } |
| 623 | |
| 624 | /* Bind the socket */ |
| 625 | memcpy(iucv->src_name, sa->siucv_name, 8); |
| 626 | |
| 627 | /* Copy the user id */ |
| 628 | memcpy(iucv->src_user_id, iucv_userid, 8); |
| 629 | sk->sk_state = IUCV_BOUND; |
| 630 | err = 0; |
| 631 | |
| 632 | done_unlock: |
| 633 | /* Release the socket list lock */ |
| 634 | write_unlock_bh(&iucv_sk_list.lock); |
| 635 | done: |
| 636 | release_sock(sk); |
| 637 | return err; |
| 638 | } |
| 639 | |
| 640 | /* Automatically bind an unbound socket */ |
| 641 | static int iucv_sock_autobind(struct sock *sk) |
| 642 | { |
| 643 | struct iucv_sock *iucv = iucv_sk(sk); |
| 644 | char query_buffer[80]; |
| 645 | char name[12]; |
| 646 | int err = 0; |
| 647 | |
| 648 | /* Set the userid and name */ |
| 649 | cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err); |
| 650 | if (unlikely(err)) |
| 651 | return -EPROTO; |
| 652 | |
| 653 | memcpy(iucv->src_user_id, query_buffer, 8); |
| 654 | |
| 655 | write_lock_bh(&iucv_sk_list.lock); |
| 656 | |
| 657 | sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name)); |
| 658 | while (__iucv_get_sock_by_name(name)) { |
| 659 | sprintf(name, "%08x", |
| 660 | atomic_inc_return(&iucv_sk_list.autobind_name)); |
| 661 | } |
| 662 | |
| 663 | write_unlock_bh(&iucv_sk_list.lock); |
| 664 | |
| 665 | memcpy(&iucv->src_name, name, 8); |
| 666 | |
| 667 | return err; |
| 668 | } |
| 669 | |
| 670 | /* Connect an unconnected socket */ |
| 671 | static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr, |
| 672 | int alen, int flags) |
| 673 | { |
| 674 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 675 | struct sock *sk = sock->sk; |
| 676 | struct iucv_sock *iucv; |
| 677 | unsigned char user_data[16]; |
| 678 | int err; |
| 679 | |
| 680 | if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv)) |
| 681 | return -EINVAL; |
| 682 | |
| 683 | if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND) |
| 684 | return -EBADFD; |
| 685 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 686 | if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 687 | return -EINVAL; |
| 688 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 689 | if (sk->sk_state == IUCV_OPEN) { |
| 690 | err = iucv_sock_autobind(sk); |
| 691 | if (unlikely(err)) |
| 692 | return err; |
| 693 | } |
| 694 | |
| 695 | lock_sock(sk); |
| 696 | |
| 697 | /* Set the destination information */ |
| 698 | memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8); |
| 699 | memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8); |
| 700 | |
| 701 | high_nmcpy(user_data, sa->siucv_name); |
| 702 | low_nmcpy(user_data, iucv_sk(sk)->src_name); |
| 703 | ASCEBC(user_data, sizeof(user_data)); |
| 704 | |
| 705 | iucv = iucv_sk(sk); |
| 706 | /* Create path. */ |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 707 | iucv->path = iucv_path_alloc(iucv->msglimit, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 708 | IUCV_IPRMDATA, GFP_KERNEL); |
Ursula Braun | d444472 | 2008-02-07 18:07:19 -0800 | [diff] [blame] | 709 | if (!iucv->path) { |
| 710 | err = -ENOMEM; |
| 711 | goto done; |
| 712 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 713 | err = iucv_path_connect(iucv->path, &af_iucv_handler, |
| 714 | sa->siucv_user_id, NULL, user_data, sk); |
| 715 | if (err) { |
| 716 | iucv_path_free(iucv->path); |
| 717 | iucv->path = NULL; |
Hendrik Brueckner | 55cdea9 | 2009-01-05 18:07:07 -0800 | [diff] [blame] | 718 | switch (err) { |
| 719 | case 0x0b: /* Target communicator is not logged on */ |
| 720 | err = -ENETUNREACH; |
| 721 | break; |
| 722 | case 0x0d: /* Max connections for this guest exceeded */ |
| 723 | case 0x0e: /* Max connections for target guest exceeded */ |
| 724 | err = -EAGAIN; |
| 725 | break; |
| 726 | case 0x0f: /* Missing IUCV authorization */ |
| 727 | err = -EACCES; |
| 728 | break; |
| 729 | default: |
| 730 | err = -ECONNREFUSED; |
| 731 | break; |
| 732 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 733 | goto done; |
| 734 | } |
| 735 | |
| 736 | if (sk->sk_state != IUCV_CONNECTED) { |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 737 | err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED, |
| 738 | IUCV_DISCONN), |
| 739 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if (sk->sk_state == IUCV_DISCONN) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 743 | err = -ECONNREFUSED; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 744 | } |
Ursula Braun | 18becbc | 2009-01-05 18:07:46 -0800 | [diff] [blame] | 745 | |
| 746 | if (err) { |
| 747 | iucv_path_sever(iucv->path, NULL); |
| 748 | iucv_path_free(iucv->path); |
| 749 | iucv->path = NULL; |
| 750 | } |
| 751 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 752 | done: |
| 753 | release_sock(sk); |
| 754 | return err; |
| 755 | } |
| 756 | |
| 757 | /* Move a socket into listening state. */ |
| 758 | static int iucv_sock_listen(struct socket *sock, int backlog) |
| 759 | { |
| 760 | struct sock *sk = sock->sk; |
| 761 | int err; |
| 762 | |
| 763 | lock_sock(sk); |
| 764 | |
| 765 | err = -EINVAL; |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 766 | if (sk->sk_state != IUCV_BOUND) |
| 767 | goto done; |
| 768 | |
| 769 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 770 | goto done; |
| 771 | |
| 772 | sk->sk_max_ack_backlog = backlog; |
| 773 | sk->sk_ack_backlog = 0; |
| 774 | sk->sk_state = IUCV_LISTEN; |
| 775 | err = 0; |
| 776 | |
| 777 | done: |
| 778 | release_sock(sk); |
| 779 | return err; |
| 780 | } |
| 781 | |
| 782 | /* Accept a pending connection */ |
| 783 | static int iucv_sock_accept(struct socket *sock, struct socket *newsock, |
| 784 | int flags) |
| 785 | { |
| 786 | DECLARE_WAITQUEUE(wait, current); |
| 787 | struct sock *sk = sock->sk, *nsk; |
| 788 | long timeo; |
| 789 | int err = 0; |
| 790 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 791 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 792 | |
| 793 | if (sk->sk_state != IUCV_LISTEN) { |
| 794 | err = -EBADFD; |
| 795 | goto done; |
| 796 | } |
| 797 | |
| 798 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 799 | |
| 800 | /* Wait for an incoming connection */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 801 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 802 | while (!(nsk = iucv_accept_dequeue(sk, newsock))) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 803 | set_current_state(TASK_INTERRUPTIBLE); |
| 804 | if (!timeo) { |
| 805 | err = -EAGAIN; |
| 806 | break; |
| 807 | } |
| 808 | |
| 809 | release_sock(sk); |
| 810 | timeo = schedule_timeout(timeo); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 811 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 812 | |
| 813 | if (sk->sk_state != IUCV_LISTEN) { |
| 814 | err = -EBADFD; |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | if (signal_pending(current)) { |
| 819 | err = sock_intr_errno(timeo); |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 825 | remove_wait_queue(sk_sleep(sk), &wait); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 826 | |
| 827 | if (err) |
| 828 | goto done; |
| 829 | |
| 830 | newsock->state = SS_CONNECTED; |
| 831 | |
| 832 | done: |
| 833 | release_sock(sk); |
| 834 | return err; |
| 835 | } |
| 836 | |
| 837 | static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 838 | int *len, int peer) |
| 839 | { |
| 840 | struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr; |
| 841 | struct sock *sk = sock->sk; |
| 842 | |
| 843 | addr->sa_family = AF_IUCV; |
| 844 | *len = sizeof(struct sockaddr_iucv); |
| 845 | |
| 846 | if (peer) { |
| 847 | memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8); |
| 848 | memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8); |
| 849 | } else { |
| 850 | memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8); |
| 851 | memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8); |
| 852 | } |
| 853 | memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port)); |
| 854 | memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr)); |
| 855 | memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid)); |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 860 | /** |
| 861 | * iucv_send_iprm() - Send socket data in parameter list of an iucv message. |
| 862 | * @path: IUCV path |
| 863 | * @msg: Pointer to a struct iucv_message |
| 864 | * @skb: The socket data to send, skb->len MUST BE <= 7 |
| 865 | * |
| 866 | * Send the socket data in the parameter list in the iucv message |
| 867 | * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter |
| 868 | * list and the socket data len at index 7 (last byte). |
| 869 | * See also iucv_msg_length(). |
| 870 | * |
| 871 | * Returns the error code from the iucv_message_send() call. |
| 872 | */ |
| 873 | static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg, |
| 874 | struct sk_buff *skb) |
| 875 | { |
| 876 | u8 prmdata[8]; |
| 877 | |
| 878 | memcpy(prmdata, (void *) skb->data, skb->len); |
| 879 | prmdata[7] = 0xff - (u8) skb->len; |
| 880 | return iucv_message_send(path, msg, IUCV_IPRMDATA, 0, |
| 881 | (void *) prmdata, 8); |
| 882 | } |
| 883 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 884 | static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 885 | struct msghdr *msg, size_t len) |
| 886 | { |
| 887 | struct sock *sk = sock->sk; |
| 888 | struct iucv_sock *iucv = iucv_sk(sk); |
| 889 | struct sk_buff *skb; |
| 890 | struct iucv_message txmsg; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 891 | struct cmsghdr *cmsg; |
| 892 | int cmsg_done; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 893 | long timeo; |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 894 | char user_id[9]; |
| 895 | char appl_id[9]; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 896 | int err; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 897 | int noblock = msg->msg_flags & MSG_DONTWAIT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 898 | |
| 899 | err = sock_error(sk); |
| 900 | if (err) |
| 901 | return err; |
| 902 | |
| 903 | if (msg->msg_flags & MSG_OOB) |
| 904 | return -EOPNOTSUPP; |
| 905 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 906 | /* SOCK_SEQPACKET: we do not support segmented records */ |
| 907 | if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR)) |
| 908 | return -EOPNOTSUPP; |
| 909 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 910 | lock_sock(sk); |
| 911 | |
| 912 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 913 | err = -EPIPE; |
| 914 | goto out; |
| 915 | } |
| 916 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 917 | /* Return if the socket is not in connected state */ |
| 918 | if (sk->sk_state != IUCV_CONNECTED) { |
| 919 | err = -ENOTCONN; |
| 920 | goto out; |
| 921 | } |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 922 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 923 | /* initialize defaults */ |
| 924 | cmsg_done = 0; /* check for duplicate headers */ |
| 925 | txmsg.class = 0; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 926 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 927 | /* iterate over control messages */ |
| 928 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; |
| 929 | cmsg = CMSG_NXTHDR(msg, cmsg)) { |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 930 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 931 | if (!CMSG_OK(msg, cmsg)) { |
| 932 | err = -EINVAL; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 933 | goto out; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 936 | if (cmsg->cmsg_level != SOL_IUCV) |
| 937 | continue; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 938 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 939 | if (cmsg->cmsg_type & cmsg_done) { |
| 940 | err = -EINVAL; |
| 941 | goto out; |
| 942 | } |
| 943 | cmsg_done |= cmsg->cmsg_type; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 944 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 945 | switch (cmsg->cmsg_type) { |
| 946 | case SCM_IUCV_TRGCLS: |
| 947 | if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) { |
| 948 | err = -EINVAL; |
| 949 | goto out; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 952 | /* set iucv message target class */ |
| 953 | memcpy(&txmsg.class, |
| 954 | (void *) CMSG_DATA(cmsg), TRGCLS_SIZE); |
| 955 | |
| 956 | break; |
| 957 | |
| 958 | default: |
| 959 | err = -EINVAL; |
| 960 | goto out; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /* allocate one skb for each iucv message: |
| 966 | * this is fine for SOCK_SEQPACKET (unless we want to support |
| 967 | * segmented records using the MSG_EOR flag), but |
| 968 | * for SOCK_STREAM we might want to improve it in future */ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 969 | skb = sock_alloc_send_skb(sk, len, noblock, &err); |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 970 | if (!skb) |
| 971 | goto out; |
| 972 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
| 973 | err = -EFAULT; |
| 974 | goto fail; |
| 975 | } |
| 976 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 977 | /* wait if outstanding messages for iucv path has reached */ |
| 978 | timeo = sock_sndtimeo(sk, noblock); |
| 979 | err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo); |
| 980 | if (err) |
| 981 | goto fail; |
| 982 | |
| 983 | /* return -ECONNRESET if the socket is no longer connected */ |
| 984 | if (sk->sk_state != IUCV_CONNECTED) { |
| 985 | err = -ECONNRESET; |
| 986 | goto fail; |
| 987 | } |
| 988 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 989 | /* increment and save iucv message tag for msg_completion cbk */ |
| 990 | txmsg.tag = iucv->send_tag++; |
| 991 | memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN); |
| 992 | skb_queue_tail(&iucv->send_skb_q, skb); |
| 993 | |
| 994 | if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) |
| 995 | && skb->len <= 7) { |
| 996 | err = iucv_send_iprm(iucv->path, &txmsg, skb); |
| 997 | |
| 998 | /* on success: there is no message_complete callback |
| 999 | * for an IPRMDATA msg; remove skb from send queue */ |
| 1000 | if (err == 0) { |
| 1001 | skb_unlink(skb, &iucv->send_skb_q); |
| 1002 | kfree_skb(skb); |
| 1003 | } |
| 1004 | |
| 1005 | /* this error should never happen since the |
| 1006 | * IUCV_IPRMDATA path flag is set... sever path */ |
| 1007 | if (err == 0x15) { |
| 1008 | iucv_path_sever(iucv->path, NULL); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1009 | skb_unlink(skb, &iucv->send_skb_q); |
| 1010 | err = -EPIPE; |
| 1011 | goto fail; |
| 1012 | } |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1013 | } else |
| 1014 | err = iucv_message_send(iucv->path, &txmsg, 0, 0, |
| 1015 | (void *) skb->data, skb->len); |
| 1016 | if (err) { |
| 1017 | if (err == 3) { |
| 1018 | user_id[8] = 0; |
| 1019 | memcpy(user_id, iucv->dst_user_id, 8); |
| 1020 | appl_id[8] = 0; |
| 1021 | memcpy(appl_id, iucv->dst_name, 8); |
| 1022 | pr_err("Application %s on z/VM guest %s" |
| 1023 | " exceeds message limit\n", |
| 1024 | appl_id, user_id); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1025 | err = -EAGAIN; |
| 1026 | } else |
| 1027 | err = -EPIPE; |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1028 | skb_unlink(skb, &iucv->send_skb_q); |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1029 | goto fail; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | release_sock(sk); |
| 1033 | return len; |
| 1034 | |
| 1035 | fail: |
| 1036 | kfree_skb(skb); |
| 1037 | out: |
| 1038 | release_sock(sk); |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1042 | /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's |
| 1043 | * |
| 1044 | * Locking: must be called with message_q.lock held |
| 1045 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1046 | static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len) |
| 1047 | { |
| 1048 | int dataleft, size, copied = 0; |
| 1049 | struct sk_buff *nskb; |
| 1050 | |
| 1051 | dataleft = len; |
| 1052 | while (dataleft) { |
| 1053 | if (dataleft >= sk->sk_rcvbuf / 4) |
| 1054 | size = sk->sk_rcvbuf / 4; |
| 1055 | else |
| 1056 | size = dataleft; |
| 1057 | |
| 1058 | nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA); |
| 1059 | if (!nskb) |
| 1060 | return -ENOMEM; |
| 1061 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1062 | /* copy target class to control buffer of new skb */ |
| 1063 | memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN); |
| 1064 | |
| 1065 | /* copy data fragment */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1066 | memcpy(nskb->data, skb->data + copied, size); |
| 1067 | copied += size; |
| 1068 | dataleft -= size; |
| 1069 | |
| 1070 | skb_reset_transport_header(nskb); |
| 1071 | skb_reset_network_header(nskb); |
| 1072 | nskb->len = size; |
| 1073 | |
| 1074 | skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb); |
| 1075 | } |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1080 | /* iucv_process_message() - Receive a single outstanding IUCV message |
| 1081 | * |
| 1082 | * Locking: must be called with message_q.lock held |
| 1083 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1084 | static void iucv_process_message(struct sock *sk, struct sk_buff *skb, |
| 1085 | struct iucv_path *path, |
| 1086 | struct iucv_message *msg) |
| 1087 | { |
| 1088 | int rc; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1089 | unsigned int len; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1090 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1091 | len = iucv_msg_length(msg); |
| 1092 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1093 | /* store msg target class in the second 4 bytes of skb ctrl buffer */ |
| 1094 | /* Note: the first 4 bytes are reserved for msg tag */ |
| 1095 | memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN); |
| 1096 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1097 | /* check for special IPRM messages (e.g. iucv_sock_shutdown) */ |
| 1098 | if ((msg->flags & IUCV_IPRMDATA) && len > 7) { |
| 1099 | if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) { |
| 1100 | skb->data = NULL; |
| 1101 | skb->len = 0; |
| 1102 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1103 | } else { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1104 | rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA, |
| 1105 | skb->data, len, NULL); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1106 | if (rc) { |
| 1107 | kfree_skb(skb); |
| 1108 | return; |
| 1109 | } |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1110 | /* we need to fragment iucv messages for SOCK_STREAM only; |
| 1111 | * for SOCK_SEQPACKET, it is only relevant if we support |
| 1112 | * record segmentation using MSG_EOR (see also recvmsg()) */ |
| 1113 | if (sk->sk_type == SOCK_STREAM && |
| 1114 | skb->truesize >= sk->sk_rcvbuf / 4) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1115 | rc = iucv_fragment_skb(sk, skb, len); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1116 | kfree_skb(skb); |
| 1117 | skb = NULL; |
| 1118 | if (rc) { |
| 1119 | iucv_path_sever(path, NULL); |
| 1120 | return; |
| 1121 | } |
| 1122 | skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q); |
| 1123 | } else { |
| 1124 | skb_reset_transport_header(skb); |
| 1125 | skb_reset_network_header(skb); |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1126 | skb->len = len; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | if (sock_queue_rcv_skb(sk, skb)) |
| 1131 | skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb); |
| 1132 | } |
| 1133 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1134 | /* iucv_process_message_q() - Process outstanding IUCV messages |
| 1135 | * |
| 1136 | * Locking: must be called with message_q.lock held |
| 1137 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1138 | static void iucv_process_message_q(struct sock *sk) |
| 1139 | { |
| 1140 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1141 | struct sk_buff *skb; |
| 1142 | struct sock_msg_q *p, *n; |
| 1143 | |
| 1144 | list_for_each_entry_safe(p, n, &iucv->message_q.list, list) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1145 | skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1146 | if (!skb) |
| 1147 | break; |
| 1148 | iucv_process_message(sk, skb, p->path, &p->msg); |
| 1149 | list_del(&p->list); |
| 1150 | kfree(p); |
| 1151 | if (!skb_queue_empty(&iucv->backlog_skb_q)) |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1156 | static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1157 | struct msghdr *msg, size_t len, int flags) |
| 1158 | { |
| 1159 | int noblock = flags & MSG_DONTWAIT; |
| 1160 | struct sock *sk = sock->sk; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1161 | struct iucv_sock *iucv = iucv_sk(sk); |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1162 | unsigned int copied, rlen; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1163 | struct sk_buff *skb, *rskb, *cskb; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1164 | int err = 0; |
| 1165 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1166 | if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) && |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1167 | skb_queue_empty(&iucv->backlog_skb_q) && |
| 1168 | skb_queue_empty(&sk->sk_receive_queue) && |
| 1169 | list_empty(&iucv->message_q.list)) |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1170 | return 0; |
| 1171 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1172 | if (flags & (MSG_OOB)) |
| 1173 | return -EOPNOTSUPP; |
| 1174 | |
Hendrik Brueckner | 60d3705 | 2009-04-21 06:04:21 +0000 | [diff] [blame] | 1175 | /* receive/dequeue next skb: |
| 1176 | * the function understands MSG_PEEK and, thus, does not dequeue skb */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1177 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 1178 | if (!skb) { |
| 1179 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1180 | return 0; |
| 1181 | return err; |
| 1182 | } |
| 1183 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1184 | rlen = skb->len; /* real length of skb */ |
| 1185 | copied = min_t(unsigned int, rlen, len); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1186 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1187 | cskb = skb; |
| 1188 | if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) { |
Hendrik Brueckner | 802788b | 2009-04-21 23:26:26 +0000 | [diff] [blame] | 1189 | if (!(flags & MSG_PEEK)) |
| 1190 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1191 | return -EFAULT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1194 | /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */ |
| 1195 | if (sk->sk_type == SOCK_SEQPACKET) { |
| 1196 | if (copied < rlen) |
| 1197 | msg->msg_flags |= MSG_TRUNC; |
| 1198 | /* each iucv message contains a complete record */ |
| 1199 | msg->msg_flags |= MSG_EOR; |
| 1200 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1201 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1202 | /* create control message to store iucv msg target class: |
| 1203 | * get the trgcls from the control buffer of the skb due to |
| 1204 | * fragmentation of original iucv message. */ |
| 1205 | err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS, |
| 1206 | CB_TRGCLS_LEN, CB_TRGCLS(skb)); |
| 1207 | if (err) { |
| 1208 | if (!(flags & MSG_PEEK)) |
| 1209 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1210 | return err; |
| 1211 | } |
| 1212 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1213 | /* Mark read part of skb as used */ |
| 1214 | if (!(flags & MSG_PEEK)) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1215 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1216 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
| 1217 | if (sk->sk_type == SOCK_STREAM) { |
| 1218 | skb_pull(skb, copied); |
| 1219 | if (skb->len) { |
| 1220 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1221 | goto done; |
| 1222 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | kfree_skb(skb); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1226 | |
| 1227 | /* Queue backlog skbs */ |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1228 | spin_lock_bh(&iucv->message_q.lock); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1229 | rskb = skb_dequeue(&iucv->backlog_skb_q); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1230 | while (rskb) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1231 | if (sock_queue_rcv_skb(sk, rskb)) { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1232 | skb_queue_head(&iucv->backlog_skb_q, |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1233 | rskb); |
| 1234 | break; |
| 1235 | } else { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1236 | rskb = skb_dequeue(&iucv->backlog_skb_q); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1239 | if (skb_queue_empty(&iucv->backlog_skb_q)) { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1240 | if (!list_empty(&iucv->message_q.list)) |
| 1241 | iucv_process_message_q(sk); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1242 | } |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1243 | spin_unlock_bh(&iucv->message_q.lock); |
Hendrik Brueckner | 60d3705 | 2009-04-21 06:04:21 +0000 | [diff] [blame] | 1244 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1245 | |
| 1246 | done: |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1247 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 1248 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 1249 | copied = rlen; |
| 1250 | |
| 1251 | return copied; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static inline unsigned int iucv_accept_poll(struct sock *parent) |
| 1255 | { |
| 1256 | struct iucv_sock *isk, *n; |
| 1257 | struct sock *sk; |
| 1258 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1259 | list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1260 | sk = (struct sock *) isk; |
| 1261 | |
| 1262 | if (sk->sk_state == IUCV_CONNECTED) |
| 1263 | return POLLIN | POLLRDNORM; |
| 1264 | } |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | unsigned int iucv_sock_poll(struct file *file, struct socket *sock, |
| 1270 | poll_table *wait) |
| 1271 | { |
| 1272 | struct sock *sk = sock->sk; |
| 1273 | unsigned int mask = 0; |
| 1274 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1275 | sock_poll_wait(file, sk_sleep(sk), wait); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1276 | |
| 1277 | if (sk->sk_state == IUCV_LISTEN) |
| 1278 | return iucv_accept_poll(sk); |
| 1279 | |
| 1280 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
| 1281 | mask |= POLLERR; |
| 1282 | |
| 1283 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1284 | mask |= POLLRDHUP; |
| 1285 | |
| 1286 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 1287 | mask |= POLLHUP; |
| 1288 | |
| 1289 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1290 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1291 | mask |= POLLIN | POLLRDNORM; |
| 1292 | |
| 1293 | if (sk->sk_state == IUCV_CLOSED) |
| 1294 | mask |= POLLHUP; |
| 1295 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1296 | if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) |
| 1297 | mask |= POLLIN; |
| 1298 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1299 | if (sock_writeable(sk)) |
| 1300 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 1301 | else |
| 1302 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
| 1303 | |
| 1304 | return mask; |
| 1305 | } |
| 1306 | |
| 1307 | static int iucv_sock_shutdown(struct socket *sock, int how) |
| 1308 | { |
| 1309 | struct sock *sk = sock->sk; |
| 1310 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1311 | struct iucv_message txmsg; |
| 1312 | int err = 0; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1313 | |
| 1314 | how++; |
| 1315 | |
| 1316 | if ((how & ~SHUTDOWN_MASK) || !how) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | lock_sock(sk); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1320 | switch (sk->sk_state) { |
Hendrik Brueckner | e14ad5f | 2009-04-21 06:04:23 +0000 | [diff] [blame] | 1321 | case IUCV_DISCONN: |
| 1322 | case IUCV_CLOSING: |
| 1323 | case IUCV_SEVERED: |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1324 | case IUCV_CLOSED: |
| 1325 | err = -ENOTCONN; |
| 1326 | goto fail; |
| 1327 | |
| 1328 | default: |
| 1329 | sk->sk_shutdown |= how; |
| 1330 | break; |
| 1331 | } |
| 1332 | |
| 1333 | if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) { |
| 1334 | txmsg.class = 0; |
| 1335 | txmsg.tag = 0; |
| 1336 | err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1337 | (void *) iprm_shutdown, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1338 | if (err) { |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1339 | switch (err) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1340 | case 1: |
| 1341 | err = -ENOTCONN; |
| 1342 | break; |
| 1343 | case 2: |
| 1344 | err = -ECONNRESET; |
| 1345 | break; |
| 1346 | default: |
| 1347 | err = -ENOTCONN; |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) { |
| 1354 | err = iucv_path_quiesce(iucv_sk(sk)->path, NULL); |
| 1355 | if (err) |
| 1356 | err = -ENOTCONN; |
| 1357 | |
| 1358 | skb_queue_purge(&sk->sk_receive_queue); |
| 1359 | } |
| 1360 | |
| 1361 | /* Wake up anyone sleeping in poll */ |
| 1362 | sk->sk_state_change(sk); |
| 1363 | |
| 1364 | fail: |
| 1365 | release_sock(sk); |
| 1366 | return err; |
| 1367 | } |
| 1368 | |
| 1369 | static int iucv_sock_release(struct socket *sock) |
| 1370 | { |
| 1371 | struct sock *sk = sock->sk; |
| 1372 | int err = 0; |
| 1373 | |
| 1374 | if (!sk) |
| 1375 | return 0; |
| 1376 | |
| 1377 | iucv_sock_close(sk); |
| 1378 | |
| 1379 | /* Unregister with IUCV base support */ |
| 1380 | if (iucv_sk(sk)->path) { |
| 1381 | iucv_path_sever(iucv_sk(sk)->path, NULL); |
| 1382 | iucv_path_free(iucv_sk(sk)->path); |
| 1383 | iucv_sk(sk)->path = NULL; |
| 1384 | } |
| 1385 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1386 | sock_orphan(sk); |
| 1387 | iucv_sock_kill(sk); |
| 1388 | return err; |
| 1389 | } |
| 1390 | |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1391 | /* getsockopt and setsockopt */ |
| 1392 | static int iucv_sock_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1393 | char __user *optval, unsigned int optlen) |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1394 | { |
| 1395 | struct sock *sk = sock->sk; |
| 1396 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1397 | int val; |
| 1398 | int rc; |
| 1399 | |
| 1400 | if (level != SOL_IUCV) |
| 1401 | return -ENOPROTOOPT; |
| 1402 | |
| 1403 | if (optlen < sizeof(int)) |
| 1404 | return -EINVAL; |
| 1405 | |
| 1406 | if (get_user(val, (int __user *) optval)) |
| 1407 | return -EFAULT; |
| 1408 | |
| 1409 | rc = 0; |
| 1410 | |
| 1411 | lock_sock(sk); |
| 1412 | switch (optname) { |
| 1413 | case SO_IPRMDATA_MSG: |
| 1414 | if (val) |
| 1415 | iucv->flags |= IUCV_IPRMDATA; |
| 1416 | else |
| 1417 | iucv->flags &= ~IUCV_IPRMDATA; |
| 1418 | break; |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1419 | case SO_MSGLIMIT: |
| 1420 | switch (sk->sk_state) { |
| 1421 | case IUCV_OPEN: |
| 1422 | case IUCV_BOUND: |
| 1423 | if (val < 1 || val > (u16)(~0)) |
| 1424 | rc = -EINVAL; |
| 1425 | else |
| 1426 | iucv->msglimit = val; |
| 1427 | break; |
| 1428 | default: |
| 1429 | rc = -EINVAL; |
| 1430 | break; |
| 1431 | } |
| 1432 | break; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1433 | default: |
| 1434 | rc = -ENOPROTOOPT; |
| 1435 | break; |
| 1436 | } |
| 1437 | release_sock(sk); |
| 1438 | |
| 1439 | return rc; |
| 1440 | } |
| 1441 | |
| 1442 | static int iucv_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1443 | char __user *optval, int __user *optlen) |
| 1444 | { |
| 1445 | struct sock *sk = sock->sk; |
| 1446 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1447 | int val, len; |
| 1448 | |
| 1449 | if (level != SOL_IUCV) |
| 1450 | return -ENOPROTOOPT; |
| 1451 | |
| 1452 | if (get_user(len, optlen)) |
| 1453 | return -EFAULT; |
| 1454 | |
| 1455 | if (len < 0) |
| 1456 | return -EINVAL; |
| 1457 | |
| 1458 | len = min_t(unsigned int, len, sizeof(int)); |
| 1459 | |
| 1460 | switch (optname) { |
| 1461 | case SO_IPRMDATA_MSG: |
| 1462 | val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0; |
| 1463 | break; |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1464 | case SO_MSGLIMIT: |
| 1465 | lock_sock(sk); |
| 1466 | val = (iucv->path != NULL) ? iucv->path->msglim /* connected */ |
| 1467 | : iucv->msglimit; /* default */ |
| 1468 | release_sock(sk); |
| 1469 | break; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1470 | default: |
| 1471 | return -ENOPROTOOPT; |
| 1472 | } |
| 1473 | |
| 1474 | if (put_user(len, optlen)) |
| 1475 | return -EFAULT; |
| 1476 | if (copy_to_user(optval, &val, len)) |
| 1477 | return -EFAULT; |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1483 | /* Callback wrappers - called from iucv base support */ |
| 1484 | static int iucv_callback_connreq(struct iucv_path *path, |
| 1485 | u8 ipvmid[8], u8 ipuser[16]) |
| 1486 | { |
| 1487 | unsigned char user_data[16]; |
| 1488 | unsigned char nuser_data[16]; |
| 1489 | unsigned char src_name[8]; |
| 1490 | struct hlist_node *node; |
| 1491 | struct sock *sk, *nsk; |
| 1492 | struct iucv_sock *iucv, *niucv; |
| 1493 | int err; |
| 1494 | |
| 1495 | memcpy(src_name, ipuser, 8); |
| 1496 | EBCASC(src_name, 8); |
| 1497 | /* Find out if this path belongs to af_iucv. */ |
| 1498 | read_lock(&iucv_sk_list.lock); |
| 1499 | iucv = NULL; |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 1500 | sk = NULL; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1501 | sk_for_each(sk, node, &iucv_sk_list.head) |
| 1502 | if (sk->sk_state == IUCV_LISTEN && |
| 1503 | !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) { |
| 1504 | /* |
| 1505 | * Found a listening socket with |
| 1506 | * src_name == ipuser[0-7]. |
| 1507 | */ |
| 1508 | iucv = iucv_sk(sk); |
| 1509 | break; |
| 1510 | } |
| 1511 | read_unlock(&iucv_sk_list.lock); |
| 1512 | if (!iucv) |
| 1513 | /* No socket found, not one of our paths. */ |
| 1514 | return -EINVAL; |
| 1515 | |
| 1516 | bh_lock_sock(sk); |
| 1517 | |
| 1518 | /* Check if parent socket is listening */ |
| 1519 | low_nmcpy(user_data, iucv->src_name); |
| 1520 | high_nmcpy(user_data, iucv->dst_name); |
| 1521 | ASCEBC(user_data, sizeof(user_data)); |
| 1522 | if (sk->sk_state != IUCV_LISTEN) { |
| 1523 | err = iucv_path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1524 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1525 | goto fail; |
| 1526 | } |
| 1527 | |
| 1528 | /* Check for backlog size */ |
| 1529 | if (sk_acceptq_is_full(sk)) { |
| 1530 | err = iucv_path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1531 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1532 | goto fail; |
| 1533 | } |
| 1534 | |
| 1535 | /* Create the new socket */ |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1536 | nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1537 | if (!nsk) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1538 | err = iucv_path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1539 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1540 | goto fail; |
| 1541 | } |
| 1542 | |
| 1543 | niucv = iucv_sk(nsk); |
| 1544 | iucv_sock_init(nsk, sk); |
| 1545 | |
| 1546 | /* Set the new iucv_sock */ |
| 1547 | memcpy(niucv->dst_name, ipuser + 8, 8); |
| 1548 | EBCASC(niucv->dst_name, 8); |
| 1549 | memcpy(niucv->dst_user_id, ipvmid, 8); |
| 1550 | memcpy(niucv->src_name, iucv->src_name, 8); |
| 1551 | memcpy(niucv->src_user_id, iucv->src_user_id, 8); |
| 1552 | niucv->path = path; |
| 1553 | |
| 1554 | /* Call iucv_accept */ |
| 1555 | high_nmcpy(nuser_data, ipuser + 8); |
| 1556 | memcpy(nuser_data + 8, niucv->src_name, 8); |
| 1557 | ASCEBC(nuser_data + 8, 8); |
| 1558 | |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1559 | /* set message limit for path based on msglimit of accepting socket */ |
| 1560 | niucv->msglimit = iucv->msglimit; |
| 1561 | path->msglim = iucv->msglimit; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1562 | err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1563 | if (err) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1564 | err = iucv_path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1565 | iucv_path_free(path); |
| 1566 | iucv_sock_kill(nsk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1567 | goto fail; |
| 1568 | } |
| 1569 | |
| 1570 | iucv_accept_enqueue(sk, nsk); |
| 1571 | |
| 1572 | /* Wake up accept */ |
| 1573 | nsk->sk_state = IUCV_CONNECTED; |
| 1574 | sk->sk_data_ready(sk, 1); |
| 1575 | err = 0; |
| 1576 | fail: |
| 1577 | bh_unlock_sock(sk); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16]) |
| 1582 | { |
| 1583 | struct sock *sk = path->private; |
| 1584 | |
| 1585 | sk->sk_state = IUCV_CONNECTED; |
| 1586 | sk->sk_state_change(sk); |
| 1587 | } |
| 1588 | |
| 1589 | static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg) |
| 1590 | { |
| 1591 | struct sock *sk = path->private; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1592 | struct iucv_sock *iucv = iucv_sk(sk); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1593 | struct sk_buff *skb; |
| 1594 | struct sock_msg_q *save_msg; |
| 1595 | int len; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1596 | |
Hendrik Brueckner | fe86e54 | 2009-04-21 06:04:22 +0000 | [diff] [blame] | 1597 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 1598 | iucv_message_reject(path, msg); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1599 | return; |
Hendrik Brueckner | fe86e54 | 2009-04-21 06:04:22 +0000 | [diff] [blame] | 1600 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1601 | |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1602 | spin_lock(&iucv->message_q.lock); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1603 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1604 | if (!list_empty(&iucv->message_q.list) || |
| 1605 | !skb_queue_empty(&iucv->backlog_skb_q)) |
| 1606 | goto save_message; |
| 1607 | |
| 1608 | len = atomic_read(&sk->sk_rmem_alloc); |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1609 | len += iucv_msg_length(msg) + sizeof(struct sk_buff); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1610 | if (len > sk->sk_rcvbuf) |
| 1611 | goto save_message; |
| 1612 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1613 | skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1614 | if (!skb) |
| 1615 | goto save_message; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1616 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1617 | iucv_process_message(sk, skb, path, msg); |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1618 | goto out_unlock; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1619 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1620 | save_message: |
| 1621 | save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | d444472 | 2008-02-07 18:07:19 -0800 | [diff] [blame] | 1622 | if (!save_msg) |
| 1623 | return; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1624 | save_msg->path = path; |
| 1625 | save_msg->msg = *msg; |
| 1626 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1627 | list_add_tail(&save_msg->list, &iucv->message_q.list); |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1628 | |
| 1629 | out_unlock: |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1630 | spin_unlock(&iucv->message_q.lock); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | static void iucv_callback_txdone(struct iucv_path *path, |
| 1634 | struct iucv_message *msg) |
| 1635 | { |
| 1636 | struct sock *sk = path->private; |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1637 | struct sk_buff *this = NULL; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1638 | struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q; |
| 1639 | struct sk_buff *list_skb = list->next; |
| 1640 | unsigned long flags; |
| 1641 | |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1642 | if (!skb_queue_empty(list)) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1643 | spin_lock_irqsave(&list->lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1644 | |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1645 | while (list_skb != (struct sk_buff *)list) { |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1646 | if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) { |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1647 | this = list_skb; |
| 1648 | break; |
| 1649 | } |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1650 | list_skb = list_skb->next; |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1651 | } |
| 1652 | if (this) |
| 1653 | __skb_unlink(this, list); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1654 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1655 | spin_unlock_irqrestore(&list->lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1656 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1657 | if (this) { |
| 1658 | kfree_skb(this); |
| 1659 | /* wake up any process waiting for sending */ |
| 1660 | iucv_sock_wake_msglim(sk); |
| 1661 | } |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1662 | } |
Ursula Braun | c2b4afd | 2008-07-14 09:59:29 +0200 | [diff] [blame] | 1663 | BUG_ON(!this); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1664 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1665 | if (sk->sk_state == IUCV_CLOSING) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1666 | if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { |
| 1667 | sk->sk_state = IUCV_CLOSED; |
| 1668 | sk->sk_state_change(sk); |
| 1669 | } |
| 1670 | } |
| 1671 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16]) |
| 1675 | { |
| 1676 | struct sock *sk = path->private; |
| 1677 | |
| 1678 | if (!list_empty(&iucv_sk(sk)->accept_q)) |
| 1679 | sk->sk_state = IUCV_SEVERED; |
| 1680 | else |
| 1681 | sk->sk_state = IUCV_DISCONN; |
| 1682 | |
| 1683 | sk->sk_state_change(sk); |
| 1684 | } |
| 1685 | |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 1686 | /* called if the other communication side shuts down its RECV direction; |
| 1687 | * in turn, the callback sets SEND_SHUTDOWN to disable sending of data. |
| 1688 | */ |
| 1689 | static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16]) |
| 1690 | { |
| 1691 | struct sock *sk = path->private; |
| 1692 | |
| 1693 | bh_lock_sock(sk); |
| 1694 | if (sk->sk_state != IUCV_CLOSED) { |
| 1695 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 1696 | sk->sk_state_change(sk); |
| 1697 | } |
| 1698 | bh_unlock_sock(sk); |
| 1699 | } |
| 1700 | |
Alexey Dobriyan | 5708e86 | 2009-09-14 12:23:23 +0000 | [diff] [blame] | 1701 | static const struct proto_ops iucv_sock_ops = { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1702 | .family = PF_IUCV, |
| 1703 | .owner = THIS_MODULE, |
| 1704 | .release = iucv_sock_release, |
| 1705 | .bind = iucv_sock_bind, |
| 1706 | .connect = iucv_sock_connect, |
| 1707 | .listen = iucv_sock_listen, |
| 1708 | .accept = iucv_sock_accept, |
| 1709 | .getname = iucv_sock_getname, |
| 1710 | .sendmsg = iucv_sock_sendmsg, |
| 1711 | .recvmsg = iucv_sock_recvmsg, |
| 1712 | .poll = iucv_sock_poll, |
| 1713 | .ioctl = sock_no_ioctl, |
| 1714 | .mmap = sock_no_mmap, |
| 1715 | .socketpair = sock_no_socketpair, |
| 1716 | .shutdown = iucv_sock_shutdown, |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1717 | .setsockopt = iucv_sock_setsockopt, |
| 1718 | .getsockopt = iucv_sock_getsockopt, |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1719 | }; |
| 1720 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1721 | static const struct net_proto_family iucv_sock_family_ops = { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1722 | .family = AF_IUCV, |
| 1723 | .owner = THIS_MODULE, |
| 1724 | .create = iucv_sock_create, |
| 1725 | }; |
| 1726 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1727 | static int __init afiucv_init(void) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1728 | { |
| 1729 | int err; |
| 1730 | |
| 1731 | if (!MACHINE_IS_VM) { |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 1732 | pr_err("The af_iucv module cannot be loaded" |
| 1733 | " without z/VM\n"); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1734 | err = -EPROTONOSUPPORT; |
| 1735 | goto out; |
| 1736 | } |
| 1737 | cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err); |
| 1738 | if (unlikely(err)) { |
Ursula Braun | c2b4afd | 2008-07-14 09:59:29 +0200 | [diff] [blame] | 1739 | WARN_ON(err); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1740 | err = -EPROTONOSUPPORT; |
| 1741 | goto out; |
| 1742 | } |
| 1743 | |
| 1744 | err = iucv_register(&af_iucv_handler, 0); |
| 1745 | if (err) |
| 1746 | goto out; |
| 1747 | err = proto_register(&iucv_proto, 0); |
| 1748 | if (err) |
| 1749 | goto out_iucv; |
| 1750 | err = sock_register(&iucv_sock_family_ops); |
| 1751 | if (err) |
| 1752 | goto out_proto; |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1753 | /* establish dummy device */ |
| 1754 | err = driver_register(&af_iucv_driver); |
| 1755 | if (err) |
| 1756 | goto out_sock; |
| 1757 | af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 1758 | if (!af_iucv_dev) { |
| 1759 | err = -ENOMEM; |
| 1760 | goto out_driver; |
| 1761 | } |
| 1762 | dev_set_name(af_iucv_dev, "af_iucv"); |
| 1763 | af_iucv_dev->bus = &iucv_bus; |
| 1764 | af_iucv_dev->parent = iucv_root; |
| 1765 | af_iucv_dev->release = (void (*)(struct device *))kfree; |
| 1766 | af_iucv_dev->driver = &af_iucv_driver; |
| 1767 | err = device_register(af_iucv_dev); |
| 1768 | if (err) |
| 1769 | goto out_driver; |
| 1770 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1771 | return 0; |
| 1772 | |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1773 | out_driver: |
| 1774 | driver_unregister(&af_iucv_driver); |
| 1775 | out_sock: |
| 1776 | sock_unregister(PF_IUCV); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1777 | out_proto: |
| 1778 | proto_unregister(&iucv_proto); |
| 1779 | out_iucv: |
| 1780 | iucv_unregister(&af_iucv_handler, 0); |
| 1781 | out: |
| 1782 | return err; |
| 1783 | } |
| 1784 | |
| 1785 | static void __exit afiucv_exit(void) |
| 1786 | { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1787 | device_unregister(af_iucv_dev); |
| 1788 | driver_unregister(&af_iucv_driver); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1789 | sock_unregister(PF_IUCV); |
| 1790 | proto_unregister(&iucv_proto); |
| 1791 | iucv_unregister(&af_iucv_handler, 0); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | module_init(afiucv_init); |
| 1795 | module_exit(afiucv_exit); |
| 1796 | |
| 1797 | MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>"); |
| 1798 | MODULE_DESCRIPTION("IUCV Sockets ver " VERSION); |
| 1799 | MODULE_VERSION(VERSION); |
| 1800 | MODULE_LICENSE("GPL"); |
| 1801 | MODULE_ALIAS_NETPROTO(PF_IUCV); |