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