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