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 | |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 45 | static struct iucv_interface *pr_iucv; |
| 46 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 47 | /* special AF_IUCV IPRM messages */ |
| 48 | static const u8 iprm_shutdown[8] = |
| 49 | {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
| 50 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 51 | #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class)) |
| 52 | |
| 53 | /* macros to set/get socket control buffer at correct offset */ |
| 54 | #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */ |
| 55 | #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag)) |
| 56 | #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */ |
| 57 | #define CB_TRGCLS_LEN (TRGCLS_SIZE) |
| 58 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 59 | #define __iucv_sock_wait(sk, condition, timeo, ret) \ |
| 60 | do { \ |
| 61 | DEFINE_WAIT(__wait); \ |
| 62 | long __timeo = timeo; \ |
| 63 | ret = 0; \ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 64 | prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 65 | while (!(condition)) { \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 66 | if (!__timeo) { \ |
| 67 | ret = -EAGAIN; \ |
| 68 | break; \ |
| 69 | } \ |
| 70 | if (signal_pending(current)) { \ |
| 71 | ret = sock_intr_errno(__timeo); \ |
| 72 | break; \ |
| 73 | } \ |
| 74 | release_sock(sk); \ |
| 75 | __timeo = schedule_timeout(__timeo); \ |
| 76 | lock_sock(sk); \ |
| 77 | ret = sock_error(sk); \ |
| 78 | if (ret) \ |
| 79 | break; \ |
| 80 | } \ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 81 | finish_wait(sk_sleep(sk), &__wait); \ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 82 | } while (0) |
| 83 | |
| 84 | #define iucv_sock_wait(sk, condition, timeo) \ |
| 85 | ({ \ |
| 86 | int __ret = 0; \ |
| 87 | if (!(condition)) \ |
| 88 | __iucv_sock_wait(sk, condition, timeo, __ret); \ |
| 89 | __ret; \ |
| 90 | }) |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 91 | |
Heiko Carstens | 57f2044 | 2007-10-08 02:02:52 -0700 | [diff] [blame] | 92 | static void iucv_sock_kill(struct sock *sk); |
| 93 | static void iucv_sock_close(struct sock *sk); |
| 94 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 95 | /* Call Back functions */ |
| 96 | static void iucv_callback_rx(struct iucv_path *, struct iucv_message *); |
| 97 | static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *); |
| 98 | static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 99 | static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8], |
| 100 | u8 ipuser[16]); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 101 | static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]); |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 102 | static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 103 | |
| 104 | static struct iucv_sock_list iucv_sk_list = { |
Robert P. J. Day | 3db8ce3 | 2008-04-10 02:11:24 -0700 | [diff] [blame] | 105 | .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock), |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 106 | .autobind_name = ATOMIC_INIT(0) |
| 107 | }; |
| 108 | |
| 109 | static struct iucv_handler af_iucv_handler = { |
| 110 | .path_pending = iucv_callback_connreq, |
| 111 | .path_complete = iucv_callback_connack, |
| 112 | .path_severed = iucv_callback_connrej, |
| 113 | .message_pending = iucv_callback_rx, |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 114 | .message_complete = iucv_callback_txdone, |
| 115 | .path_quiesced = iucv_callback_shutdown, |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static inline void high_nmcpy(unsigned char *dst, char *src) |
| 119 | { |
| 120 | memcpy(dst, src, 8); |
| 121 | } |
| 122 | |
| 123 | static inline void low_nmcpy(unsigned char *dst, char *src) |
| 124 | { |
| 125 | memcpy(&dst[8], src, 8); |
| 126 | } |
| 127 | |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 128 | static int afiucv_pm_prepare(struct device *dev) |
| 129 | { |
| 130 | #ifdef CONFIG_PM_DEBUG |
| 131 | printk(KERN_WARNING "afiucv_pm_prepare\n"); |
| 132 | #endif |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static void afiucv_pm_complete(struct device *dev) |
| 137 | { |
| 138 | #ifdef CONFIG_PM_DEBUG |
| 139 | printk(KERN_WARNING "afiucv_pm_complete\n"); |
| 140 | #endif |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * afiucv_pm_freeze() - Freeze PM callback |
| 145 | * @dev: AFIUCV dummy device |
| 146 | * |
| 147 | * Sever all established IUCV communication pathes |
| 148 | */ |
| 149 | static int afiucv_pm_freeze(struct device *dev) |
| 150 | { |
| 151 | struct iucv_sock *iucv; |
| 152 | struct sock *sk; |
| 153 | struct hlist_node *node; |
| 154 | int err = 0; |
| 155 | |
| 156 | #ifdef CONFIG_PM_DEBUG |
| 157 | printk(KERN_WARNING "afiucv_pm_freeze\n"); |
| 158 | #endif |
| 159 | read_lock(&iucv_sk_list.lock); |
| 160 | sk_for_each(sk, node, &iucv_sk_list.head) { |
| 161 | iucv = iucv_sk(sk); |
| 162 | skb_queue_purge(&iucv->send_skb_q); |
| 163 | skb_queue_purge(&iucv->backlog_skb_q); |
| 164 | switch (sk->sk_state) { |
| 165 | case IUCV_SEVERED: |
| 166 | case IUCV_DISCONN: |
| 167 | case IUCV_CLOSING: |
| 168 | case IUCV_CONNECTED: |
| 169 | if (iucv->path) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 170 | err = pr_iucv->path_sever(iucv->path, NULL); |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 171 | iucv_path_free(iucv->path); |
| 172 | iucv->path = NULL; |
| 173 | } |
| 174 | break; |
| 175 | case IUCV_OPEN: |
| 176 | case IUCV_BOUND: |
| 177 | case IUCV_LISTEN: |
| 178 | case IUCV_CLOSED: |
| 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | read_unlock(&iucv_sk_list.lock); |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * afiucv_pm_restore_thaw() - Thaw and restore PM callback |
| 189 | * @dev: AFIUCV dummy device |
| 190 | * |
| 191 | * socket clean up after freeze |
| 192 | */ |
| 193 | static int afiucv_pm_restore_thaw(struct device *dev) |
| 194 | { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 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) { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 203 | switch (sk->sk_state) { |
| 204 | case IUCV_CONNECTED: |
| 205 | sk->sk_err = EPIPE; |
| 206 | sk->sk_state = IUCV_DISCONN; |
| 207 | sk->sk_state_change(sk); |
| 208 | break; |
| 209 | case IUCV_DISCONN: |
| 210 | case IUCV_SEVERED: |
| 211 | case IUCV_CLOSING: |
| 212 | case IUCV_LISTEN: |
| 213 | case IUCV_BOUND: |
| 214 | case IUCV_OPEN: |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | read_unlock(&iucv_sk_list.lock); |
| 220 | return 0; |
| 221 | } |
| 222 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 223 | static const struct dev_pm_ops afiucv_pm_ops = { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 224 | .prepare = afiucv_pm_prepare, |
| 225 | .complete = afiucv_pm_complete, |
| 226 | .freeze = afiucv_pm_freeze, |
| 227 | .thaw = afiucv_pm_restore_thaw, |
| 228 | .restore = afiucv_pm_restore_thaw, |
| 229 | }; |
| 230 | |
| 231 | static struct device_driver af_iucv_driver = { |
| 232 | .owner = THIS_MODULE, |
| 233 | .name = "afiucv", |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 234 | .bus = NULL, |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 235 | .pm = &afiucv_pm_ops, |
| 236 | }; |
| 237 | |
| 238 | /* dummy device used as trigger for PM functions */ |
| 239 | static struct device *af_iucv_dev; |
| 240 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 241 | /** |
| 242 | * iucv_msg_length() - Returns the length of an iucv message. |
| 243 | * @msg: Pointer to struct iucv_message, MUST NOT be NULL |
| 244 | * |
| 245 | * The function returns the length of the specified iucv message @msg of data |
| 246 | * stored in a buffer and of data stored in the parameter list (PRMDATA). |
| 247 | * |
| 248 | * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket |
| 249 | * data: |
| 250 | * PRMDATA[0..6] socket data (max 7 bytes); |
| 251 | * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7]) |
| 252 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 253 | * The socket data length is computed by subtracting the socket data length |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 254 | * value from 0xFF. |
| 255 | * If the socket data len is greater 7, then PRMDATA can be used for special |
| 256 | * notifications (see iucv_sock_shutdown); and further, |
| 257 | * if the socket data len is > 7, the function returns 8. |
| 258 | * |
| 259 | * Use this function to allocate socket buffers to store iucv message data. |
| 260 | */ |
| 261 | static inline size_t iucv_msg_length(struct iucv_message *msg) |
| 262 | { |
| 263 | size_t datalen; |
| 264 | |
| 265 | if (msg->flags & IUCV_IPRMDATA) { |
| 266 | datalen = 0xff - msg->rmmsg[7]; |
| 267 | return (datalen < 8) ? datalen : 8; |
| 268 | } |
| 269 | return msg->length; |
| 270 | } |
| 271 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 272 | /** |
| 273 | * iucv_sock_in_state() - check for specific states |
| 274 | * @sk: sock structure |
| 275 | * @state: first iucv sk state |
| 276 | * @state: second iucv sk state |
| 277 | * |
| 278 | * Returns true if the socket in either in the first or second state. |
| 279 | */ |
| 280 | static int iucv_sock_in_state(struct sock *sk, int state, int state2) |
| 281 | { |
| 282 | return (sk->sk_state == state || sk->sk_state == state2); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * iucv_below_msglim() - function to check if messages can be sent |
| 287 | * @sk: sock structure |
| 288 | * |
| 289 | * Returns true if the send queue length is lower than the message limit. |
| 290 | * Always returns true if the socket is not connected (no iucv path for |
| 291 | * checking the message limit). |
| 292 | */ |
| 293 | static inline int iucv_below_msglim(struct sock *sk) |
| 294 | { |
| 295 | struct iucv_sock *iucv = iucv_sk(sk); |
| 296 | |
| 297 | if (sk->sk_state != IUCV_CONNECTED) |
| 298 | return 1; |
| 299 | return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit |
| 304 | */ |
| 305 | static void iucv_sock_wake_msglim(struct sock *sk) |
| 306 | { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 307 | struct socket_wq *wq; |
| 308 | |
| 309 | rcu_read_lock(); |
| 310 | wq = rcu_dereference(sk->sk_wq); |
| 311 | if (wq_has_sleeper(wq)) |
| 312 | wake_up_interruptible_all(&wq->wait); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 313 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 314 | rcu_read_unlock(); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 317 | /* Timers */ |
| 318 | static void iucv_sock_timeout(unsigned long arg) |
| 319 | { |
| 320 | struct sock *sk = (struct sock *)arg; |
| 321 | |
| 322 | bh_lock_sock(sk); |
| 323 | sk->sk_err = ETIMEDOUT; |
| 324 | sk->sk_state_change(sk); |
| 325 | bh_unlock_sock(sk); |
| 326 | |
| 327 | iucv_sock_kill(sk); |
| 328 | sock_put(sk); |
| 329 | } |
| 330 | |
| 331 | static void iucv_sock_clear_timer(struct sock *sk) |
| 332 | { |
| 333 | sk_stop_timer(sk, &sk->sk_timer); |
| 334 | } |
| 335 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 336 | static struct sock *__iucv_get_sock_by_name(char *nm) |
| 337 | { |
| 338 | struct sock *sk; |
| 339 | struct hlist_node *node; |
| 340 | |
| 341 | sk_for_each(sk, node, &iucv_sk_list.head) |
| 342 | if (!memcmp(&iucv_sk(sk)->src_name, nm, 8)) |
| 343 | return sk; |
| 344 | |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | static void iucv_sock_destruct(struct sock *sk) |
| 349 | { |
| 350 | skb_queue_purge(&sk->sk_receive_queue); |
| 351 | skb_queue_purge(&sk->sk_write_queue); |
| 352 | } |
| 353 | |
| 354 | /* Cleanup Listen */ |
| 355 | static void iucv_sock_cleanup_listen(struct sock *parent) |
| 356 | { |
| 357 | struct sock *sk; |
| 358 | |
| 359 | /* Close non-accepted connections */ |
| 360 | while ((sk = iucv_accept_dequeue(parent, NULL))) { |
| 361 | iucv_sock_close(sk); |
| 362 | iucv_sock_kill(sk); |
| 363 | } |
| 364 | |
| 365 | parent->sk_state = IUCV_CLOSED; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 368 | /* Kill socket (only if zapped and orphaned) */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 369 | static void iucv_sock_kill(struct sock *sk) |
| 370 | { |
| 371 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 372 | return; |
| 373 | |
| 374 | iucv_sock_unlink(&iucv_sk_list, sk); |
| 375 | sock_set_flag(sk, SOCK_DEAD); |
| 376 | sock_put(sk); |
| 377 | } |
| 378 | |
| 379 | /* Close an IUCV socket */ |
| 380 | static void iucv_sock_close(struct sock *sk) |
| 381 | { |
| 382 | unsigned char user_data[16]; |
| 383 | struct iucv_sock *iucv = iucv_sk(sk); |
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: |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 396 | sk->sk_state = IUCV_CLOSING; |
| 397 | sk->sk_state_change(sk); |
| 398 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 399 | if (!skb_queue_empty(&iucv->send_skb_q)) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 400 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 401 | timeo = sk->sk_lingertime; |
| 402 | else |
| 403 | timeo = IUCV_DISCONN_TIMEOUT; |
Ursula Braun | 9f6298a | 2011-05-12 18:45:08 +0000 | [diff] [blame] | 404 | iucv_sock_wait(sk, |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 405 | iucv_sock_in_state(sk, IUCV_CLOSED, 0), |
| 406 | timeo); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Ursula Braun | bbe188c | 2009-04-21 06:04:20 +0000 | [diff] [blame] | 409 | case IUCV_CLOSING: /* fall through */ |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 410 | sk->sk_state = IUCV_CLOSED; |
| 411 | sk->sk_state_change(sk); |
| 412 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 413 | if (iucv->path) { |
| 414 | low_nmcpy(user_data, iucv->src_name); |
| 415 | high_nmcpy(user_data, iucv->dst_name); |
| 416 | ASCEBC(user_data, sizeof(user_data)); |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 417 | pr_iucv->path_sever(iucv->path, user_data); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 418 | iucv_path_free(iucv->path); |
| 419 | iucv->path = NULL; |
| 420 | } |
| 421 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 422 | sk->sk_err = ECONNRESET; |
| 423 | sk->sk_state_change(sk); |
| 424 | |
| 425 | skb_queue_purge(&iucv->send_skb_q); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 426 | skb_queue_purge(&iucv->backlog_skb_q); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | default: |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 430 | /* nothing to do here */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 431 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 432 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 433 | |
Hendrik Brueckner | 7514bab | 2009-09-16 04:37:27 +0000 | [diff] [blame] | 434 | /* mark socket for deletion by iucv_sock_kill() */ |
| 435 | sock_set_flag(sk, SOCK_ZAPPED); |
| 436 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 437 | release_sock(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void iucv_sock_init(struct sock *sk, struct sock *parent) |
| 441 | { |
| 442 | if (parent) |
| 443 | sk->sk_type = parent->sk_type; |
| 444 | } |
| 445 | |
| 446 | static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio) |
| 447 | { |
| 448 | struct sock *sk; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 449 | struct iucv_sock *iucv; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 450 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 451 | sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 452 | if (!sk) |
| 453 | return NULL; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 454 | iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 455 | |
| 456 | sock_init_data(sock, sk); |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 457 | INIT_LIST_HEAD(&iucv->accept_q); |
| 458 | spin_lock_init(&iucv->accept_q_lock); |
| 459 | skb_queue_head_init(&iucv->send_skb_q); |
| 460 | INIT_LIST_HEAD(&iucv->message_q.list); |
| 461 | spin_lock_init(&iucv->message_q.lock); |
| 462 | skb_queue_head_init(&iucv->backlog_skb_q); |
| 463 | iucv->send_tag = 0; |
| 464 | iucv->flags = 0; |
| 465 | iucv->msglimit = IUCV_QUEUELEN_DEFAULT; |
| 466 | iucv->path = NULL; |
| 467 | memset(&iucv->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 Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 485 | static int iucv_sock_create(struct net *net, struct socket *sock, int protocol, |
| 486 | int kern) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 487 | { |
| 488 | struct sock *sk; |
| 489 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 490 | if (protocol && protocol != PF_IUCV) |
| 491 | return -EPROTONOSUPPORT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 492 | |
| 493 | sock->state = SS_UNCONNECTED; |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 494 | |
| 495 | switch (sock->type) { |
| 496 | case SOCK_STREAM: |
| 497 | sock->ops = &iucv_sock_ops; |
| 498 | break; |
| 499 | case SOCK_SEQPACKET: |
| 500 | /* currently, proto ops can handle both sk types */ |
| 501 | sock->ops = &iucv_sock_ops; |
| 502 | break; |
| 503 | default: |
| 504 | return -ESOCKTNOSUPPORT; |
| 505 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 506 | |
| 507 | sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL); |
| 508 | if (!sk) |
| 509 | return -ENOMEM; |
| 510 | |
| 511 | iucv_sock_init(sk, NULL); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk) |
| 517 | { |
| 518 | write_lock_bh(&l->lock); |
| 519 | sk_add_node(sk, &l->head); |
| 520 | write_unlock_bh(&l->lock); |
| 521 | } |
| 522 | |
| 523 | void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk) |
| 524 | { |
| 525 | write_lock_bh(&l->lock); |
| 526 | sk_del_node_init(sk); |
| 527 | write_unlock_bh(&l->lock); |
| 528 | } |
| 529 | |
| 530 | void iucv_accept_enqueue(struct sock *parent, struct sock *sk) |
| 531 | { |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 532 | unsigned long flags; |
| 533 | struct iucv_sock *par = iucv_sk(parent); |
| 534 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 535 | sock_hold(sk); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 536 | spin_lock_irqsave(&par->accept_q_lock, flags); |
| 537 | list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q); |
| 538 | spin_unlock_irqrestore(&par->accept_q_lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 539 | iucv_sk(sk)->parent = parent; |
Hendrik Brueckner | 49f5eba | 2009-10-14 22:54:55 +0000 | [diff] [blame] | 540 | sk_acceptq_added(parent); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void iucv_accept_unlink(struct sock *sk) |
| 544 | { |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 545 | unsigned long flags; |
| 546 | struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent); |
| 547 | |
| 548 | spin_lock_irqsave(&par->accept_q_lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 549 | list_del_init(&iucv_sk(sk)->accept_q); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 550 | spin_unlock_irqrestore(&par->accept_q_lock, flags); |
Hendrik Brueckner | 49f5eba | 2009-10-14 22:54:55 +0000 | [diff] [blame] | 551 | sk_acceptq_removed(iucv_sk(sk)->parent); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 552 | iucv_sk(sk)->parent = NULL; |
| 553 | sock_put(sk); |
| 554 | } |
| 555 | |
| 556 | struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock) |
| 557 | { |
| 558 | struct iucv_sock *isk, *n; |
| 559 | struct sock *sk; |
| 560 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 561 | 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] | 562 | sk = (struct sock *) isk; |
| 563 | lock_sock(sk); |
| 564 | |
| 565 | if (sk->sk_state == IUCV_CLOSED) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 566 | iucv_accept_unlink(sk); |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 567 | release_sock(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | |
| 571 | if (sk->sk_state == IUCV_CONNECTED || |
| 572 | sk->sk_state == IUCV_SEVERED || |
Hendrik Brueckner | 56a73de | 2009-09-16 04:37:26 +0000 | [diff] [blame] | 573 | sk->sk_state == IUCV_DISCONN || /* due to PM restore */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 574 | !newsock) { |
| 575 | iucv_accept_unlink(sk); |
| 576 | if (newsock) |
| 577 | sock_graft(sk, newsock); |
| 578 | |
| 579 | if (sk->sk_state == IUCV_SEVERED) |
| 580 | sk->sk_state = IUCV_DISCONN; |
| 581 | |
| 582 | release_sock(sk); |
| 583 | return sk; |
| 584 | } |
| 585 | |
| 586 | release_sock(sk); |
| 587 | } |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 591 | /* Bind an unbound socket */ |
| 592 | static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr, |
| 593 | int addr_len) |
| 594 | { |
| 595 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 596 | struct sock *sk = sock->sk; |
| 597 | struct iucv_sock *iucv; |
| 598 | int err; |
| 599 | |
| 600 | /* Verify the input sockaddr */ |
| 601 | if (!addr || addr->sa_family != AF_IUCV) |
| 602 | return -EINVAL; |
| 603 | |
| 604 | lock_sock(sk); |
| 605 | if (sk->sk_state != IUCV_OPEN) { |
| 606 | err = -EBADFD; |
| 607 | goto done; |
| 608 | } |
| 609 | |
| 610 | write_lock_bh(&iucv_sk_list.lock); |
| 611 | |
| 612 | iucv = iucv_sk(sk); |
| 613 | if (__iucv_get_sock_by_name(sa->siucv_name)) { |
| 614 | err = -EADDRINUSE; |
| 615 | goto done_unlock; |
| 616 | } |
| 617 | if (iucv->path) { |
| 618 | err = 0; |
| 619 | goto done_unlock; |
| 620 | } |
| 621 | |
| 622 | /* Bind the socket */ |
| 623 | memcpy(iucv->src_name, sa->siucv_name, 8); |
| 624 | |
| 625 | /* Copy the user id */ |
| 626 | memcpy(iucv->src_user_id, iucv_userid, 8); |
| 627 | sk->sk_state = IUCV_BOUND; |
| 628 | err = 0; |
| 629 | |
| 630 | done_unlock: |
| 631 | /* Release the socket list lock */ |
| 632 | write_unlock_bh(&iucv_sk_list.lock); |
| 633 | done: |
| 634 | release_sock(sk); |
| 635 | return err; |
| 636 | } |
| 637 | |
| 638 | /* Automatically bind an unbound socket */ |
| 639 | static int iucv_sock_autobind(struct sock *sk) |
| 640 | { |
| 641 | struct iucv_sock *iucv = iucv_sk(sk); |
| 642 | char query_buffer[80]; |
| 643 | char name[12]; |
| 644 | int err = 0; |
| 645 | |
| 646 | /* Set the userid and name */ |
| 647 | cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err); |
| 648 | if (unlikely(err)) |
| 649 | return -EPROTO; |
| 650 | |
| 651 | memcpy(iucv->src_user_id, query_buffer, 8); |
| 652 | |
| 653 | write_lock_bh(&iucv_sk_list.lock); |
| 654 | |
| 655 | sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name)); |
| 656 | while (__iucv_get_sock_by_name(name)) { |
| 657 | sprintf(name, "%08x", |
| 658 | atomic_inc_return(&iucv_sk_list.autobind_name)); |
| 659 | } |
| 660 | |
| 661 | write_unlock_bh(&iucv_sk_list.lock); |
| 662 | |
| 663 | memcpy(&iucv->src_name, name, 8); |
| 664 | |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | /* Connect an unconnected socket */ |
| 669 | static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr, |
| 670 | int alen, int flags) |
| 671 | { |
| 672 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 673 | struct sock *sk = sock->sk; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 674 | struct iucv_sock *iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 675 | unsigned char user_data[16]; |
| 676 | int err; |
| 677 | |
| 678 | if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv)) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND) |
| 682 | return -EBADFD; |
| 683 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 684 | if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 685 | return -EINVAL; |
| 686 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 687 | if (sk->sk_state == IUCV_OPEN) { |
| 688 | err = iucv_sock_autobind(sk); |
| 689 | if (unlikely(err)) |
| 690 | return err; |
| 691 | } |
| 692 | |
| 693 | lock_sock(sk); |
| 694 | |
| 695 | /* Set the destination information */ |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 696 | memcpy(iucv->dst_user_id, sa->siucv_user_id, 8); |
| 697 | memcpy(iucv->dst_name, sa->siucv_name, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 698 | |
| 699 | high_nmcpy(user_data, sa->siucv_name); |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 700 | low_nmcpy(user_data, iucv->src_name); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 701 | ASCEBC(user_data, sizeof(user_data)); |
| 702 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 703 | /* Create path. */ |
Hendrik Brueckner | 09488e2e | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 704 | iucv->path = iucv_path_alloc(iucv->msglimit, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 705 | IUCV_IPRMDATA, GFP_KERNEL); |
Ursula Braun | d444472 | 2008-02-07 18:07:19 -0800 | [diff] [blame] | 706 | if (!iucv->path) { |
| 707 | err = -ENOMEM; |
| 708 | goto done; |
| 709 | } |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 710 | err = pr_iucv->path_connect(iucv->path, &af_iucv_handler, |
| 711 | sa->siucv_user_id, NULL, user_data, |
| 712 | sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 713 | if (err) { |
| 714 | iucv_path_free(iucv->path); |
| 715 | iucv->path = NULL; |
Hendrik Brueckner | 55cdea9 | 2009-01-05 18:07:07 -0800 | [diff] [blame] | 716 | switch (err) { |
| 717 | case 0x0b: /* Target communicator is not logged on */ |
| 718 | err = -ENETUNREACH; |
| 719 | break; |
| 720 | case 0x0d: /* Max connections for this guest exceeded */ |
| 721 | case 0x0e: /* Max connections for target guest exceeded */ |
| 722 | err = -EAGAIN; |
| 723 | break; |
| 724 | case 0x0f: /* Missing IUCV authorization */ |
| 725 | err = -EACCES; |
| 726 | break; |
| 727 | default: |
| 728 | err = -ECONNREFUSED; |
| 729 | break; |
| 730 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 731 | goto done; |
| 732 | } |
| 733 | |
| 734 | if (sk->sk_state != IUCV_CONNECTED) { |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 735 | err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED, |
| 736 | IUCV_DISCONN), |
| 737 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if (sk->sk_state == IUCV_DISCONN) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 741 | err = -ECONNREFUSED; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 742 | } |
Ursula Braun | 18becbc | 2009-01-05 18:07:46 -0800 | [diff] [blame] | 743 | |
| 744 | if (err) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 745 | pr_iucv->path_sever(iucv->path, NULL); |
Ursula Braun | 18becbc | 2009-01-05 18:07:46 -0800 | [diff] [blame] | 746 | iucv_path_free(iucv->path); |
| 747 | iucv->path = NULL; |
| 748 | } |
| 749 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 750 | done: |
| 751 | release_sock(sk); |
| 752 | return err; |
| 753 | } |
| 754 | |
| 755 | /* Move a socket into listening state. */ |
| 756 | static int iucv_sock_listen(struct socket *sock, int backlog) |
| 757 | { |
| 758 | struct sock *sk = sock->sk; |
| 759 | int err; |
| 760 | |
| 761 | lock_sock(sk); |
| 762 | |
| 763 | err = -EINVAL; |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 764 | if (sk->sk_state != IUCV_BOUND) |
| 765 | goto done; |
| 766 | |
| 767 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 768 | goto done; |
| 769 | |
| 770 | sk->sk_max_ack_backlog = backlog; |
| 771 | sk->sk_ack_backlog = 0; |
| 772 | sk->sk_state = IUCV_LISTEN; |
| 773 | err = 0; |
| 774 | |
| 775 | done: |
| 776 | release_sock(sk); |
| 777 | return err; |
| 778 | } |
| 779 | |
| 780 | /* Accept a pending connection */ |
| 781 | static int iucv_sock_accept(struct socket *sock, struct socket *newsock, |
| 782 | int flags) |
| 783 | { |
| 784 | DECLARE_WAITQUEUE(wait, current); |
| 785 | struct sock *sk = sock->sk, *nsk; |
| 786 | long timeo; |
| 787 | int err = 0; |
| 788 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 789 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 790 | |
| 791 | if (sk->sk_state != IUCV_LISTEN) { |
| 792 | err = -EBADFD; |
| 793 | goto done; |
| 794 | } |
| 795 | |
| 796 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 797 | |
| 798 | /* Wait for an incoming connection */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 799 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 800 | while (!(nsk = iucv_accept_dequeue(sk, newsock))) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 801 | set_current_state(TASK_INTERRUPTIBLE); |
| 802 | if (!timeo) { |
| 803 | err = -EAGAIN; |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | release_sock(sk); |
| 808 | timeo = schedule_timeout(timeo); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 809 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 810 | |
| 811 | if (sk->sk_state != IUCV_LISTEN) { |
| 812 | err = -EBADFD; |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | if (signal_pending(current)) { |
| 817 | err = sock_intr_errno(timeo); |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 823 | remove_wait_queue(sk_sleep(sk), &wait); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 824 | |
| 825 | if (err) |
| 826 | goto done; |
| 827 | |
| 828 | newsock->state = SS_CONNECTED; |
| 829 | |
| 830 | done: |
| 831 | release_sock(sk); |
| 832 | return err; |
| 833 | } |
| 834 | |
| 835 | static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 836 | int *len, int peer) |
| 837 | { |
| 838 | struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr; |
| 839 | struct sock *sk = sock->sk; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 840 | struct iucv_sock *iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 841 | |
| 842 | addr->sa_family = AF_IUCV; |
| 843 | *len = sizeof(struct sockaddr_iucv); |
| 844 | |
| 845 | if (peer) { |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 846 | memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8); |
| 847 | memcpy(siucv->siucv_name, iucv->dst_name, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 848 | } else { |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 849 | memcpy(siucv->siucv_user_id, iucv->src_user_id, 8); |
| 850 | memcpy(siucv->siucv_name, iucv->src_name, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 851 | } |
| 852 | memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port)); |
| 853 | memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr)); |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame^] | 854 | memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid)); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 859 | /** |
| 860 | * iucv_send_iprm() - Send socket data in parameter list of an iucv message. |
| 861 | * @path: IUCV path |
| 862 | * @msg: Pointer to a struct iucv_message |
| 863 | * @skb: The socket data to send, skb->len MUST BE <= 7 |
| 864 | * |
| 865 | * Send the socket data in the parameter list in the iucv message |
| 866 | * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter |
| 867 | * list and the socket data len at index 7 (last byte). |
| 868 | * See also iucv_msg_length(). |
| 869 | * |
| 870 | * Returns the error code from the iucv_message_send() call. |
| 871 | */ |
| 872 | static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg, |
| 873 | struct sk_buff *skb) |
| 874 | { |
| 875 | u8 prmdata[8]; |
| 876 | |
| 877 | memcpy(prmdata, (void *) skb->data, skb->len); |
| 878 | prmdata[7] = 0xff - (u8) skb->len; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 879 | return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 880 | (void *) prmdata, 8); |
| 881 | } |
| 882 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 883 | static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 884 | struct msghdr *msg, size_t len) |
| 885 | { |
| 886 | struct sock *sk = sock->sk; |
| 887 | struct iucv_sock *iucv = iucv_sk(sk); |
| 888 | struct sk_buff *skb; |
| 889 | struct iucv_message txmsg; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 890 | struct cmsghdr *cmsg; |
| 891 | int cmsg_done; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 892 | long timeo; |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 893 | char user_id[9]; |
| 894 | char appl_id[9]; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 895 | int err; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 896 | int noblock = msg->msg_flags & MSG_DONTWAIT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 897 | |
| 898 | err = sock_error(sk); |
| 899 | if (err) |
| 900 | return err; |
| 901 | |
| 902 | if (msg->msg_flags & MSG_OOB) |
| 903 | return -EOPNOTSUPP; |
| 904 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 905 | /* SOCK_SEQPACKET: we do not support segmented records */ |
| 906 | if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR)) |
| 907 | return -EOPNOTSUPP; |
| 908 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 909 | lock_sock(sk); |
| 910 | |
| 911 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 912 | err = -EPIPE; |
| 913 | goto out; |
| 914 | } |
| 915 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 916 | /* Return if the socket is not in connected state */ |
| 917 | if (sk->sk_state != IUCV_CONNECTED) { |
| 918 | err = -ENOTCONN; |
| 919 | goto out; |
| 920 | } |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 921 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 922 | /* initialize defaults */ |
| 923 | cmsg_done = 0; /* check for duplicate headers */ |
| 924 | txmsg.class = 0; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 925 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 926 | /* iterate over control messages */ |
| 927 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; |
| 928 | cmsg = CMSG_NXTHDR(msg, cmsg)) { |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 929 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 930 | if (!CMSG_OK(msg, cmsg)) { |
| 931 | err = -EINVAL; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 932 | goto out; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 935 | if (cmsg->cmsg_level != SOL_IUCV) |
| 936 | continue; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 937 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 938 | if (cmsg->cmsg_type & cmsg_done) { |
| 939 | err = -EINVAL; |
| 940 | goto out; |
| 941 | } |
| 942 | cmsg_done |= cmsg->cmsg_type; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 943 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 944 | switch (cmsg->cmsg_type) { |
| 945 | case SCM_IUCV_TRGCLS: |
| 946 | if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) { |
| 947 | err = -EINVAL; |
| 948 | goto out; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 951 | /* set iucv message target class */ |
| 952 | memcpy(&txmsg.class, |
| 953 | (void *) CMSG_DATA(cmsg), TRGCLS_SIZE); |
| 954 | |
| 955 | break; |
| 956 | |
| 957 | default: |
| 958 | err = -EINVAL; |
| 959 | goto out; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | /* allocate one skb for each iucv message: |
| 965 | * this is fine for SOCK_SEQPACKET (unless we want to support |
| 966 | * segmented records using the MSG_EOR flag), but |
| 967 | * for SOCK_STREAM we might want to improve it in future */ |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 968 | skb = sock_alloc_send_skb(sk, len, noblock, &err); |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 969 | if (!skb) |
| 970 | goto out; |
| 971 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
| 972 | err = -EFAULT; |
| 973 | goto fail; |
| 974 | } |
| 975 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 976 | /* wait if outstanding messages for iucv path has reached */ |
| 977 | timeo = sock_sndtimeo(sk, noblock); |
| 978 | err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo); |
| 979 | if (err) |
| 980 | goto fail; |
| 981 | |
| 982 | /* return -ECONNRESET if the socket is no longer connected */ |
| 983 | if (sk->sk_state != IUCV_CONNECTED) { |
| 984 | err = -ECONNRESET; |
| 985 | goto fail; |
| 986 | } |
| 987 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 988 | /* increment and save iucv message tag for msg_completion cbk */ |
| 989 | txmsg.tag = iucv->send_tag++; |
| 990 | memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN); |
| 991 | skb_queue_tail(&iucv->send_skb_q, skb); |
| 992 | |
| 993 | if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) |
| 994 | && skb->len <= 7) { |
| 995 | err = iucv_send_iprm(iucv->path, &txmsg, skb); |
| 996 | |
| 997 | /* on success: there is no message_complete callback |
| 998 | * for an IPRMDATA msg; remove skb from send queue */ |
| 999 | if (err == 0) { |
| 1000 | skb_unlink(skb, &iucv->send_skb_q); |
| 1001 | kfree_skb(skb); |
| 1002 | } |
| 1003 | |
| 1004 | /* this error should never happen since the |
| 1005 | * IUCV_IPRMDATA path flag is set... sever path */ |
| 1006 | if (err == 0x15) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1007 | pr_iucv->path_sever(iucv->path, NULL); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1008 | skb_unlink(skb, &iucv->send_skb_q); |
| 1009 | err = -EPIPE; |
| 1010 | goto fail; |
| 1011 | } |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1012 | } else |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1013 | err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0, |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1014 | (void *) skb->data, skb->len); |
| 1015 | if (err) { |
| 1016 | if (err == 3) { |
| 1017 | user_id[8] = 0; |
| 1018 | memcpy(user_id, iucv->dst_user_id, 8); |
| 1019 | appl_id[8] = 0; |
| 1020 | memcpy(appl_id, iucv->dst_name, 8); |
| 1021 | pr_err("Application %s on z/VM guest %s" |
| 1022 | " exceeds message limit\n", |
| 1023 | appl_id, user_id); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1024 | err = -EAGAIN; |
| 1025 | } else |
| 1026 | err = -EPIPE; |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1027 | skb_unlink(skb, &iucv->send_skb_q); |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1028 | goto fail; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | release_sock(sk); |
| 1032 | return len; |
| 1033 | |
| 1034 | fail: |
| 1035 | kfree_skb(skb); |
| 1036 | out: |
| 1037 | release_sock(sk); |
| 1038 | return err; |
| 1039 | } |
| 1040 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1041 | /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's |
| 1042 | * |
| 1043 | * Locking: must be called with message_q.lock held |
| 1044 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1045 | static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len) |
| 1046 | { |
| 1047 | int dataleft, size, copied = 0; |
| 1048 | struct sk_buff *nskb; |
| 1049 | |
| 1050 | dataleft = len; |
| 1051 | while (dataleft) { |
| 1052 | if (dataleft >= sk->sk_rcvbuf / 4) |
| 1053 | size = sk->sk_rcvbuf / 4; |
| 1054 | else |
| 1055 | size = dataleft; |
| 1056 | |
| 1057 | nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA); |
| 1058 | if (!nskb) |
| 1059 | return -ENOMEM; |
| 1060 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1061 | /* copy target class to control buffer of new skb */ |
| 1062 | memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN); |
| 1063 | |
| 1064 | /* copy data fragment */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1065 | memcpy(nskb->data, skb->data + copied, size); |
| 1066 | copied += size; |
| 1067 | dataleft -= size; |
| 1068 | |
| 1069 | skb_reset_transport_header(nskb); |
| 1070 | skb_reset_network_header(nskb); |
| 1071 | nskb->len = size; |
| 1072 | |
| 1073 | skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb); |
| 1074 | } |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1079 | /* iucv_process_message() - Receive a single outstanding IUCV message |
| 1080 | * |
| 1081 | * Locking: must be called with message_q.lock held |
| 1082 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1083 | static void iucv_process_message(struct sock *sk, struct sk_buff *skb, |
| 1084 | struct iucv_path *path, |
| 1085 | struct iucv_message *msg) |
| 1086 | { |
| 1087 | int rc; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1088 | unsigned int len; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1089 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1090 | len = iucv_msg_length(msg); |
| 1091 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1092 | /* store msg target class in the second 4 bytes of skb ctrl buffer */ |
| 1093 | /* Note: the first 4 bytes are reserved for msg tag */ |
| 1094 | memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN); |
| 1095 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1096 | /* check for special IPRM messages (e.g. iucv_sock_shutdown) */ |
| 1097 | if ((msg->flags & IUCV_IPRMDATA) && len > 7) { |
| 1098 | if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) { |
| 1099 | skb->data = NULL; |
| 1100 | skb->len = 0; |
| 1101 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1102 | } else { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1103 | rc = pr_iucv->message_receive(path, msg, |
| 1104 | 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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1119 | pr_iucv->path_sever(path, NULL); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 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; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1336 | err = pr_iucv->message_send(iucv->path, &txmsg, IUCV_IPRMDATA, |
| 1337 | 0, (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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1354 | err = pr_iucv->path_quiesce(iucv->path, NULL); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1381 | pr_iucv->path_sever(iucv_sk(sk)->path, NULL); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 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 | 09488e2e | 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 | 09488e2e | 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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1523 | err = pr_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)) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1530 | err = pr_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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1538 | err = pr_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 | 09488e2e | 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; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1562 | err = pr_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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1564 | err = pr_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) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1598 | pr_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) |
Julia Lawall | a56635a | 2010-05-26 05:56:48 +0000 | [diff] [blame] | 1623 | goto out_unlock; |
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 | |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1727 | static int __init afiucv_iucv_init(void) |
| 1728 | { |
| 1729 | int err; |
| 1730 | |
| 1731 | err = pr_iucv->iucv_register(&af_iucv_handler, 0); |
| 1732 | if (err) |
| 1733 | goto out; |
| 1734 | /* establish dummy device */ |
| 1735 | af_iucv_driver.bus = pr_iucv->bus; |
| 1736 | err = driver_register(&af_iucv_driver); |
| 1737 | if (err) |
| 1738 | goto out_iucv; |
| 1739 | af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 1740 | if (!af_iucv_dev) { |
| 1741 | err = -ENOMEM; |
| 1742 | goto out_driver; |
| 1743 | } |
| 1744 | dev_set_name(af_iucv_dev, "af_iucv"); |
| 1745 | af_iucv_dev->bus = pr_iucv->bus; |
| 1746 | af_iucv_dev->parent = pr_iucv->root; |
| 1747 | af_iucv_dev->release = (void (*)(struct device *))kfree; |
| 1748 | af_iucv_dev->driver = &af_iucv_driver; |
| 1749 | err = device_register(af_iucv_dev); |
| 1750 | if (err) |
| 1751 | goto out_driver; |
| 1752 | return 0; |
| 1753 | |
| 1754 | out_driver: |
| 1755 | driver_unregister(&af_iucv_driver); |
| 1756 | out_iucv: |
| 1757 | pr_iucv->iucv_unregister(&af_iucv_handler, 0); |
| 1758 | out: |
| 1759 | return err; |
| 1760 | } |
| 1761 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1762 | static int __init afiucv_init(void) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1763 | { |
| 1764 | int err; |
| 1765 | |
| 1766 | if (!MACHINE_IS_VM) { |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 1767 | pr_err("The af_iucv module cannot be loaded" |
| 1768 | " without z/VM\n"); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1769 | err = -EPROTONOSUPPORT; |
| 1770 | goto out; |
| 1771 | } |
| 1772 | cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err); |
| 1773 | if (unlikely(err)) { |
Ursula Braun | c2b4afd | 2008-07-14 09:59:29 +0200 | [diff] [blame] | 1774 | WARN_ON(err); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1775 | err = -EPROTONOSUPPORT; |
| 1776 | goto out; |
| 1777 | } |
| 1778 | |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1779 | pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv"); |
| 1780 | if (!pr_iucv) { |
| 1781 | printk(KERN_WARNING "iucv_if lookup failed\n"); |
| 1782 | err = -EPROTONOSUPPORT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1783 | goto out; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1786 | err = proto_register(&iucv_proto, 0); |
| 1787 | if (err) |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1788 | goto out; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1789 | err = sock_register(&iucv_sock_family_ops); |
| 1790 | if (err) |
| 1791 | goto out_proto; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1792 | |
| 1793 | err = afiucv_iucv_init(); |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1794 | if (err) |
| 1795 | goto out_sock; |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1796 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1797 | return 0; |
| 1798 | |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1799 | out_sock: |
| 1800 | sock_unregister(PF_IUCV); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1801 | out_proto: |
| 1802 | proto_unregister(&iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1803 | out: |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1804 | if (pr_iucv) |
| 1805 | symbol_put(iucv_if); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1806 | return err; |
| 1807 | } |
| 1808 | |
| 1809 | static void __exit afiucv_exit(void) |
| 1810 | { |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 1811 | device_unregister(af_iucv_dev); |
| 1812 | driver_unregister(&af_iucv_driver); |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1813 | pr_iucv->iucv_unregister(&af_iucv_handler, 0); |
| 1814 | symbol_put(iucv_if); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1815 | sock_unregister(PF_IUCV); |
| 1816 | proto_unregister(&iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | module_init(afiucv_init); |
| 1820 | module_exit(afiucv_exit); |
| 1821 | |
| 1822 | MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>"); |
| 1823 | MODULE_DESCRIPTION("IUCV Sockets ver " VERSION); |
| 1824 | MODULE_VERSION(VERSION); |
| 1825 | MODULE_LICENSE("GPL"); |
| 1826 | MODULE_ALIAS_NETPROTO(PF_IUCV); |