1 /* 2 * IUCV protocol stack for Linux on zSeries 3 * 4 * Copyright IBM Corp. 2006, 2009 5 * 6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com> 7 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com> 8 * PM functions: 9 * Ursula Braun <ursula.braun@de.ibm.com> 10 */ 11 12 #define KMSG_COMPONENT "af_iucv" 13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 14 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/list.h> 18 #include <linux/errno.h> 19 #include <linux/kernel.h> 20 #include <linux/sched/signal.h> 21 #include <linux/slab.h> 22 #include <linux/skbuff.h> 23 #include <linux/init.h> 24 #include <linux/poll.h> 25 #include <linux/security.h> 26 #include <net/sock.h> 27 #include <asm/ebcdic.h> 28 #include <asm/cpcmd.h> 29 #include <linux/kmod.h> 30 31 #include <net/iucv/af_iucv.h> 32 33 #define VERSION "1.2" 34 35 static char iucv_userid[80]; 36 37 static const struct proto_ops iucv_sock_ops; 38 39 static struct proto iucv_proto = { 40 .name = "AF_IUCV", 41 .owner = THIS_MODULE, 42 .obj_size = sizeof(struct iucv_sock), 43 }; 44 45 static struct iucv_interface *pr_iucv; 46 47 /* special AF_IUCV IPRM messages */ 48 static const u8 iprm_shutdown[8] = 49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; 50 51 #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class)) 52 53 #define __iucv_sock_wait(sk, condition, timeo, ret) \ 54 do { \ 55 DEFINE_WAIT(__wait); \ 56 long __timeo = timeo; \ 57 ret = 0; \ 58 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \ 59 while (!(condition)) { \ 60 if (!__timeo) { \ 61 ret = -EAGAIN; \ 62 break; \ 63 } \ 64 if (signal_pending(current)) { \ 65 ret = sock_intr_errno(__timeo); \ 66 break; \ 67 } \ 68 release_sock(sk); \ 69 __timeo = schedule_timeout(__timeo); \ 70 lock_sock(sk); \ 71 ret = sock_error(sk); \ 72 if (ret) \ 73 break; \ 74 } \ 75 finish_wait(sk_sleep(sk), &__wait); \ 76 } while (0) 77 78 #define iucv_sock_wait(sk, condition, timeo) \ 79 ({ \ 80 int __ret = 0; \ 81 if (!(condition)) \ 82 __iucv_sock_wait(sk, condition, timeo, __ret); \ 83 __ret; \ 84 }) 85 86 static void iucv_sock_kill(struct sock *sk); 87 static void iucv_sock_close(struct sock *sk); 88 static void iucv_sever_path(struct sock *, int); 89 90 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev, 91 struct packet_type *pt, struct net_device *orig_dev); 92 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, 93 struct sk_buff *skb, u8 flags); 94 static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify); 95 96 /* Call Back functions */ 97 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *); 98 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *); 99 static void iucv_callback_connack(struct iucv_path *, u8 *); 100 static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *); 101 static void iucv_callback_connrej(struct iucv_path *, u8 *); 102 static void iucv_callback_shutdown(struct iucv_path *, u8 *); 103 104 static struct iucv_sock_list iucv_sk_list = { 105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock), 106 .autobind_name = ATOMIC_INIT(0) 107 }; 108 109 static struct iucv_handler af_iucv_handler = { 110 .path_pending = iucv_callback_connreq, 111 .path_complete = iucv_callback_connack, 112 .path_severed = iucv_callback_connrej, 113 .message_pending = iucv_callback_rx, 114 .message_complete = iucv_callback_txdone, 115 .path_quiesced = iucv_callback_shutdown, 116 }; 117 118 static inline void high_nmcpy(unsigned char *dst, char *src) 119 { 120 memcpy(dst, src, 8); 121 } 122 123 static inline void low_nmcpy(unsigned char *dst, char *src) 124 { 125 memcpy(&dst[8], src, 8); 126 } 127 128 static int afiucv_pm_prepare(struct device *dev) 129 { 130 #ifdef CONFIG_PM_DEBUG 131 printk(KERN_WARNING "afiucv_pm_prepare\n"); 132 #endif 133 return 0; 134 } 135 136 static void afiucv_pm_complete(struct device *dev) 137 { 138 #ifdef CONFIG_PM_DEBUG 139 printk(KERN_WARNING "afiucv_pm_complete\n"); 140 #endif 141 } 142 143 /** 144 * afiucv_pm_freeze() - Freeze PM callback 145 * @dev: AFIUCV dummy device 146 * 147 * Sever all established IUCV communication pathes 148 */ 149 static int afiucv_pm_freeze(struct device *dev) 150 { 151 struct iucv_sock *iucv; 152 struct sock *sk; 153 154 #ifdef CONFIG_PM_DEBUG 155 printk(KERN_WARNING "afiucv_pm_freeze\n"); 156 #endif 157 read_lock(&iucv_sk_list.lock); 158 sk_for_each(sk, &iucv_sk_list.head) { 159 iucv = iucv_sk(sk); 160 switch (sk->sk_state) { 161 case IUCV_DISCONN: 162 case IUCV_CLOSING: 163 case IUCV_CONNECTED: 164 iucv_sever_path(sk, 0); 165 break; 166 case IUCV_OPEN: 167 case IUCV_BOUND: 168 case IUCV_LISTEN: 169 case IUCV_CLOSED: 170 default: 171 break; 172 } 173 skb_queue_purge(&iucv->send_skb_q); 174 skb_queue_purge(&iucv->backlog_skb_q); 175 } 176 read_unlock(&iucv_sk_list.lock); 177 return 0; 178 } 179 180 /** 181 * afiucv_pm_restore_thaw() - Thaw and restore PM callback 182 * @dev: AFIUCV dummy device 183 * 184 * socket clean up after freeze 185 */ 186 static int afiucv_pm_restore_thaw(struct device *dev) 187 { 188 struct sock *sk; 189 190 #ifdef CONFIG_PM_DEBUG 191 printk(KERN_WARNING "afiucv_pm_restore_thaw\n"); 192 #endif 193 read_lock(&iucv_sk_list.lock); 194 sk_for_each(sk, &iucv_sk_list.head) { 195 switch (sk->sk_state) { 196 case IUCV_CONNECTED: 197 sk->sk_err = EPIPE; 198 sk->sk_state = IUCV_DISCONN; 199 sk->sk_state_change(sk); 200 break; 201 case IUCV_DISCONN: 202 case IUCV_CLOSING: 203 case IUCV_LISTEN: 204 case IUCV_BOUND: 205 case IUCV_OPEN: 206 default: 207 break; 208 } 209 } 210 read_unlock(&iucv_sk_list.lock); 211 return 0; 212 } 213 214 static const struct dev_pm_ops afiucv_pm_ops = { 215 .prepare = afiucv_pm_prepare, 216 .complete = afiucv_pm_complete, 217 .freeze = afiucv_pm_freeze, 218 .thaw = afiucv_pm_restore_thaw, 219 .restore = afiucv_pm_restore_thaw, 220 }; 221 222 static struct device_driver af_iucv_driver = { 223 .owner = THIS_MODULE, 224 .name = "afiucv", 225 .bus = NULL, 226 .pm = &afiucv_pm_ops, 227 }; 228 229 /* dummy device used as trigger for PM functions */ 230 static struct device *af_iucv_dev; 231 232 /** 233 * iucv_msg_length() - Returns the length of an iucv message. 234 * @msg: Pointer to struct iucv_message, MUST NOT be NULL 235 * 236 * The function returns the length of the specified iucv message @msg of data 237 * stored in a buffer and of data stored in the parameter list (PRMDATA). 238 * 239 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket 240 * data: 241 * PRMDATA[0..6] socket data (max 7 bytes); 242 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7]) 243 * 244 * The socket data length is computed by subtracting the socket data length 245 * value from 0xFF. 246 * If the socket data len is greater 7, then PRMDATA can be used for special 247 * notifications (see iucv_sock_shutdown); and further, 248 * if the socket data len is > 7, the function returns 8. 249 * 250 * Use this function to allocate socket buffers to store iucv message data. 251 */ 252 static inline size_t iucv_msg_length(struct iucv_message *msg) 253 { 254 size_t datalen; 255 256 if (msg->flags & IUCV_IPRMDATA) { 257 datalen = 0xff - msg->rmmsg[7]; 258 return (datalen < 8) ? datalen : 8; 259 } 260 return msg->length; 261 } 262 263 /** 264 * iucv_sock_in_state() - check for specific states 265 * @sk: sock structure 266 * @state: first iucv sk state 267 * @state: second iucv sk state 268 * 269 * Returns true if the socket in either in the first or second state. 270 */ 271 static int iucv_sock_in_state(struct sock *sk, int state, int state2) 272 { 273 return (sk->sk_state == state || sk->sk_state == state2); 274 } 275 276 /** 277 * iucv_below_msglim() - function to check if messages can be sent 278 * @sk: sock structure 279 * 280 * Returns true if the send queue length is lower than the message limit. 281 * Always returns true if the socket is not connected (no iucv path for 282 * checking the message limit). 283 */ 284 static inline int iucv_below_msglim(struct sock *sk) 285 { 286 struct iucv_sock *iucv = iucv_sk(sk); 287 288 if (sk->sk_state != IUCV_CONNECTED) 289 return 1; 290 if (iucv->transport == AF_IUCV_TRANS_IUCV) 291 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim); 292 else 293 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) && 294 (atomic_read(&iucv->pendings) <= 0)); 295 } 296 297 /** 298 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit 299 */ 300 static void iucv_sock_wake_msglim(struct sock *sk) 301 { 302 struct socket_wq *wq; 303 304 rcu_read_lock(); 305 wq = rcu_dereference(sk->sk_wq); 306 if (skwq_has_sleeper(wq)) 307 wake_up_interruptible_all(&wq->wait); 308 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 309 rcu_read_unlock(); 310 } 311 312 /** 313 * afiucv_hs_send() - send a message through HiperSockets transport 314 */ 315 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, 316 struct sk_buff *skb, u8 flags) 317 { 318 struct iucv_sock *iucv = iucv_sk(sock); 319 struct af_iucv_trans_hdr *phs_hdr; 320 struct sk_buff *nskb; 321 int err, confirm_recv = 0; 322 323 memset(skb->head, 0, ETH_HLEN); 324 phs_hdr = skb_push(skb, sizeof(struct af_iucv_trans_hdr)); 325 skb_reset_mac_header(skb); 326 skb_reset_network_header(skb); 327 skb_push(skb, ETH_HLEN); 328 skb_reset_mac_header(skb); 329 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr)); 330 331 phs_hdr->magic = ETH_P_AF_IUCV; 332 phs_hdr->version = 1; 333 phs_hdr->flags = flags; 334 if (flags == AF_IUCV_FLAG_SYN) 335 phs_hdr->window = iucv->msglimit; 336 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) { 337 confirm_recv = atomic_read(&iucv->msg_recv); 338 phs_hdr->window = confirm_recv; 339 if (confirm_recv) 340 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN; 341 } 342 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8); 343 memcpy(phs_hdr->destAppName, iucv->dst_name, 8); 344 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8); 345 memcpy(phs_hdr->srcAppName, iucv->src_name, 8); 346 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID)); 347 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName)); 348 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID)); 349 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName)); 350 if (imsg) 351 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message)); 352 353 skb->dev = iucv->hs_dev; 354 if (!skb->dev) 355 return -ENODEV; 356 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) 357 return -ENETDOWN; 358 if (skb->len > skb->dev->mtu) { 359 if (sock->sk_type == SOCK_SEQPACKET) 360 return -EMSGSIZE; 361 else 362 skb_trim(skb, skb->dev->mtu); 363 } 364 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV); 365 nskb = skb_clone(skb, GFP_ATOMIC); 366 if (!nskb) 367 return -ENOMEM; 368 skb_queue_tail(&iucv->send_skb_q, nskb); 369 err = dev_queue_xmit(skb); 370 if (net_xmit_eval(err)) { 371 skb_unlink(nskb, &iucv->send_skb_q); 372 kfree_skb(nskb); 373 } else { 374 atomic_sub(confirm_recv, &iucv->msg_recv); 375 WARN_ON(atomic_read(&iucv->msg_recv) < 0); 376 } 377 return net_xmit_eval(err); 378 } 379 380 static struct sock *__iucv_get_sock_by_name(char *nm) 381 { 382 struct sock *sk; 383 384 sk_for_each(sk, &iucv_sk_list.head) 385 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8)) 386 return sk; 387 388 return NULL; 389 } 390 391 static void iucv_sock_destruct(struct sock *sk) 392 { 393 skb_queue_purge(&sk->sk_receive_queue); 394 skb_queue_purge(&sk->sk_error_queue); 395 396 sk_mem_reclaim(sk); 397 398 if (!sock_flag(sk, SOCK_DEAD)) { 399 pr_err("Attempt to release alive iucv socket %p\n", sk); 400 return; 401 } 402 403 WARN_ON(atomic_read(&sk->sk_rmem_alloc)); 404 WARN_ON(refcount_read(&sk->sk_wmem_alloc)); 405 WARN_ON(sk->sk_wmem_queued); 406 WARN_ON(sk->sk_forward_alloc); 407 } 408 409 /* Cleanup Listen */ 410 static void iucv_sock_cleanup_listen(struct sock *parent) 411 { 412 struct sock *sk; 413 414 /* Close non-accepted connections */ 415 while ((sk = iucv_accept_dequeue(parent, NULL))) { 416 iucv_sock_close(sk); 417 iucv_sock_kill(sk); 418 } 419 420 parent->sk_state = IUCV_CLOSED; 421 } 422 423 /* Kill socket (only if zapped and orphaned) */ 424 static void iucv_sock_kill(struct sock *sk) 425 { 426 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) 427 return; 428 429 iucv_sock_unlink(&iucv_sk_list, sk); 430 sock_set_flag(sk, SOCK_DEAD); 431 sock_put(sk); 432 } 433 434 /* Terminate an IUCV path */ 435 static void iucv_sever_path(struct sock *sk, int with_user_data) 436 { 437 unsigned char user_data[16]; 438 struct iucv_sock *iucv = iucv_sk(sk); 439 struct iucv_path *path = iucv->path; 440 441 if (iucv->path) { 442 iucv->path = NULL; 443 if (with_user_data) { 444 low_nmcpy(user_data, iucv->src_name); 445 high_nmcpy(user_data, iucv->dst_name); 446 ASCEBC(user_data, sizeof(user_data)); 447 pr_iucv->path_sever(path, user_data); 448 } else 449 pr_iucv->path_sever(path, NULL); 450 iucv_path_free(path); 451 } 452 } 453 454 /* Send controlling flags through an IUCV socket for HIPER transport */ 455 static int iucv_send_ctrl(struct sock *sk, u8 flags) 456 { 457 int err = 0; 458 int blen; 459 struct sk_buff *skb; 460 u8 shutdown = 0; 461 462 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN; 463 if (sk->sk_shutdown & SEND_SHUTDOWN) { 464 /* controlling flags should be sent anyway */ 465 shutdown = sk->sk_shutdown; 466 sk->sk_shutdown &= RCV_SHUTDOWN; 467 } 468 skb = sock_alloc_send_skb(sk, blen, 1, &err); 469 if (skb) { 470 skb_reserve(skb, blen); 471 err = afiucv_hs_send(NULL, sk, skb, flags); 472 } 473 if (shutdown) 474 sk->sk_shutdown = shutdown; 475 return err; 476 } 477 478 /* Close an IUCV socket */ 479 static void iucv_sock_close(struct sock *sk) 480 { 481 struct iucv_sock *iucv = iucv_sk(sk); 482 unsigned long timeo; 483 int err = 0; 484 485 lock_sock(sk); 486 487 switch (sk->sk_state) { 488 case IUCV_LISTEN: 489 iucv_sock_cleanup_listen(sk); 490 break; 491 492 case IUCV_CONNECTED: 493 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 494 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN); 495 sk->sk_state = IUCV_DISCONN; 496 sk->sk_state_change(sk); 497 } 498 case IUCV_DISCONN: /* fall through */ 499 sk->sk_state = IUCV_CLOSING; 500 sk->sk_state_change(sk); 501 502 if (!err && !skb_queue_empty(&iucv->send_skb_q)) { 503 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) 504 timeo = sk->sk_lingertime; 505 else 506 timeo = IUCV_DISCONN_TIMEOUT; 507 iucv_sock_wait(sk, 508 iucv_sock_in_state(sk, IUCV_CLOSED, 0), 509 timeo); 510 } 511 512 case IUCV_CLOSING: /* fall through */ 513 sk->sk_state = IUCV_CLOSED; 514 sk->sk_state_change(sk); 515 516 sk->sk_err = ECONNRESET; 517 sk->sk_state_change(sk); 518 519 skb_queue_purge(&iucv->send_skb_q); 520 skb_queue_purge(&iucv->backlog_skb_q); 521 522 default: /* fall through */ 523 iucv_sever_path(sk, 1); 524 } 525 526 if (iucv->hs_dev) { 527 dev_put(iucv->hs_dev); 528 iucv->hs_dev = NULL; 529 sk->sk_bound_dev_if = 0; 530 } 531 532 /* mark socket for deletion by iucv_sock_kill() */ 533 sock_set_flag(sk, SOCK_ZAPPED); 534 535 release_sock(sk); 536 } 537 538 static void iucv_sock_init(struct sock *sk, struct sock *parent) 539 { 540 if (parent) { 541 sk->sk_type = parent->sk_type; 542 security_sk_clone(parent, sk); 543 } 544 } 545 546 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern) 547 { 548 struct sock *sk; 549 struct iucv_sock *iucv; 550 551 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern); 552 if (!sk) 553 return NULL; 554 iucv = iucv_sk(sk); 555 556 sock_init_data(sock, sk); 557 INIT_LIST_HEAD(&iucv->accept_q); 558 spin_lock_init(&iucv->accept_q_lock); 559 skb_queue_head_init(&iucv->send_skb_q); 560 INIT_LIST_HEAD(&iucv->message_q.list); 561 spin_lock_init(&iucv->message_q.lock); 562 skb_queue_head_init(&iucv->backlog_skb_q); 563 iucv->send_tag = 0; 564 atomic_set(&iucv->pendings, 0); 565 iucv->flags = 0; 566 iucv->msglimit = 0; 567 atomic_set(&iucv->msg_sent, 0); 568 atomic_set(&iucv->msg_recv, 0); 569 iucv->path = NULL; 570 iucv->sk_txnotify = afiucv_hs_callback_txnotify; 571 memset(&iucv->src_user_id , 0, 32); 572 if (pr_iucv) 573 iucv->transport = AF_IUCV_TRANS_IUCV; 574 else 575 iucv->transport = AF_IUCV_TRANS_HIPER; 576 577 sk->sk_destruct = iucv_sock_destruct; 578 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT; 579 sk->sk_allocation = GFP_DMA; 580 581 sock_reset_flag(sk, SOCK_ZAPPED); 582 583 sk->sk_protocol = proto; 584 sk->sk_state = IUCV_OPEN; 585 586 iucv_sock_link(&iucv_sk_list, sk); 587 return sk; 588 } 589 590 /* Create an IUCV socket */ 591 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol, 592 int kern) 593 { 594 struct sock *sk; 595 596 if (protocol && protocol != PF_IUCV) 597 return -EPROTONOSUPPORT; 598 599 sock->state = SS_UNCONNECTED; 600 601 switch (sock->type) { 602 case SOCK_STREAM: 603 sock->ops = &iucv_sock_ops; 604 break; 605 case SOCK_SEQPACKET: 606 /* currently, proto ops can handle both sk types */ 607 sock->ops = &iucv_sock_ops; 608 break; 609 default: 610 return -ESOCKTNOSUPPORT; 611 } 612 613 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern); 614 if (!sk) 615 return -ENOMEM; 616 617 iucv_sock_init(sk, NULL); 618 619 return 0; 620 } 621 622 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk) 623 { 624 write_lock_bh(&l->lock); 625 sk_add_node(sk, &l->head); 626 write_unlock_bh(&l->lock); 627 } 628 629 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk) 630 { 631 write_lock_bh(&l->lock); 632 sk_del_node_init(sk); 633 write_unlock_bh(&l->lock); 634 } 635 636 void iucv_accept_enqueue(struct sock *parent, struct sock *sk) 637 { 638 unsigned long flags; 639 struct iucv_sock *par = iucv_sk(parent); 640 641 sock_hold(sk); 642 spin_lock_irqsave(&par->accept_q_lock, flags); 643 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q); 644 spin_unlock_irqrestore(&par->accept_q_lock, flags); 645 iucv_sk(sk)->parent = parent; 646 sk_acceptq_added(parent); 647 } 648 649 void iucv_accept_unlink(struct sock *sk) 650 { 651 unsigned long flags; 652 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent); 653 654 spin_lock_irqsave(&par->accept_q_lock, flags); 655 list_del_init(&iucv_sk(sk)->accept_q); 656 spin_unlock_irqrestore(&par->accept_q_lock, flags); 657 sk_acceptq_removed(iucv_sk(sk)->parent); 658 iucv_sk(sk)->parent = NULL; 659 sock_put(sk); 660 } 661 662 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock) 663 { 664 struct iucv_sock *isk, *n; 665 struct sock *sk; 666 667 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { 668 sk = (struct sock *) isk; 669 lock_sock(sk); 670 671 if (sk->sk_state == IUCV_CLOSED) { 672 iucv_accept_unlink(sk); 673 release_sock(sk); 674 continue; 675 } 676 677 if (sk->sk_state == IUCV_CONNECTED || 678 sk->sk_state == IUCV_DISCONN || 679 !newsock) { 680 iucv_accept_unlink(sk); 681 if (newsock) 682 sock_graft(sk, newsock); 683 684 release_sock(sk); 685 return sk; 686 } 687 688 release_sock(sk); 689 } 690 return NULL; 691 } 692 693 static void __iucv_auto_name(struct iucv_sock *iucv) 694 { 695 char name[12]; 696 697 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name)); 698 while (__iucv_get_sock_by_name(name)) { 699 sprintf(name, "%08x", 700 atomic_inc_return(&iucv_sk_list.autobind_name)); 701 } 702 memcpy(iucv->src_name, name, 8); 703 } 704 705 /* Bind an unbound socket */ 706 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr, 707 int addr_len) 708 { 709 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 710 struct sock *sk = sock->sk; 711 struct iucv_sock *iucv; 712 int err = 0; 713 struct net_device *dev; 714 char uid[9]; 715 716 /* Verify the input sockaddr */ 717 if (addr_len < sizeof(struct sockaddr_iucv) || 718 addr->sa_family != AF_IUCV) 719 return -EINVAL; 720 721 lock_sock(sk); 722 if (sk->sk_state != IUCV_OPEN) { 723 err = -EBADFD; 724 goto done; 725 } 726 727 write_lock_bh(&iucv_sk_list.lock); 728 729 iucv = iucv_sk(sk); 730 if (__iucv_get_sock_by_name(sa->siucv_name)) { 731 err = -EADDRINUSE; 732 goto done_unlock; 733 } 734 if (iucv->path) 735 goto done_unlock; 736 737 /* Bind the socket */ 738 if (pr_iucv) 739 if (!memcmp(sa->siucv_user_id, iucv_userid, 8)) 740 goto vm_bind; /* VM IUCV transport */ 741 742 /* try hiper transport */ 743 memcpy(uid, sa->siucv_user_id, sizeof(uid)); 744 ASCEBC(uid, 8); 745 rcu_read_lock(); 746 for_each_netdev_rcu(&init_net, dev) { 747 if (!memcmp(dev->perm_addr, uid, 8)) { 748 memcpy(iucv->src_user_id, sa->siucv_user_id, 8); 749 /* Check for unitialized siucv_name */ 750 if (strncmp(sa->siucv_name, " ", 8) == 0) 751 __iucv_auto_name(iucv); 752 else 753 memcpy(iucv->src_name, sa->siucv_name, 8); 754 sk->sk_bound_dev_if = dev->ifindex; 755 iucv->hs_dev = dev; 756 dev_hold(dev); 757 sk->sk_state = IUCV_BOUND; 758 iucv->transport = AF_IUCV_TRANS_HIPER; 759 if (!iucv->msglimit) 760 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT; 761 rcu_read_unlock(); 762 goto done_unlock; 763 } 764 } 765 rcu_read_unlock(); 766 vm_bind: 767 if (pr_iucv) { 768 /* use local userid for backward compat */ 769 memcpy(iucv->src_name, sa->siucv_name, 8); 770 memcpy(iucv->src_user_id, iucv_userid, 8); 771 sk->sk_state = IUCV_BOUND; 772 iucv->transport = AF_IUCV_TRANS_IUCV; 773 if (!iucv->msglimit) 774 iucv->msglimit = IUCV_QUEUELEN_DEFAULT; 775 goto done_unlock; 776 } 777 /* found no dev to bind */ 778 err = -ENODEV; 779 done_unlock: 780 /* Release the socket list lock */ 781 write_unlock_bh(&iucv_sk_list.lock); 782 done: 783 release_sock(sk); 784 return err; 785 } 786 787 /* Automatically bind an unbound socket */ 788 static int iucv_sock_autobind(struct sock *sk) 789 { 790 struct iucv_sock *iucv = iucv_sk(sk); 791 int err = 0; 792 793 if (unlikely(!pr_iucv)) 794 return -EPROTO; 795 796 memcpy(iucv->src_user_id, iucv_userid, 8); 797 798 write_lock_bh(&iucv_sk_list.lock); 799 __iucv_auto_name(iucv); 800 write_unlock_bh(&iucv_sk_list.lock); 801 802 if (!iucv->msglimit) 803 iucv->msglimit = IUCV_QUEUELEN_DEFAULT; 804 805 return err; 806 } 807 808 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr) 809 { 810 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 811 struct sock *sk = sock->sk; 812 struct iucv_sock *iucv = iucv_sk(sk); 813 unsigned char user_data[16]; 814 int err; 815 816 high_nmcpy(user_data, sa->siucv_name); 817 low_nmcpy(user_data, iucv->src_name); 818 ASCEBC(user_data, sizeof(user_data)); 819 820 /* Create path. */ 821 iucv->path = iucv_path_alloc(iucv->msglimit, 822 IUCV_IPRMDATA, GFP_KERNEL); 823 if (!iucv->path) { 824 err = -ENOMEM; 825 goto done; 826 } 827 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler, 828 sa->siucv_user_id, NULL, user_data, 829 sk); 830 if (err) { 831 iucv_path_free(iucv->path); 832 iucv->path = NULL; 833 switch (err) { 834 case 0x0b: /* Target communicator is not logged on */ 835 err = -ENETUNREACH; 836 break; 837 case 0x0d: /* Max connections for this guest exceeded */ 838 case 0x0e: /* Max connections for target guest exceeded */ 839 err = -EAGAIN; 840 break; 841 case 0x0f: /* Missing IUCV authorization */ 842 err = -EACCES; 843 break; 844 default: 845 err = -ECONNREFUSED; 846 break; 847 } 848 } 849 done: 850 return err; 851 } 852 853 /* Connect an unconnected socket */ 854 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr, 855 int alen, int flags) 856 { 857 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 858 struct sock *sk = sock->sk; 859 struct iucv_sock *iucv = iucv_sk(sk); 860 int err; 861 862 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV) 863 return -EINVAL; 864 865 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND) 866 return -EBADFD; 867 868 if (sk->sk_state == IUCV_OPEN && 869 iucv->transport == AF_IUCV_TRANS_HIPER) 870 return -EBADFD; /* explicit bind required */ 871 872 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET) 873 return -EINVAL; 874 875 if (sk->sk_state == IUCV_OPEN) { 876 err = iucv_sock_autobind(sk); 877 if (unlikely(err)) 878 return err; 879 } 880 881 lock_sock(sk); 882 883 /* Set the destination information */ 884 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8); 885 memcpy(iucv->dst_name, sa->siucv_name, 8); 886 887 if (iucv->transport == AF_IUCV_TRANS_HIPER) 888 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN); 889 else 890 err = afiucv_path_connect(sock, addr); 891 if (err) 892 goto done; 893 894 if (sk->sk_state != IUCV_CONNECTED) 895 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED, 896 IUCV_DISCONN), 897 sock_sndtimeo(sk, flags & O_NONBLOCK)); 898 899 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED) 900 err = -ECONNREFUSED; 901 902 if (err && iucv->transport == AF_IUCV_TRANS_IUCV) 903 iucv_sever_path(sk, 0); 904 905 done: 906 release_sock(sk); 907 return err; 908 } 909 910 /* Move a socket into listening state. */ 911 static int iucv_sock_listen(struct socket *sock, int backlog) 912 { 913 struct sock *sk = sock->sk; 914 int err; 915 916 lock_sock(sk); 917 918 err = -EINVAL; 919 if (sk->sk_state != IUCV_BOUND) 920 goto done; 921 922 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) 923 goto done; 924 925 sk->sk_max_ack_backlog = backlog; 926 sk->sk_ack_backlog = 0; 927 sk->sk_state = IUCV_LISTEN; 928 err = 0; 929 930 done: 931 release_sock(sk); 932 return err; 933 } 934 935 /* Accept a pending connection */ 936 static int iucv_sock_accept(struct socket *sock, struct socket *newsock, 937 int flags, bool kern) 938 { 939 DECLARE_WAITQUEUE(wait, current); 940 struct sock *sk = sock->sk, *nsk; 941 long timeo; 942 int err = 0; 943 944 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 945 946 if (sk->sk_state != IUCV_LISTEN) { 947 err = -EBADFD; 948 goto done; 949 } 950 951 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 952 953 /* Wait for an incoming connection */ 954 add_wait_queue_exclusive(sk_sleep(sk), &wait); 955 while (!(nsk = iucv_accept_dequeue(sk, newsock))) { 956 set_current_state(TASK_INTERRUPTIBLE); 957 if (!timeo) { 958 err = -EAGAIN; 959 break; 960 } 961 962 release_sock(sk); 963 timeo = schedule_timeout(timeo); 964 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 965 966 if (sk->sk_state != IUCV_LISTEN) { 967 err = -EBADFD; 968 break; 969 } 970 971 if (signal_pending(current)) { 972 err = sock_intr_errno(timeo); 973 break; 974 } 975 } 976 977 set_current_state(TASK_RUNNING); 978 remove_wait_queue(sk_sleep(sk), &wait); 979 980 if (err) 981 goto done; 982 983 newsock->state = SS_CONNECTED; 984 985 done: 986 release_sock(sk); 987 return err; 988 } 989 990 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr, 991 int peer) 992 { 993 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr; 994 struct sock *sk = sock->sk; 995 struct iucv_sock *iucv = iucv_sk(sk); 996 997 addr->sa_family = AF_IUCV; 998 999 if (peer) { 1000 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8); 1001 memcpy(siucv->siucv_name, iucv->dst_name, 8); 1002 } else { 1003 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8); 1004 memcpy(siucv->siucv_name, iucv->src_name, 8); 1005 } 1006 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port)); 1007 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr)); 1008 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid)); 1009 1010 return sizeof(struct sockaddr_iucv); 1011 } 1012 1013 /** 1014 * iucv_send_iprm() - Send socket data in parameter list of an iucv message. 1015 * @path: IUCV path 1016 * @msg: Pointer to a struct iucv_message 1017 * @skb: The socket data to send, skb->len MUST BE <= 7 1018 * 1019 * Send the socket data in the parameter list in the iucv message 1020 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter 1021 * list and the socket data len at index 7 (last byte). 1022 * See also iucv_msg_length(). 1023 * 1024 * Returns the error code from the iucv_message_send() call. 1025 */ 1026 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg, 1027 struct sk_buff *skb) 1028 { 1029 u8 prmdata[8]; 1030 1031 memcpy(prmdata, (void *) skb->data, skb->len); 1032 prmdata[7] = 0xff - (u8) skb->len; 1033 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0, 1034 (void *) prmdata, 8); 1035 } 1036 1037 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg, 1038 size_t len) 1039 { 1040 struct sock *sk = sock->sk; 1041 struct iucv_sock *iucv = iucv_sk(sk); 1042 size_t headroom = 0; 1043 size_t linear; 1044 struct sk_buff *skb; 1045 struct iucv_message txmsg = {0}; 1046 struct cmsghdr *cmsg; 1047 int cmsg_done; 1048 long timeo; 1049 char user_id[9]; 1050 char appl_id[9]; 1051 int err; 1052 int noblock = msg->msg_flags & MSG_DONTWAIT; 1053 1054 err = sock_error(sk); 1055 if (err) 1056 return err; 1057 1058 if (msg->msg_flags & MSG_OOB) 1059 return -EOPNOTSUPP; 1060 1061 /* SOCK_SEQPACKET: we do not support segmented records */ 1062 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR)) 1063 return -EOPNOTSUPP; 1064 1065 lock_sock(sk); 1066 1067 if (sk->sk_shutdown & SEND_SHUTDOWN) { 1068 err = -EPIPE; 1069 goto out; 1070 } 1071 1072 /* Return if the socket is not in connected state */ 1073 if (sk->sk_state != IUCV_CONNECTED) { 1074 err = -ENOTCONN; 1075 goto out; 1076 } 1077 1078 /* initialize defaults */ 1079 cmsg_done = 0; /* check for duplicate headers */ 1080 txmsg.class = 0; 1081 1082 /* iterate over control messages */ 1083 for_each_cmsghdr(cmsg, msg) { 1084 if (!CMSG_OK(msg, cmsg)) { 1085 err = -EINVAL; 1086 goto out; 1087 } 1088 1089 if (cmsg->cmsg_level != SOL_IUCV) 1090 continue; 1091 1092 if (cmsg->cmsg_type & cmsg_done) { 1093 err = -EINVAL; 1094 goto out; 1095 } 1096 cmsg_done |= cmsg->cmsg_type; 1097 1098 switch (cmsg->cmsg_type) { 1099 case SCM_IUCV_TRGCLS: 1100 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) { 1101 err = -EINVAL; 1102 goto out; 1103 } 1104 1105 /* set iucv message target class */ 1106 memcpy(&txmsg.class, 1107 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE); 1108 1109 break; 1110 1111 default: 1112 err = -EINVAL; 1113 goto out; 1114 } 1115 } 1116 1117 /* allocate one skb for each iucv message: 1118 * this is fine for SOCK_SEQPACKET (unless we want to support 1119 * segmented records using the MSG_EOR flag), but 1120 * for SOCK_STREAM we might want to improve it in future */ 1121 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1122 headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN; 1123 linear = len; 1124 } else { 1125 if (len < PAGE_SIZE) { 1126 linear = len; 1127 } else { 1128 /* In nonlinear "classic" iucv skb, 1129 * reserve space for iucv_array 1130 */ 1131 headroom = sizeof(struct iucv_array) * 1132 (MAX_SKB_FRAGS + 1); 1133 linear = PAGE_SIZE - headroom; 1134 } 1135 } 1136 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear, 1137 noblock, &err, 0); 1138 if (!skb) 1139 goto out; 1140 if (headroom) 1141 skb_reserve(skb, headroom); 1142 skb_put(skb, linear); 1143 skb->len = len; 1144 skb->data_len = len - linear; 1145 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len); 1146 if (err) 1147 goto fail; 1148 1149 /* wait if outstanding messages for iucv path has reached */ 1150 timeo = sock_sndtimeo(sk, noblock); 1151 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo); 1152 if (err) 1153 goto fail; 1154 1155 /* return -ECONNRESET if the socket is no longer connected */ 1156 if (sk->sk_state != IUCV_CONNECTED) { 1157 err = -ECONNRESET; 1158 goto fail; 1159 } 1160 1161 /* increment and save iucv message tag for msg_completion cbk */ 1162 txmsg.tag = iucv->send_tag++; 1163 IUCV_SKB_CB(skb)->tag = txmsg.tag; 1164 1165 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1166 atomic_inc(&iucv->msg_sent); 1167 err = afiucv_hs_send(&txmsg, sk, skb, 0); 1168 if (err) { 1169 atomic_dec(&iucv->msg_sent); 1170 goto fail; 1171 } 1172 } else { /* Classic VM IUCV transport */ 1173 skb_queue_tail(&iucv->send_skb_q, skb); 1174 1175 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) && 1176 skb->len <= 7) { 1177 err = iucv_send_iprm(iucv->path, &txmsg, skb); 1178 1179 /* on success: there is no message_complete callback */ 1180 /* for an IPRMDATA msg; remove skb from send queue */ 1181 if (err == 0) { 1182 skb_unlink(skb, &iucv->send_skb_q); 1183 kfree_skb(skb); 1184 } 1185 1186 /* this error should never happen since the */ 1187 /* IUCV_IPRMDATA path flag is set... sever path */ 1188 if (err == 0x15) { 1189 pr_iucv->path_sever(iucv->path, NULL); 1190 skb_unlink(skb, &iucv->send_skb_q); 1191 err = -EPIPE; 1192 goto fail; 1193 } 1194 } else if (skb_is_nonlinear(skb)) { 1195 struct iucv_array *iba = (struct iucv_array *)skb->head; 1196 int i; 1197 1198 /* skip iucv_array lying in the headroom */ 1199 iba[0].address = (u32)(addr_t)skb->data; 1200 iba[0].length = (u32)skb_headlen(skb); 1201 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1202 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1203 1204 iba[i + 1].address = 1205 (u32)(addr_t)skb_frag_address(frag); 1206 iba[i + 1].length = (u32)skb_frag_size(frag); 1207 } 1208 err = pr_iucv->message_send(iucv->path, &txmsg, 1209 IUCV_IPBUFLST, 0, 1210 (void *)iba, skb->len); 1211 } else { /* non-IPRM Linear skb */ 1212 err = pr_iucv->message_send(iucv->path, &txmsg, 1213 0, 0, (void *)skb->data, skb->len); 1214 } 1215 if (err) { 1216 if (err == 3) { 1217 user_id[8] = 0; 1218 memcpy(user_id, iucv->dst_user_id, 8); 1219 appl_id[8] = 0; 1220 memcpy(appl_id, iucv->dst_name, 8); 1221 pr_err( 1222 "Application %s on z/VM guest %s exceeds message limit\n", 1223 appl_id, user_id); 1224 err = -EAGAIN; 1225 } else { 1226 err = -EPIPE; 1227 } 1228 skb_unlink(skb, &iucv->send_skb_q); 1229 goto fail; 1230 } 1231 } 1232 1233 release_sock(sk); 1234 return len; 1235 1236 fail: 1237 kfree_skb(skb); 1238 out: 1239 release_sock(sk); 1240 return err; 1241 } 1242 1243 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len) 1244 { 1245 size_t headroom, linear; 1246 struct sk_buff *skb; 1247 int err; 1248 1249 if (len < PAGE_SIZE) { 1250 headroom = 0; 1251 linear = len; 1252 } else { 1253 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1); 1254 linear = PAGE_SIZE - headroom; 1255 } 1256 skb = alloc_skb_with_frags(headroom + linear, len - linear, 1257 0, &err, GFP_ATOMIC | GFP_DMA); 1258 WARN_ONCE(!skb, 1259 "alloc of recv iucv skb len=%lu failed with errcode=%d\n", 1260 len, err); 1261 if (skb) { 1262 if (headroom) 1263 skb_reserve(skb, headroom); 1264 skb_put(skb, linear); 1265 skb->len = len; 1266 skb->data_len = len - linear; 1267 } 1268 return skb; 1269 } 1270 1271 /* iucv_process_message() - Receive a single outstanding IUCV message 1272 * 1273 * Locking: must be called with message_q.lock held 1274 */ 1275 static void iucv_process_message(struct sock *sk, struct sk_buff *skb, 1276 struct iucv_path *path, 1277 struct iucv_message *msg) 1278 { 1279 int rc; 1280 unsigned int len; 1281 1282 len = iucv_msg_length(msg); 1283 1284 /* store msg target class in the second 4 bytes of skb ctrl buffer */ 1285 /* Note: the first 4 bytes are reserved for msg tag */ 1286 IUCV_SKB_CB(skb)->class = msg->class; 1287 1288 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */ 1289 if ((msg->flags & IUCV_IPRMDATA) && len > 7) { 1290 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) { 1291 skb->data = NULL; 1292 skb->len = 0; 1293 } 1294 } else { 1295 if (skb_is_nonlinear(skb)) { 1296 struct iucv_array *iba = (struct iucv_array *)skb->head; 1297 int i; 1298 1299 iba[0].address = (u32)(addr_t)skb->data; 1300 iba[0].length = (u32)skb_headlen(skb); 1301 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1302 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1303 1304 iba[i + 1].address = 1305 (u32)(addr_t)skb_frag_address(frag); 1306 iba[i + 1].length = (u32)skb_frag_size(frag); 1307 } 1308 rc = pr_iucv->message_receive(path, msg, 1309 IUCV_IPBUFLST, 1310 (void *)iba, len, NULL); 1311 } else { 1312 rc = pr_iucv->message_receive(path, msg, 1313 msg->flags & IUCV_IPRMDATA, 1314 skb->data, len, NULL); 1315 } 1316 if (rc) { 1317 kfree_skb(skb); 1318 return; 1319 } 1320 WARN_ON_ONCE(skb->len != len); 1321 } 1322 1323 IUCV_SKB_CB(skb)->offset = 0; 1324 if (sk_filter(sk, skb)) { 1325 atomic_inc(&sk->sk_drops); /* skb rejected by filter */ 1326 kfree_skb(skb); 1327 return; 1328 } 1329 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */ 1330 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb); 1331 } 1332 1333 /* iucv_process_message_q() - Process outstanding IUCV messages 1334 * 1335 * Locking: must be called with message_q.lock held 1336 */ 1337 static void iucv_process_message_q(struct sock *sk) 1338 { 1339 struct iucv_sock *iucv = iucv_sk(sk); 1340 struct sk_buff *skb; 1341 struct sock_msg_q *p, *n; 1342 1343 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) { 1344 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg)); 1345 if (!skb) 1346 break; 1347 iucv_process_message(sk, skb, p->path, &p->msg); 1348 list_del(&p->list); 1349 kfree(p); 1350 if (!skb_queue_empty(&iucv->backlog_skb_q)) 1351 break; 1352 } 1353 } 1354 1355 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, 1356 size_t len, int flags) 1357 { 1358 int noblock = flags & MSG_DONTWAIT; 1359 struct sock *sk = sock->sk; 1360 struct iucv_sock *iucv = iucv_sk(sk); 1361 unsigned int copied, rlen; 1362 struct sk_buff *skb, *rskb, *cskb; 1363 int err = 0; 1364 u32 offset; 1365 1366 if ((sk->sk_state == IUCV_DISCONN) && 1367 skb_queue_empty(&iucv->backlog_skb_q) && 1368 skb_queue_empty(&sk->sk_receive_queue) && 1369 list_empty(&iucv->message_q.list)) 1370 return 0; 1371 1372 if (flags & (MSG_OOB)) 1373 return -EOPNOTSUPP; 1374 1375 /* receive/dequeue next skb: 1376 * the function understands MSG_PEEK and, thus, does not dequeue skb */ 1377 skb = skb_recv_datagram(sk, flags, noblock, &err); 1378 if (!skb) { 1379 if (sk->sk_shutdown & RCV_SHUTDOWN) 1380 return 0; 1381 return err; 1382 } 1383 1384 offset = IUCV_SKB_CB(skb)->offset; 1385 rlen = skb->len - offset; /* real length of skb */ 1386 copied = min_t(unsigned int, rlen, len); 1387 if (!rlen) 1388 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN; 1389 1390 cskb = skb; 1391 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) { 1392 if (!(flags & MSG_PEEK)) 1393 skb_queue_head(&sk->sk_receive_queue, skb); 1394 return -EFAULT; 1395 } 1396 1397 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */ 1398 if (sk->sk_type == SOCK_SEQPACKET) { 1399 if (copied < rlen) 1400 msg->msg_flags |= MSG_TRUNC; 1401 /* each iucv message contains a complete record */ 1402 msg->msg_flags |= MSG_EOR; 1403 } 1404 1405 /* create control message to store iucv msg target class: 1406 * get the trgcls from the control buffer of the skb due to 1407 * fragmentation of original iucv message. */ 1408 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS, 1409 sizeof(IUCV_SKB_CB(skb)->class), 1410 (void *)&IUCV_SKB_CB(skb)->class); 1411 if (err) { 1412 if (!(flags & MSG_PEEK)) 1413 skb_queue_head(&sk->sk_receive_queue, skb); 1414 return err; 1415 } 1416 1417 /* Mark read part of skb as used */ 1418 if (!(flags & MSG_PEEK)) { 1419 1420 /* SOCK_STREAM: re-queue skb if it contains unreceived data */ 1421 if (sk->sk_type == SOCK_STREAM) { 1422 if (copied < rlen) { 1423 IUCV_SKB_CB(skb)->offset = offset + copied; 1424 skb_queue_head(&sk->sk_receive_queue, skb); 1425 goto done; 1426 } 1427 } 1428 1429 kfree_skb(skb); 1430 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1431 atomic_inc(&iucv->msg_recv); 1432 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) { 1433 WARN_ON(1); 1434 iucv_sock_close(sk); 1435 return -EFAULT; 1436 } 1437 } 1438 1439 /* Queue backlog skbs */ 1440 spin_lock_bh(&iucv->message_q.lock); 1441 rskb = skb_dequeue(&iucv->backlog_skb_q); 1442 while (rskb) { 1443 IUCV_SKB_CB(rskb)->offset = 0; 1444 if (__sock_queue_rcv_skb(sk, rskb)) { 1445 /* handle rcv queue full */ 1446 skb_queue_head(&iucv->backlog_skb_q, 1447 rskb); 1448 break; 1449 } 1450 rskb = skb_dequeue(&iucv->backlog_skb_q); 1451 } 1452 if (skb_queue_empty(&iucv->backlog_skb_q)) { 1453 if (!list_empty(&iucv->message_q.list)) 1454 iucv_process_message_q(sk); 1455 if (atomic_read(&iucv->msg_recv) >= 1456 iucv->msglimit / 2) { 1457 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN); 1458 if (err) { 1459 sk->sk_state = IUCV_DISCONN; 1460 sk->sk_state_change(sk); 1461 } 1462 } 1463 } 1464 spin_unlock_bh(&iucv->message_q.lock); 1465 } 1466 1467 done: 1468 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ 1469 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) 1470 copied = rlen; 1471 1472 return copied; 1473 } 1474 1475 static inline __poll_t iucv_accept_poll(struct sock *parent) 1476 { 1477 struct iucv_sock *isk, *n; 1478 struct sock *sk; 1479 1480 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { 1481 sk = (struct sock *) isk; 1482 1483 if (sk->sk_state == IUCV_CONNECTED) 1484 return EPOLLIN | EPOLLRDNORM; 1485 } 1486 1487 return 0; 1488 } 1489 1490 __poll_t iucv_sock_poll(struct file *file, struct socket *sock, 1491 poll_table *wait) 1492 { 1493 struct sock *sk = sock->sk; 1494 __poll_t mask = 0; 1495 1496 sock_poll_wait(file, wait); 1497 1498 if (sk->sk_state == IUCV_LISTEN) 1499 return iucv_accept_poll(sk); 1500 1501 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) 1502 mask |= EPOLLERR | 1503 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 1504 1505 if (sk->sk_shutdown & RCV_SHUTDOWN) 1506 mask |= EPOLLRDHUP; 1507 1508 if (sk->sk_shutdown == SHUTDOWN_MASK) 1509 mask |= EPOLLHUP; 1510 1511 if (!skb_queue_empty(&sk->sk_receive_queue) || 1512 (sk->sk_shutdown & RCV_SHUTDOWN)) 1513 mask |= EPOLLIN | EPOLLRDNORM; 1514 1515 if (sk->sk_state == IUCV_CLOSED) 1516 mask |= EPOLLHUP; 1517 1518 if (sk->sk_state == IUCV_DISCONN) 1519 mask |= EPOLLIN; 1520 1521 if (sock_writeable(sk) && iucv_below_msglim(sk)) 1522 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 1523 else 1524 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1525 1526 return mask; 1527 } 1528 1529 static int iucv_sock_shutdown(struct socket *sock, int how) 1530 { 1531 struct sock *sk = sock->sk; 1532 struct iucv_sock *iucv = iucv_sk(sk); 1533 struct iucv_message txmsg; 1534 int err = 0; 1535 1536 how++; 1537 1538 if ((how & ~SHUTDOWN_MASK) || !how) 1539 return -EINVAL; 1540 1541 lock_sock(sk); 1542 switch (sk->sk_state) { 1543 case IUCV_LISTEN: 1544 case IUCV_DISCONN: 1545 case IUCV_CLOSING: 1546 case IUCV_CLOSED: 1547 err = -ENOTCONN; 1548 goto fail; 1549 default: 1550 break; 1551 } 1552 1553 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) { 1554 if (iucv->transport == AF_IUCV_TRANS_IUCV) { 1555 txmsg.class = 0; 1556 txmsg.tag = 0; 1557 err = pr_iucv->message_send(iucv->path, &txmsg, 1558 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8); 1559 if (err) { 1560 switch (err) { 1561 case 1: 1562 err = -ENOTCONN; 1563 break; 1564 case 2: 1565 err = -ECONNRESET; 1566 break; 1567 default: 1568 err = -ENOTCONN; 1569 break; 1570 } 1571 } 1572 } else 1573 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT); 1574 } 1575 1576 sk->sk_shutdown |= how; 1577 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) { 1578 if ((iucv->transport == AF_IUCV_TRANS_IUCV) && 1579 iucv->path) { 1580 err = pr_iucv->path_quiesce(iucv->path, NULL); 1581 if (err) 1582 err = -ENOTCONN; 1583 /* skb_queue_purge(&sk->sk_receive_queue); */ 1584 } 1585 skb_queue_purge(&sk->sk_receive_queue); 1586 } 1587 1588 /* Wake up anyone sleeping in poll */ 1589 sk->sk_state_change(sk); 1590 1591 fail: 1592 release_sock(sk); 1593 return err; 1594 } 1595 1596 static int iucv_sock_release(struct socket *sock) 1597 { 1598 struct sock *sk = sock->sk; 1599 int err = 0; 1600 1601 if (!sk) 1602 return 0; 1603 1604 iucv_sock_close(sk); 1605 1606 sock_orphan(sk); 1607 iucv_sock_kill(sk); 1608 return err; 1609 } 1610 1611 /* getsockopt and setsockopt */ 1612 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname, 1613 char __user *optval, unsigned int optlen) 1614 { 1615 struct sock *sk = sock->sk; 1616 struct iucv_sock *iucv = iucv_sk(sk); 1617 int val; 1618 int rc; 1619 1620 if (level != SOL_IUCV) 1621 return -ENOPROTOOPT; 1622 1623 if (optlen < sizeof(int)) 1624 return -EINVAL; 1625 1626 if (get_user(val, (int __user *) optval)) 1627 return -EFAULT; 1628 1629 rc = 0; 1630 1631 lock_sock(sk); 1632 switch (optname) { 1633 case SO_IPRMDATA_MSG: 1634 if (val) 1635 iucv->flags |= IUCV_IPRMDATA; 1636 else 1637 iucv->flags &= ~IUCV_IPRMDATA; 1638 break; 1639 case SO_MSGLIMIT: 1640 switch (sk->sk_state) { 1641 case IUCV_OPEN: 1642 case IUCV_BOUND: 1643 if (val < 1 || val > (u16)(~0)) 1644 rc = -EINVAL; 1645 else 1646 iucv->msglimit = val; 1647 break; 1648 default: 1649 rc = -EINVAL; 1650 break; 1651 } 1652 break; 1653 default: 1654 rc = -ENOPROTOOPT; 1655 break; 1656 } 1657 release_sock(sk); 1658 1659 return rc; 1660 } 1661 1662 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname, 1663 char __user *optval, int __user *optlen) 1664 { 1665 struct sock *sk = sock->sk; 1666 struct iucv_sock *iucv = iucv_sk(sk); 1667 unsigned int val; 1668 int len; 1669 1670 if (level != SOL_IUCV) 1671 return -ENOPROTOOPT; 1672 1673 if (get_user(len, optlen)) 1674 return -EFAULT; 1675 1676 if (len < 0) 1677 return -EINVAL; 1678 1679 len = min_t(unsigned int, len, sizeof(int)); 1680 1681 switch (optname) { 1682 case SO_IPRMDATA_MSG: 1683 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0; 1684 break; 1685 case SO_MSGLIMIT: 1686 lock_sock(sk); 1687 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */ 1688 : iucv->msglimit; /* default */ 1689 release_sock(sk); 1690 break; 1691 case SO_MSGSIZE: 1692 if (sk->sk_state == IUCV_OPEN) 1693 return -EBADFD; 1694 val = (iucv->hs_dev) ? iucv->hs_dev->mtu - 1695 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN : 1696 0x7fffffff; 1697 break; 1698 default: 1699 return -ENOPROTOOPT; 1700 } 1701 1702 if (put_user(len, optlen)) 1703 return -EFAULT; 1704 if (copy_to_user(optval, &val, len)) 1705 return -EFAULT; 1706 1707 return 0; 1708 } 1709 1710 1711 /* Callback wrappers - called from iucv base support */ 1712 static int iucv_callback_connreq(struct iucv_path *path, 1713 u8 ipvmid[8], u8 ipuser[16]) 1714 { 1715 unsigned char user_data[16]; 1716 unsigned char nuser_data[16]; 1717 unsigned char src_name[8]; 1718 struct sock *sk, *nsk; 1719 struct iucv_sock *iucv, *niucv; 1720 int err; 1721 1722 memcpy(src_name, ipuser, 8); 1723 EBCASC(src_name, 8); 1724 /* Find out if this path belongs to af_iucv. */ 1725 read_lock(&iucv_sk_list.lock); 1726 iucv = NULL; 1727 sk = NULL; 1728 sk_for_each(sk, &iucv_sk_list.head) 1729 if (sk->sk_state == IUCV_LISTEN && 1730 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) { 1731 /* 1732 * Found a listening socket with 1733 * src_name == ipuser[0-7]. 1734 */ 1735 iucv = iucv_sk(sk); 1736 break; 1737 } 1738 read_unlock(&iucv_sk_list.lock); 1739 if (!iucv) 1740 /* No socket found, not one of our paths. */ 1741 return -EINVAL; 1742 1743 bh_lock_sock(sk); 1744 1745 /* Check if parent socket is listening */ 1746 low_nmcpy(user_data, iucv->src_name); 1747 high_nmcpy(user_data, iucv->dst_name); 1748 ASCEBC(user_data, sizeof(user_data)); 1749 if (sk->sk_state != IUCV_LISTEN) { 1750 err = pr_iucv->path_sever(path, user_data); 1751 iucv_path_free(path); 1752 goto fail; 1753 } 1754 1755 /* Check for backlog size */ 1756 if (sk_acceptq_is_full(sk)) { 1757 err = pr_iucv->path_sever(path, user_data); 1758 iucv_path_free(path); 1759 goto fail; 1760 } 1761 1762 /* Create the new socket */ 1763 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); 1764 if (!nsk) { 1765 err = pr_iucv->path_sever(path, user_data); 1766 iucv_path_free(path); 1767 goto fail; 1768 } 1769 1770 niucv = iucv_sk(nsk); 1771 iucv_sock_init(nsk, sk); 1772 1773 /* Set the new iucv_sock */ 1774 memcpy(niucv->dst_name, ipuser + 8, 8); 1775 EBCASC(niucv->dst_name, 8); 1776 memcpy(niucv->dst_user_id, ipvmid, 8); 1777 memcpy(niucv->src_name, iucv->src_name, 8); 1778 memcpy(niucv->src_user_id, iucv->src_user_id, 8); 1779 niucv->path = path; 1780 1781 /* Call iucv_accept */ 1782 high_nmcpy(nuser_data, ipuser + 8); 1783 memcpy(nuser_data + 8, niucv->src_name, 8); 1784 ASCEBC(nuser_data + 8, 8); 1785 1786 /* set message limit for path based on msglimit of accepting socket */ 1787 niucv->msglimit = iucv->msglimit; 1788 path->msglim = iucv->msglimit; 1789 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk); 1790 if (err) { 1791 iucv_sever_path(nsk, 1); 1792 iucv_sock_kill(nsk); 1793 goto fail; 1794 } 1795 1796 iucv_accept_enqueue(sk, nsk); 1797 1798 /* Wake up accept */ 1799 nsk->sk_state = IUCV_CONNECTED; 1800 sk->sk_data_ready(sk); 1801 err = 0; 1802 fail: 1803 bh_unlock_sock(sk); 1804 return 0; 1805 } 1806 1807 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16]) 1808 { 1809 struct sock *sk = path->private; 1810 1811 sk->sk_state = IUCV_CONNECTED; 1812 sk->sk_state_change(sk); 1813 } 1814 1815 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg) 1816 { 1817 struct sock *sk = path->private; 1818 struct iucv_sock *iucv = iucv_sk(sk); 1819 struct sk_buff *skb; 1820 struct sock_msg_q *save_msg; 1821 int len; 1822 1823 if (sk->sk_shutdown & RCV_SHUTDOWN) { 1824 pr_iucv->message_reject(path, msg); 1825 return; 1826 } 1827 1828 spin_lock(&iucv->message_q.lock); 1829 1830 if (!list_empty(&iucv->message_q.list) || 1831 !skb_queue_empty(&iucv->backlog_skb_q)) 1832 goto save_message; 1833 1834 len = atomic_read(&sk->sk_rmem_alloc); 1835 len += SKB_TRUESIZE(iucv_msg_length(msg)); 1836 if (len > sk->sk_rcvbuf) 1837 goto save_message; 1838 1839 skb = alloc_iucv_recv_skb(iucv_msg_length(msg)); 1840 if (!skb) 1841 goto save_message; 1842 1843 iucv_process_message(sk, skb, path, msg); 1844 goto out_unlock; 1845 1846 save_message: 1847 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA); 1848 if (!save_msg) 1849 goto out_unlock; 1850 save_msg->path = path; 1851 save_msg->msg = *msg; 1852 1853 list_add_tail(&save_msg->list, &iucv->message_q.list); 1854 1855 out_unlock: 1856 spin_unlock(&iucv->message_q.lock); 1857 } 1858 1859 static void iucv_callback_txdone(struct iucv_path *path, 1860 struct iucv_message *msg) 1861 { 1862 struct sock *sk = path->private; 1863 struct sk_buff *this = NULL; 1864 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q; 1865 struct sk_buff *list_skb = list->next; 1866 unsigned long flags; 1867 1868 bh_lock_sock(sk); 1869 if (!skb_queue_empty(list)) { 1870 spin_lock_irqsave(&list->lock, flags); 1871 1872 while (list_skb != (struct sk_buff *)list) { 1873 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) { 1874 this = list_skb; 1875 break; 1876 } 1877 list_skb = list_skb->next; 1878 } 1879 if (this) 1880 __skb_unlink(this, list); 1881 1882 spin_unlock_irqrestore(&list->lock, flags); 1883 1884 if (this) { 1885 kfree_skb(this); 1886 /* wake up any process waiting for sending */ 1887 iucv_sock_wake_msglim(sk); 1888 } 1889 } 1890 1891 if (sk->sk_state == IUCV_CLOSING) { 1892 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { 1893 sk->sk_state = IUCV_CLOSED; 1894 sk->sk_state_change(sk); 1895 } 1896 } 1897 bh_unlock_sock(sk); 1898 1899 } 1900 1901 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16]) 1902 { 1903 struct sock *sk = path->private; 1904 1905 if (sk->sk_state == IUCV_CLOSED) 1906 return; 1907 1908 bh_lock_sock(sk); 1909 iucv_sever_path(sk, 1); 1910 sk->sk_state = IUCV_DISCONN; 1911 1912 sk->sk_state_change(sk); 1913 bh_unlock_sock(sk); 1914 } 1915 1916 /* called if the other communication side shuts down its RECV direction; 1917 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data. 1918 */ 1919 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16]) 1920 { 1921 struct sock *sk = path->private; 1922 1923 bh_lock_sock(sk); 1924 if (sk->sk_state != IUCV_CLOSED) { 1925 sk->sk_shutdown |= SEND_SHUTDOWN; 1926 sk->sk_state_change(sk); 1927 } 1928 bh_unlock_sock(sk); 1929 } 1930 1931 /***************** HiperSockets transport callbacks ********************/ 1932 static void afiucv_swap_src_dest(struct sk_buff *skb) 1933 { 1934 struct af_iucv_trans_hdr *trans_hdr = 1935 (struct af_iucv_trans_hdr *)skb->data; 1936 char tmpID[8]; 1937 char tmpName[8]; 1938 1939 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); 1940 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); 1941 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); 1942 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); 1943 memcpy(tmpID, trans_hdr->srcUserID, 8); 1944 memcpy(tmpName, trans_hdr->srcAppName, 8); 1945 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8); 1946 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8); 1947 memcpy(trans_hdr->destUserID, tmpID, 8); 1948 memcpy(trans_hdr->destAppName, tmpName, 8); 1949 skb_push(skb, ETH_HLEN); 1950 memset(skb->data, 0, ETH_HLEN); 1951 } 1952 1953 /** 1954 * afiucv_hs_callback_syn - react on received SYN 1955 **/ 1956 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb) 1957 { 1958 struct sock *nsk; 1959 struct iucv_sock *iucv, *niucv; 1960 struct af_iucv_trans_hdr *trans_hdr; 1961 int err; 1962 1963 iucv = iucv_sk(sk); 1964 trans_hdr = (struct af_iucv_trans_hdr *)skb->data; 1965 if (!iucv) { 1966 /* no sock - connection refused */ 1967 afiucv_swap_src_dest(skb); 1968 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; 1969 err = dev_queue_xmit(skb); 1970 goto out; 1971 } 1972 1973 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); 1974 bh_lock_sock(sk); 1975 if ((sk->sk_state != IUCV_LISTEN) || 1976 sk_acceptq_is_full(sk) || 1977 !nsk) { 1978 /* error on server socket - connection refused */ 1979 afiucv_swap_src_dest(skb); 1980 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; 1981 err = dev_queue_xmit(skb); 1982 iucv_sock_kill(nsk); 1983 bh_unlock_sock(sk); 1984 goto out; 1985 } 1986 1987 niucv = iucv_sk(nsk); 1988 iucv_sock_init(nsk, sk); 1989 niucv->transport = AF_IUCV_TRANS_HIPER; 1990 niucv->msglimit = iucv->msglimit; 1991 if (!trans_hdr->window) 1992 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT; 1993 else 1994 niucv->msglimit_peer = trans_hdr->window; 1995 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8); 1996 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8); 1997 memcpy(niucv->src_name, iucv->src_name, 8); 1998 memcpy(niucv->src_user_id, iucv->src_user_id, 8); 1999 nsk->sk_bound_dev_if = sk->sk_bound_dev_if; 2000 niucv->hs_dev = iucv->hs_dev; 2001 dev_hold(niucv->hs_dev); 2002 afiucv_swap_src_dest(skb); 2003 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK; 2004 trans_hdr->window = niucv->msglimit; 2005 /* if receiver acks the xmit connection is established */ 2006 err = dev_queue_xmit(skb); 2007 if (!err) { 2008 iucv_accept_enqueue(sk, nsk); 2009 nsk->sk_state = IUCV_CONNECTED; 2010 sk->sk_data_ready(sk); 2011 } else 2012 iucv_sock_kill(nsk); 2013 bh_unlock_sock(sk); 2014 2015 out: 2016 return NET_RX_SUCCESS; 2017 } 2018 2019 /** 2020 * afiucv_hs_callback_synack() - react on received SYN-ACK 2021 **/ 2022 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb) 2023 { 2024 struct iucv_sock *iucv = iucv_sk(sk); 2025 struct af_iucv_trans_hdr *trans_hdr = 2026 (struct af_iucv_trans_hdr *)skb->data; 2027 2028 if (!iucv) 2029 goto out; 2030 if (sk->sk_state != IUCV_BOUND) 2031 goto out; 2032 bh_lock_sock(sk); 2033 iucv->msglimit_peer = trans_hdr->window; 2034 sk->sk_state = IUCV_CONNECTED; 2035 sk->sk_state_change(sk); 2036 bh_unlock_sock(sk); 2037 out: 2038 kfree_skb(skb); 2039 return NET_RX_SUCCESS; 2040 } 2041 2042 /** 2043 * afiucv_hs_callback_synfin() - react on received SYN_FIN 2044 **/ 2045 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb) 2046 { 2047 struct iucv_sock *iucv = iucv_sk(sk); 2048 2049 if (!iucv) 2050 goto out; 2051 if (sk->sk_state != IUCV_BOUND) 2052 goto out; 2053 bh_lock_sock(sk); 2054 sk->sk_state = IUCV_DISCONN; 2055 sk->sk_state_change(sk); 2056 bh_unlock_sock(sk); 2057 out: 2058 kfree_skb(skb); 2059 return NET_RX_SUCCESS; 2060 } 2061 2062 /** 2063 * afiucv_hs_callback_fin() - react on received FIN 2064 **/ 2065 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb) 2066 { 2067 struct iucv_sock *iucv = iucv_sk(sk); 2068 2069 /* other end of connection closed */ 2070 if (!iucv) 2071 goto out; 2072 bh_lock_sock(sk); 2073 if (sk->sk_state == IUCV_CONNECTED) { 2074 sk->sk_state = IUCV_DISCONN; 2075 sk->sk_state_change(sk); 2076 } 2077 bh_unlock_sock(sk); 2078 out: 2079 kfree_skb(skb); 2080 return NET_RX_SUCCESS; 2081 } 2082 2083 /** 2084 * afiucv_hs_callback_win() - react on received WIN 2085 **/ 2086 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb) 2087 { 2088 struct iucv_sock *iucv = iucv_sk(sk); 2089 struct af_iucv_trans_hdr *trans_hdr = 2090 (struct af_iucv_trans_hdr *)skb->data; 2091 2092 if (!iucv) 2093 return NET_RX_SUCCESS; 2094 2095 if (sk->sk_state != IUCV_CONNECTED) 2096 return NET_RX_SUCCESS; 2097 2098 atomic_sub(trans_hdr->window, &iucv->msg_sent); 2099 iucv_sock_wake_msglim(sk); 2100 return NET_RX_SUCCESS; 2101 } 2102 2103 /** 2104 * afiucv_hs_callback_rx() - react on received data 2105 **/ 2106 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb) 2107 { 2108 struct iucv_sock *iucv = iucv_sk(sk); 2109 2110 if (!iucv) { 2111 kfree_skb(skb); 2112 return NET_RX_SUCCESS; 2113 } 2114 2115 if (sk->sk_state != IUCV_CONNECTED) { 2116 kfree_skb(skb); 2117 return NET_RX_SUCCESS; 2118 } 2119 2120 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2121 kfree_skb(skb); 2122 return NET_RX_SUCCESS; 2123 } 2124 2125 /* write stuff from iucv_msg to skb cb */ 2126 skb_pull(skb, sizeof(struct af_iucv_trans_hdr)); 2127 skb_reset_transport_header(skb); 2128 skb_reset_network_header(skb); 2129 IUCV_SKB_CB(skb)->offset = 0; 2130 if (sk_filter(sk, skb)) { 2131 atomic_inc(&sk->sk_drops); /* skb rejected by filter */ 2132 kfree_skb(skb); 2133 return NET_RX_SUCCESS; 2134 } 2135 2136 spin_lock(&iucv->message_q.lock); 2137 if (skb_queue_empty(&iucv->backlog_skb_q)) { 2138 if (__sock_queue_rcv_skb(sk, skb)) 2139 /* handle rcv queue full */ 2140 skb_queue_tail(&iucv->backlog_skb_q, skb); 2141 } else 2142 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb); 2143 spin_unlock(&iucv->message_q.lock); 2144 return NET_RX_SUCCESS; 2145 } 2146 2147 /** 2148 * afiucv_hs_rcv() - base function for arriving data through HiperSockets 2149 * transport 2150 * called from netif RX softirq 2151 **/ 2152 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev, 2153 struct packet_type *pt, struct net_device *orig_dev) 2154 { 2155 struct sock *sk; 2156 struct iucv_sock *iucv; 2157 struct af_iucv_trans_hdr *trans_hdr; 2158 char nullstring[8]; 2159 int err = 0; 2160 2161 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) { 2162 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d", 2163 (int)skb->len, 2164 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr))); 2165 kfree_skb(skb); 2166 return NET_RX_SUCCESS; 2167 } 2168 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) 2169 if (skb_linearize(skb)) { 2170 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d", 2171 (int)skb->len); 2172 kfree_skb(skb); 2173 return NET_RX_SUCCESS; 2174 } 2175 skb_pull(skb, ETH_HLEN); 2176 trans_hdr = (struct af_iucv_trans_hdr *)skb->data; 2177 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); 2178 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); 2179 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); 2180 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); 2181 memset(nullstring, 0, sizeof(nullstring)); 2182 iucv = NULL; 2183 sk = NULL; 2184 read_lock(&iucv_sk_list.lock); 2185 sk_for_each(sk, &iucv_sk_list.head) { 2186 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) { 2187 if ((!memcmp(&iucv_sk(sk)->src_name, 2188 trans_hdr->destAppName, 8)) && 2189 (!memcmp(&iucv_sk(sk)->src_user_id, 2190 trans_hdr->destUserID, 8)) && 2191 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) && 2192 (!memcmp(&iucv_sk(sk)->dst_user_id, 2193 nullstring, 8))) { 2194 iucv = iucv_sk(sk); 2195 break; 2196 } 2197 } else { 2198 if ((!memcmp(&iucv_sk(sk)->src_name, 2199 trans_hdr->destAppName, 8)) && 2200 (!memcmp(&iucv_sk(sk)->src_user_id, 2201 trans_hdr->destUserID, 8)) && 2202 (!memcmp(&iucv_sk(sk)->dst_name, 2203 trans_hdr->srcAppName, 8)) && 2204 (!memcmp(&iucv_sk(sk)->dst_user_id, 2205 trans_hdr->srcUserID, 8))) { 2206 iucv = iucv_sk(sk); 2207 break; 2208 } 2209 } 2210 } 2211 read_unlock(&iucv_sk_list.lock); 2212 if (!iucv) 2213 sk = NULL; 2214 2215 /* no sock 2216 how should we send with no sock 2217 1) send without sock no send rc checking? 2218 2) introduce default sock to handle this cases 2219 2220 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case 2221 data -> send FIN 2222 SYN|ACK, SYN|FIN, FIN -> no action? */ 2223 2224 switch (trans_hdr->flags) { 2225 case AF_IUCV_FLAG_SYN: 2226 /* connect request */ 2227 err = afiucv_hs_callback_syn(sk, skb); 2228 break; 2229 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK): 2230 /* connect request confirmed */ 2231 err = afiucv_hs_callback_synack(sk, skb); 2232 break; 2233 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN): 2234 /* connect request refused */ 2235 err = afiucv_hs_callback_synfin(sk, skb); 2236 break; 2237 case (AF_IUCV_FLAG_FIN): 2238 /* close request */ 2239 err = afiucv_hs_callback_fin(sk, skb); 2240 break; 2241 case (AF_IUCV_FLAG_WIN): 2242 err = afiucv_hs_callback_win(sk, skb); 2243 if (skb->len == sizeof(struct af_iucv_trans_hdr)) { 2244 kfree_skb(skb); 2245 break; 2246 } 2247 /* fall through and receive non-zero length data */ 2248 case (AF_IUCV_FLAG_SHT): 2249 /* shutdown request */ 2250 /* fall through and receive zero length data */ 2251 case 0: 2252 /* plain data frame */ 2253 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class; 2254 err = afiucv_hs_callback_rx(sk, skb); 2255 break; 2256 default: 2257 ; 2258 } 2259 2260 return err; 2261 } 2262 2263 /** 2264 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets 2265 * transport 2266 **/ 2267 static void afiucv_hs_callback_txnotify(struct sk_buff *skb, 2268 enum iucv_tx_notify n) 2269 { 2270 struct sock *isk = skb->sk; 2271 struct sock *sk = NULL; 2272 struct iucv_sock *iucv = NULL; 2273 struct sk_buff_head *list; 2274 struct sk_buff *list_skb; 2275 struct sk_buff *nskb; 2276 unsigned long flags; 2277 2278 read_lock_irqsave(&iucv_sk_list.lock, flags); 2279 sk_for_each(sk, &iucv_sk_list.head) 2280 if (sk == isk) { 2281 iucv = iucv_sk(sk); 2282 break; 2283 } 2284 read_unlock_irqrestore(&iucv_sk_list.lock, flags); 2285 2286 if (!iucv || sock_flag(sk, SOCK_ZAPPED)) 2287 return; 2288 2289 list = &iucv->send_skb_q; 2290 spin_lock_irqsave(&list->lock, flags); 2291 if (skb_queue_empty(list)) 2292 goto out_unlock; 2293 list_skb = list->next; 2294 nskb = list_skb->next; 2295 while (list_skb != (struct sk_buff *)list) { 2296 if (skb_shinfo(list_skb) == skb_shinfo(skb)) { 2297 switch (n) { 2298 case TX_NOTIFY_OK: 2299 __skb_unlink(list_skb, list); 2300 kfree_skb(list_skb); 2301 iucv_sock_wake_msglim(sk); 2302 break; 2303 case TX_NOTIFY_PENDING: 2304 atomic_inc(&iucv->pendings); 2305 break; 2306 case TX_NOTIFY_DELAYED_OK: 2307 __skb_unlink(list_skb, list); 2308 atomic_dec(&iucv->pendings); 2309 if (atomic_read(&iucv->pendings) <= 0) 2310 iucv_sock_wake_msglim(sk); 2311 kfree_skb(list_skb); 2312 break; 2313 case TX_NOTIFY_UNREACHABLE: 2314 case TX_NOTIFY_DELAYED_UNREACHABLE: 2315 case TX_NOTIFY_TPQFULL: /* not yet used */ 2316 case TX_NOTIFY_GENERALERROR: 2317 case TX_NOTIFY_DELAYED_GENERALERROR: 2318 __skb_unlink(list_skb, list); 2319 kfree_skb(list_skb); 2320 if (sk->sk_state == IUCV_CONNECTED) { 2321 sk->sk_state = IUCV_DISCONN; 2322 sk->sk_state_change(sk); 2323 } 2324 break; 2325 } 2326 break; 2327 } 2328 list_skb = nskb; 2329 nskb = nskb->next; 2330 } 2331 out_unlock: 2332 spin_unlock_irqrestore(&list->lock, flags); 2333 2334 if (sk->sk_state == IUCV_CLOSING) { 2335 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { 2336 sk->sk_state = IUCV_CLOSED; 2337 sk->sk_state_change(sk); 2338 } 2339 } 2340 2341 } 2342 2343 /* 2344 * afiucv_netdev_event: handle netdev notifier chain events 2345 */ 2346 static int afiucv_netdev_event(struct notifier_block *this, 2347 unsigned long event, void *ptr) 2348 { 2349 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 2350 struct sock *sk; 2351 struct iucv_sock *iucv; 2352 2353 switch (event) { 2354 case NETDEV_REBOOT: 2355 case NETDEV_GOING_DOWN: 2356 sk_for_each(sk, &iucv_sk_list.head) { 2357 iucv = iucv_sk(sk); 2358 if ((iucv->hs_dev == event_dev) && 2359 (sk->sk_state == IUCV_CONNECTED)) { 2360 if (event == NETDEV_GOING_DOWN) 2361 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN); 2362 sk->sk_state = IUCV_DISCONN; 2363 sk->sk_state_change(sk); 2364 } 2365 } 2366 break; 2367 case NETDEV_DOWN: 2368 case NETDEV_UNREGISTER: 2369 default: 2370 break; 2371 } 2372 return NOTIFY_DONE; 2373 } 2374 2375 static struct notifier_block afiucv_netdev_notifier = { 2376 .notifier_call = afiucv_netdev_event, 2377 }; 2378 2379 static const struct proto_ops iucv_sock_ops = { 2380 .family = PF_IUCV, 2381 .owner = THIS_MODULE, 2382 .release = iucv_sock_release, 2383 .bind = iucv_sock_bind, 2384 .connect = iucv_sock_connect, 2385 .listen = iucv_sock_listen, 2386 .accept = iucv_sock_accept, 2387 .getname = iucv_sock_getname, 2388 .sendmsg = iucv_sock_sendmsg, 2389 .recvmsg = iucv_sock_recvmsg, 2390 .poll = iucv_sock_poll, 2391 .ioctl = sock_no_ioctl, 2392 .mmap = sock_no_mmap, 2393 .socketpair = sock_no_socketpair, 2394 .shutdown = iucv_sock_shutdown, 2395 .setsockopt = iucv_sock_setsockopt, 2396 .getsockopt = iucv_sock_getsockopt, 2397 }; 2398 2399 static const struct net_proto_family iucv_sock_family_ops = { 2400 .family = AF_IUCV, 2401 .owner = THIS_MODULE, 2402 .create = iucv_sock_create, 2403 }; 2404 2405 static struct packet_type iucv_packet_type = { 2406 .type = cpu_to_be16(ETH_P_AF_IUCV), 2407 .func = afiucv_hs_rcv, 2408 }; 2409 2410 static int afiucv_iucv_init(void) 2411 { 2412 int err; 2413 2414 err = pr_iucv->iucv_register(&af_iucv_handler, 0); 2415 if (err) 2416 goto out; 2417 /* establish dummy device */ 2418 af_iucv_driver.bus = pr_iucv->bus; 2419 err = driver_register(&af_iucv_driver); 2420 if (err) 2421 goto out_iucv; 2422 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); 2423 if (!af_iucv_dev) { 2424 err = -ENOMEM; 2425 goto out_driver; 2426 } 2427 dev_set_name(af_iucv_dev, "af_iucv"); 2428 af_iucv_dev->bus = pr_iucv->bus; 2429 af_iucv_dev->parent = pr_iucv->root; 2430 af_iucv_dev->release = (void (*)(struct device *))kfree; 2431 af_iucv_dev->driver = &af_iucv_driver; 2432 err = device_register(af_iucv_dev); 2433 if (err) 2434 goto out_iucv_dev; 2435 return 0; 2436 2437 out_iucv_dev: 2438 put_device(af_iucv_dev); 2439 out_driver: 2440 driver_unregister(&af_iucv_driver); 2441 out_iucv: 2442 pr_iucv->iucv_unregister(&af_iucv_handler, 0); 2443 out: 2444 return err; 2445 } 2446 2447 static int __init afiucv_init(void) 2448 { 2449 int err; 2450 2451 if (MACHINE_IS_VM) { 2452 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err); 2453 if (unlikely(err)) { 2454 WARN_ON(err); 2455 err = -EPROTONOSUPPORT; 2456 goto out; 2457 } 2458 2459 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv"); 2460 if (!pr_iucv) { 2461 printk(KERN_WARNING "iucv_if lookup failed\n"); 2462 memset(&iucv_userid, 0, sizeof(iucv_userid)); 2463 } 2464 } else { 2465 memset(&iucv_userid, 0, sizeof(iucv_userid)); 2466 pr_iucv = NULL; 2467 } 2468 2469 err = proto_register(&iucv_proto, 0); 2470 if (err) 2471 goto out; 2472 err = sock_register(&iucv_sock_family_ops); 2473 if (err) 2474 goto out_proto; 2475 2476 if (pr_iucv) { 2477 err = afiucv_iucv_init(); 2478 if (err) 2479 goto out_sock; 2480 } else 2481 register_netdevice_notifier(&afiucv_netdev_notifier); 2482 dev_add_pack(&iucv_packet_type); 2483 return 0; 2484 2485 out_sock: 2486 sock_unregister(PF_IUCV); 2487 out_proto: 2488 proto_unregister(&iucv_proto); 2489 out: 2490 if (pr_iucv) 2491 symbol_put(iucv_if); 2492 return err; 2493 } 2494 2495 static void __exit afiucv_exit(void) 2496 { 2497 if (pr_iucv) { 2498 device_unregister(af_iucv_dev); 2499 driver_unregister(&af_iucv_driver); 2500 pr_iucv->iucv_unregister(&af_iucv_handler, 0); 2501 symbol_put(iucv_if); 2502 } else 2503 unregister_netdevice_notifier(&afiucv_netdev_notifier); 2504 dev_remove_pack(&iucv_packet_type); 2505 sock_unregister(PF_IUCV); 2506 proto_unregister(&iucv_proto); 2507 } 2508 2509 module_init(afiucv_init); 2510 module_exit(afiucv_exit); 2511 2512 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>"); 2513 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION); 2514 MODULE_VERSION(VERSION); 2515 MODULE_LICENSE("GPL"); 2516 MODULE_ALIAS_NETPROTO(PF_IUCV); 2517