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