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