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