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