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 | |
Ursula Braun | 52a82e2 | 2016-01-19 10:41:33 +0100 | [diff] [blame^] | 711 | if (addr_len < sizeof(struct sockaddr_iucv)) |
| 712 | return -EINVAL; |
| 713 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 714 | lock_sock(sk); |
| 715 | if (sk->sk_state != IUCV_OPEN) { |
| 716 | err = -EBADFD; |
| 717 | goto done; |
| 718 | } |
| 719 | |
| 720 | write_lock_bh(&iucv_sk_list.lock); |
| 721 | |
| 722 | iucv = iucv_sk(sk); |
| 723 | if (__iucv_get_sock_by_name(sa->siucv_name)) { |
| 724 | err = -EADDRINUSE; |
| 725 | goto done_unlock; |
| 726 | } |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 727 | if (iucv->path) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 728 | goto done_unlock; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 729 | |
| 730 | /* Bind the socket */ |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 731 | if (pr_iucv) |
| 732 | if (!memcmp(sa->siucv_user_id, iucv_userid, 8)) |
| 733 | goto vm_bind; /* VM IUCV transport */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 734 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 735 | /* try hiper transport */ |
| 736 | memcpy(uid, sa->siucv_user_id, sizeof(uid)); |
| 737 | ASCEBC(uid, 8); |
| 738 | rcu_read_lock(); |
| 739 | for_each_netdev_rcu(&init_net, dev) { |
| 740 | if (!memcmp(dev->perm_addr, uid, 8)) { |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 741 | memcpy(iucv->src_user_id, sa->siucv_user_id, 8); |
Philipp Hachtmann | 53a4b49 | 2014-05-28 10:22:28 +0200 | [diff] [blame] | 742 | /* Check for unitialized siucv_name */ |
| 743 | if (strncmp(sa->siucv_name, " ", 8) == 0) |
| 744 | __iucv_auto_name(iucv); |
| 745 | else |
| 746 | memcpy(iucv->src_name, sa->siucv_name, 8); |
Ursula Braun | 816abba | 2011-12-19 22:56:29 +0000 | [diff] [blame] | 747 | sk->sk_bound_dev_if = dev->ifindex; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 748 | iucv->hs_dev = dev; |
| 749 | dev_hold(dev); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 750 | sk->sk_state = IUCV_BOUND; |
| 751 | iucv->transport = AF_IUCV_TRANS_HIPER; |
| 752 | if (!iucv->msglimit) |
| 753 | iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT; |
| 754 | rcu_read_unlock(); |
| 755 | goto done_unlock; |
| 756 | } |
| 757 | } |
| 758 | rcu_read_unlock(); |
| 759 | vm_bind: |
| 760 | if (pr_iucv) { |
| 761 | /* use local userid for backward compat */ |
| 762 | memcpy(iucv->src_name, sa->siucv_name, 8); |
| 763 | memcpy(iucv->src_user_id, iucv_userid, 8); |
| 764 | sk->sk_state = IUCV_BOUND; |
| 765 | iucv->transport = AF_IUCV_TRANS_IUCV; |
| 766 | if (!iucv->msglimit) |
| 767 | iucv->msglimit = IUCV_QUEUELEN_DEFAULT; |
| 768 | goto done_unlock; |
| 769 | } |
| 770 | /* found no dev to bind */ |
| 771 | err = -ENODEV; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 772 | done_unlock: |
| 773 | /* Release the socket list lock */ |
| 774 | write_unlock_bh(&iucv_sk_list.lock); |
| 775 | done: |
| 776 | release_sock(sk); |
| 777 | return err; |
| 778 | } |
| 779 | |
| 780 | /* Automatically bind an unbound socket */ |
| 781 | static int iucv_sock_autobind(struct sock *sk) |
| 782 | { |
| 783 | struct iucv_sock *iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 784 | int err = 0; |
| 785 | |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 786 | if (unlikely(!pr_iucv)) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 787 | return -EPROTO; |
| 788 | |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 789 | memcpy(iucv->src_user_id, iucv_userid, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 790 | |
| 791 | write_lock_bh(&iucv_sk_list.lock); |
Philipp Hachtmann | 53a4b49 | 2014-05-28 10:22:28 +0200 | [diff] [blame] | 792 | __iucv_auto_name(iucv); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 793 | write_unlock_bh(&iucv_sk_list.lock); |
| 794 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 795 | if (!iucv->msglimit) |
| 796 | iucv->msglimit = IUCV_QUEUELEN_DEFAULT; |
| 797 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 798 | return err; |
| 799 | } |
| 800 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 801 | static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 802 | { |
| 803 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 804 | struct sock *sk = sock->sk; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 805 | struct iucv_sock *iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 806 | unsigned char user_data[16]; |
| 807 | int err; |
| 808 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 809 | high_nmcpy(user_data, sa->siucv_name); |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 810 | low_nmcpy(user_data, iucv->src_name); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 811 | ASCEBC(user_data, sizeof(user_data)); |
| 812 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 813 | /* Create path. */ |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 814 | iucv->path = iucv_path_alloc(iucv->msglimit, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 815 | IUCV_IPRMDATA, GFP_KERNEL); |
Ursula Braun | d444472 | 2008-02-07 18:07:19 -0800 | [diff] [blame] | 816 | if (!iucv->path) { |
| 817 | err = -ENOMEM; |
| 818 | goto done; |
| 819 | } |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 820 | err = pr_iucv->path_connect(iucv->path, &af_iucv_handler, |
| 821 | sa->siucv_user_id, NULL, user_data, |
| 822 | sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 823 | if (err) { |
| 824 | iucv_path_free(iucv->path); |
| 825 | iucv->path = NULL; |
Hendrik Brueckner | 55cdea9 | 2009-01-05 18:07:07 -0800 | [diff] [blame] | 826 | switch (err) { |
| 827 | case 0x0b: /* Target communicator is not logged on */ |
| 828 | err = -ENETUNREACH; |
| 829 | break; |
| 830 | case 0x0d: /* Max connections for this guest exceeded */ |
| 831 | case 0x0e: /* Max connections for target guest exceeded */ |
| 832 | err = -EAGAIN; |
| 833 | break; |
| 834 | case 0x0f: /* Missing IUCV authorization */ |
| 835 | err = -EACCES; |
| 836 | break; |
| 837 | default: |
| 838 | err = -ECONNREFUSED; |
| 839 | break; |
| 840 | } |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 841 | } |
| 842 | done: |
| 843 | return err; |
| 844 | } |
| 845 | |
| 846 | /* Connect an unconnected socket */ |
| 847 | static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr, |
| 848 | int alen, int flags) |
| 849 | { |
| 850 | struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; |
| 851 | struct sock *sk = sock->sk; |
| 852 | struct iucv_sock *iucv = iucv_sk(sk); |
| 853 | int err; |
| 854 | |
| 855 | if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv)) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND) |
| 859 | return -EBADFD; |
| 860 | |
| 861 | if (sk->sk_state == IUCV_OPEN && |
| 862 | iucv->transport == AF_IUCV_TRANS_HIPER) |
| 863 | return -EBADFD; /* explicit bind required */ |
| 864 | |
| 865 | if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET) |
| 866 | return -EINVAL; |
| 867 | |
| 868 | if (sk->sk_state == IUCV_OPEN) { |
| 869 | err = iucv_sock_autobind(sk); |
| 870 | if (unlikely(err)) |
| 871 | return err; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 874 | lock_sock(sk); |
| 875 | |
| 876 | /* Set the destination information */ |
| 877 | memcpy(iucv->dst_user_id, sa->siucv_user_id, 8); |
| 878 | memcpy(iucv->dst_name, sa->siucv_name, 8); |
| 879 | |
| 880 | if (iucv->transport == AF_IUCV_TRANS_HIPER) |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 881 | err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 882 | else |
| 883 | err = afiucv_path_connect(sock, addr); |
| 884 | if (err) |
| 885 | goto done; |
| 886 | |
| 887 | if (sk->sk_state != IUCV_CONNECTED) |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 888 | err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED, |
| 889 | IUCV_DISCONN), |
| 890 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 891 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 892 | if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED) |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 893 | err = -ECONNREFUSED; |
Ursula Braun | 18becbc | 2009-01-05 18:07:46 -0800 | [diff] [blame] | 894 | |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 895 | if (err && iucv->transport == AF_IUCV_TRANS_IUCV) |
| 896 | iucv_sever_path(sk, 0); |
Ursula Braun | 18becbc | 2009-01-05 18:07:46 -0800 | [diff] [blame] | 897 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 898 | done: |
| 899 | release_sock(sk); |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | /* Move a socket into listening state. */ |
| 904 | static int iucv_sock_listen(struct socket *sock, int backlog) |
| 905 | { |
| 906 | struct sock *sk = sock->sk; |
| 907 | int err; |
| 908 | |
| 909 | lock_sock(sk); |
| 910 | |
| 911 | err = -EINVAL; |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 912 | if (sk->sk_state != IUCV_BOUND) |
| 913 | goto done; |
| 914 | |
| 915 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 916 | goto done; |
| 917 | |
| 918 | sk->sk_max_ack_backlog = backlog; |
| 919 | sk->sk_ack_backlog = 0; |
| 920 | sk->sk_state = IUCV_LISTEN; |
| 921 | err = 0; |
| 922 | |
| 923 | done: |
| 924 | release_sock(sk); |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | /* Accept a pending connection */ |
| 929 | static int iucv_sock_accept(struct socket *sock, struct socket *newsock, |
| 930 | int flags) |
| 931 | { |
| 932 | DECLARE_WAITQUEUE(wait, current); |
| 933 | struct sock *sk = sock->sk, *nsk; |
| 934 | long timeo; |
| 935 | int err = 0; |
| 936 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 937 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 938 | |
| 939 | if (sk->sk_state != IUCV_LISTEN) { |
| 940 | err = -EBADFD; |
| 941 | goto done; |
| 942 | } |
| 943 | |
| 944 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 945 | |
| 946 | /* Wait for an incoming connection */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 947 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 948 | while (!(nsk = iucv_accept_dequeue(sk, newsock))) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 949 | set_current_state(TASK_INTERRUPTIBLE); |
| 950 | if (!timeo) { |
| 951 | err = -EAGAIN; |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | release_sock(sk); |
| 956 | timeo = schedule_timeout(timeo); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 957 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 958 | |
| 959 | if (sk->sk_state != IUCV_LISTEN) { |
| 960 | err = -EBADFD; |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | if (signal_pending(current)) { |
| 965 | err = sock_intr_errno(timeo); |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 971 | remove_wait_queue(sk_sleep(sk), &wait); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 972 | |
| 973 | if (err) |
| 974 | goto done; |
| 975 | |
| 976 | newsock->state = SS_CONNECTED; |
| 977 | |
| 978 | done: |
| 979 | release_sock(sk); |
| 980 | return err; |
| 981 | } |
| 982 | |
| 983 | static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr, |
| 984 | int *len, int peer) |
| 985 | { |
| 986 | struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr; |
| 987 | struct sock *sk = sock->sk; |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 988 | struct iucv_sock *iucv = iucv_sk(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 989 | |
| 990 | addr->sa_family = AF_IUCV; |
| 991 | *len = sizeof(struct sockaddr_iucv); |
| 992 | |
| 993 | if (peer) { |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 994 | memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8); |
| 995 | memcpy(siucv->siucv_name, iucv->dst_name, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 996 | } else { |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 997 | memcpy(siucv->siucv_user_id, iucv->src_user_id, 8); |
| 998 | memcpy(siucv->siucv_name, iucv->src_name, 8); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 999 | } |
| 1000 | memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port)); |
| 1001 | memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr)); |
Ursula Braun | 493d397 | 2011-08-08 01:33:52 +0000 | [diff] [blame] | 1002 | memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid)); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1007 | /** |
| 1008 | * iucv_send_iprm() - Send socket data in parameter list of an iucv message. |
| 1009 | * @path: IUCV path |
| 1010 | * @msg: Pointer to a struct iucv_message |
| 1011 | * @skb: The socket data to send, skb->len MUST BE <= 7 |
| 1012 | * |
| 1013 | * Send the socket data in the parameter list in the iucv message |
| 1014 | * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter |
| 1015 | * list and the socket data len at index 7 (last byte). |
| 1016 | * See also iucv_msg_length(). |
| 1017 | * |
| 1018 | * Returns the error code from the iucv_message_send() call. |
| 1019 | */ |
| 1020 | static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg, |
| 1021 | struct sk_buff *skb) |
| 1022 | { |
| 1023 | u8 prmdata[8]; |
| 1024 | |
| 1025 | memcpy(prmdata, (void *) skb->data, skb->len); |
| 1026 | prmdata[7] = 0xff - (u8) skb->len; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1027 | return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0, |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1028 | (void *) prmdata, 8); |
| 1029 | } |
| 1030 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1031 | static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg, |
| 1032 | size_t len) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1033 | { |
| 1034 | struct sock *sk = sock->sk; |
| 1035 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1036 | struct sk_buff *skb; |
Eugene Crosser | 0506eb0 | 2015-12-11 12:27:50 +0100 | [diff] [blame] | 1037 | struct iucv_message txmsg = {0}; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1038 | struct cmsghdr *cmsg; |
| 1039 | int cmsg_done; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1040 | long timeo; |
Ursula Braun | 8f7c502 | 2008-12-25 13:39:47 +0100 | [diff] [blame] | 1041 | char user_id[9]; |
| 1042 | char appl_id[9]; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1043 | int err; |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1044 | int noblock = msg->msg_flags & MSG_DONTWAIT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1045 | |
| 1046 | err = sock_error(sk); |
| 1047 | if (err) |
| 1048 | return err; |
| 1049 | |
| 1050 | if (msg->msg_flags & MSG_OOB) |
| 1051 | return -EOPNOTSUPP; |
| 1052 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1053 | /* SOCK_SEQPACKET: we do not support segmented records */ |
| 1054 | if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR)) |
| 1055 | return -EOPNOTSUPP; |
| 1056 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1057 | lock_sock(sk); |
| 1058 | |
| 1059 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 1060 | err = -EPIPE; |
| 1061 | goto out; |
| 1062 | } |
| 1063 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1064 | /* Return if the socket is not in connected state */ |
| 1065 | if (sk->sk_state != IUCV_CONNECTED) { |
| 1066 | err = -ENOTCONN; |
| 1067 | goto out; |
| 1068 | } |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1069 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1070 | /* initialize defaults */ |
| 1071 | cmsg_done = 0; /* check for duplicate headers */ |
| 1072 | txmsg.class = 0; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1073 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1074 | /* iterate over control messages */ |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 1075 | for_each_cmsghdr(cmsg, msg) { |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1076 | if (!CMSG_OK(msg, cmsg)) { |
| 1077 | err = -EINVAL; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1078 | goto out; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1081 | if (cmsg->cmsg_level != SOL_IUCV) |
| 1082 | continue; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1083 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1084 | if (cmsg->cmsg_type & cmsg_done) { |
| 1085 | err = -EINVAL; |
| 1086 | goto out; |
| 1087 | } |
| 1088 | cmsg_done |= cmsg->cmsg_type; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1089 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1090 | switch (cmsg->cmsg_type) { |
| 1091 | case SCM_IUCV_TRGCLS: |
| 1092 | if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) { |
| 1093 | err = -EINVAL; |
| 1094 | goto out; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1097 | /* set iucv message target class */ |
| 1098 | memcpy(&txmsg.class, |
| 1099 | (void *) CMSG_DATA(cmsg), TRGCLS_SIZE); |
| 1100 | |
| 1101 | break; |
| 1102 | |
| 1103 | default: |
| 1104 | err = -EINVAL; |
| 1105 | goto out; |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /* allocate one skb for each iucv message: |
| 1110 | * this is fine for SOCK_SEQPACKET (unless we want to support |
| 1111 | * segmented records using the MSG_EOR flag), but |
| 1112 | * for SOCK_STREAM we might want to improve it in future */ |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1113 | if (iucv->transport == AF_IUCV_TRANS_HIPER) |
| 1114 | skb = sock_alloc_send_skb(sk, |
| 1115 | len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN, |
| 1116 | noblock, &err); |
| 1117 | else |
| 1118 | skb = sock_alloc_send_skb(sk, len, noblock, &err); |
Eugene Crosser | ed4ac42 | 2015-03-30 15:40:42 +0200 | [diff] [blame] | 1119 | if (!skb) |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1120 | goto out; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1121 | if (iucv->transport == AF_IUCV_TRANS_HIPER) |
| 1122 | skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN); |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 1123 | if (memcpy_from_msg(skb_put(skb, len), msg, len)) { |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1124 | err = -EFAULT; |
| 1125 | goto fail; |
| 1126 | } |
| 1127 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1128 | /* wait if outstanding messages for iucv path has reached */ |
| 1129 | timeo = sock_sndtimeo(sk, noblock); |
| 1130 | err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo); |
| 1131 | if (err) |
| 1132 | goto fail; |
| 1133 | |
| 1134 | /* return -ECONNRESET if the socket is no longer connected */ |
| 1135 | if (sk->sk_state != IUCV_CONNECTED) { |
| 1136 | err = -ECONNRESET; |
| 1137 | goto fail; |
| 1138 | } |
| 1139 | |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1140 | /* increment and save iucv message tag for msg_completion cbk */ |
| 1141 | txmsg.tag = iucv->send_tag++; |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1142 | IUCV_SKB_CB(skb)->tag = txmsg.tag; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 1143 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1144 | if (iucv->transport == AF_IUCV_TRANS_HIPER) { |
| 1145 | atomic_inc(&iucv->msg_sent); |
| 1146 | err = afiucv_hs_send(&txmsg, sk, skb, 0); |
| 1147 | if (err) { |
| 1148 | atomic_dec(&iucv->msg_sent); |
| 1149 | goto fail; |
| 1150 | } |
| 1151 | goto release; |
| 1152 | } |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1153 | skb_queue_tail(&iucv->send_skb_q, skb); |
| 1154 | |
| 1155 | if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) |
| 1156 | && skb->len <= 7) { |
| 1157 | err = iucv_send_iprm(iucv->path, &txmsg, skb); |
| 1158 | |
| 1159 | /* on success: there is no message_complete callback |
| 1160 | * for an IPRMDATA msg; remove skb from send queue */ |
| 1161 | if (err == 0) { |
| 1162 | skb_unlink(skb, &iucv->send_skb_q); |
| 1163 | kfree_skb(skb); |
| 1164 | } |
| 1165 | |
| 1166 | /* this error should never happen since the |
| 1167 | * IUCV_IPRMDATA path flag is set... sever path */ |
| 1168 | if (err == 0x15) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1169 | pr_iucv->path_sever(iucv->path, NULL); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1170 | skb_unlink(skb, &iucv->send_skb_q); |
| 1171 | err = -EPIPE; |
| 1172 | goto fail; |
| 1173 | } |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1174 | } else |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1175 | err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0, |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1176 | (void *) skb->data, skb->len); |
| 1177 | if (err) { |
| 1178 | if (err == 3) { |
| 1179 | user_id[8] = 0; |
| 1180 | memcpy(user_id, iucv->dst_user_id, 8); |
| 1181 | appl_id[8] = 0; |
| 1182 | memcpy(appl_id, iucv->dst_name, 8); |
| 1183 | pr_err("Application %s on z/VM guest %s" |
| 1184 | " exceeds message limit\n", |
| 1185 | appl_id, user_id); |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1186 | err = -EAGAIN; |
| 1187 | } else |
| 1188 | err = -EPIPE; |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1189 | skb_unlink(skb, &iucv->send_skb_q); |
Hendrik Brueckner | bb664f4 | 2009-06-17 21:54:47 +0000 | [diff] [blame] | 1190 | goto fail; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1193 | release: |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1194 | release_sock(sk); |
| 1195 | return len; |
| 1196 | |
| 1197 | fail: |
| 1198 | kfree_skb(skb); |
| 1199 | out: |
| 1200 | release_sock(sk); |
| 1201 | return err; |
| 1202 | } |
| 1203 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1204 | /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's |
| 1205 | * |
| 1206 | * Locking: must be called with message_q.lock held |
| 1207 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1208 | static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len) |
| 1209 | { |
| 1210 | int dataleft, size, copied = 0; |
| 1211 | struct sk_buff *nskb; |
| 1212 | |
| 1213 | dataleft = len; |
| 1214 | while (dataleft) { |
| 1215 | if (dataleft >= sk->sk_rcvbuf / 4) |
| 1216 | size = sk->sk_rcvbuf / 4; |
| 1217 | else |
| 1218 | size = dataleft; |
| 1219 | |
| 1220 | nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA); |
| 1221 | if (!nskb) |
| 1222 | return -ENOMEM; |
| 1223 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1224 | /* copy target class to control buffer of new skb */ |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1225 | IUCV_SKB_CB(nskb)->class = IUCV_SKB_CB(skb)->class; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1226 | |
| 1227 | /* copy data fragment */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1228 | memcpy(nskb->data, skb->data + copied, size); |
| 1229 | copied += size; |
| 1230 | dataleft -= size; |
| 1231 | |
| 1232 | skb_reset_transport_header(nskb); |
| 1233 | skb_reset_network_header(nskb); |
| 1234 | nskb->len = size; |
| 1235 | |
| 1236 | skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb); |
| 1237 | } |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1242 | /* iucv_process_message() - Receive a single outstanding IUCV message |
| 1243 | * |
| 1244 | * Locking: must be called with message_q.lock held |
| 1245 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1246 | static void iucv_process_message(struct sock *sk, struct sk_buff *skb, |
| 1247 | struct iucv_path *path, |
| 1248 | struct iucv_message *msg) |
| 1249 | { |
| 1250 | int rc; |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1251 | unsigned int len; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1252 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1253 | len = iucv_msg_length(msg); |
| 1254 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1255 | /* store msg target class in the second 4 bytes of skb ctrl buffer */ |
| 1256 | /* Note: the first 4 bytes are reserved for msg tag */ |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1257 | IUCV_SKB_CB(skb)->class = msg->class; |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1258 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1259 | /* check for special IPRM messages (e.g. iucv_sock_shutdown) */ |
| 1260 | if ((msg->flags & IUCV_IPRMDATA) && len > 7) { |
| 1261 | if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) { |
| 1262 | skb->data = NULL; |
| 1263 | skb->len = 0; |
| 1264 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1265 | } else { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1266 | rc = pr_iucv->message_receive(path, msg, |
| 1267 | msg->flags & IUCV_IPRMDATA, |
| 1268 | skb->data, len, NULL); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1269 | if (rc) { |
| 1270 | kfree_skb(skb); |
| 1271 | return; |
| 1272 | } |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1273 | /* we need to fragment iucv messages for SOCK_STREAM only; |
| 1274 | * for SOCK_SEQPACKET, it is only relevant if we support |
| 1275 | * record segmentation using MSG_EOR (see also recvmsg()) */ |
| 1276 | if (sk->sk_type == SOCK_STREAM && |
| 1277 | skb->truesize >= sk->sk_rcvbuf / 4) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1278 | rc = iucv_fragment_skb(sk, skb, len); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1279 | kfree_skb(skb); |
| 1280 | skb = NULL; |
| 1281 | if (rc) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1282 | pr_iucv->path_sever(path, NULL); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1283 | return; |
| 1284 | } |
| 1285 | skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q); |
| 1286 | } else { |
| 1287 | skb_reset_transport_header(skb); |
| 1288 | skb_reset_network_header(skb); |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1289 | skb->len = len; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1293 | IUCV_SKB_CB(skb)->offset = 0; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1294 | if (sock_queue_rcv_skb(sk, skb)) |
| 1295 | skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb); |
| 1296 | } |
| 1297 | |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1298 | /* iucv_process_message_q() - Process outstanding IUCV messages |
| 1299 | * |
| 1300 | * Locking: must be called with message_q.lock held |
| 1301 | */ |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1302 | static void iucv_process_message_q(struct sock *sk) |
| 1303 | { |
| 1304 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1305 | struct sk_buff *skb; |
| 1306 | struct sock_msg_q *p, *n; |
| 1307 | |
| 1308 | list_for_each_entry_safe(p, n, &iucv->message_q.list, list) { |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1309 | skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1310 | if (!skb) |
| 1311 | break; |
| 1312 | iucv_process_message(sk, skb, p->path, &p->msg); |
| 1313 | list_del(&p->list); |
| 1314 | kfree(p); |
| 1315 | if (!skb_queue_empty(&iucv->backlog_skb_q)) |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1320 | static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, |
| 1321 | size_t len, int flags) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1322 | { |
| 1323 | int noblock = flags & MSG_DONTWAIT; |
| 1324 | struct sock *sk = sock->sk; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1325 | struct iucv_sock *iucv = iucv_sk(sk); |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1326 | unsigned int copied, rlen; |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 1327 | struct sk_buff *skb, *rskb, *cskb; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1328 | int err = 0; |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1329 | u32 offset; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1330 | |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 1331 | if ((sk->sk_state == IUCV_DISCONN) && |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1332 | skb_queue_empty(&iucv->backlog_skb_q) && |
| 1333 | skb_queue_empty(&sk->sk_receive_queue) && |
| 1334 | list_empty(&iucv->message_q.list)) |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1335 | return 0; |
| 1336 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1337 | if (flags & (MSG_OOB)) |
| 1338 | return -EOPNOTSUPP; |
| 1339 | |
Hendrik Brueckner | 60d3705 | 2009-04-21 06:04:21 +0000 | [diff] [blame] | 1340 | /* receive/dequeue next skb: |
| 1341 | * the function understands MSG_PEEK and, thus, does not dequeue skb */ |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1342 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 1343 | if (!skb) { |
| 1344 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1345 | return 0; |
| 1346 | return err; |
| 1347 | } |
| 1348 | |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1349 | offset = IUCV_SKB_CB(skb)->offset; |
| 1350 | rlen = skb->len - offset; /* real length of skb */ |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1351 | copied = min_t(unsigned int, rlen, len); |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1352 | if (!rlen) |
| 1353 | sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1354 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1355 | cskb = skb; |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1356 | if (skb_copy_datagram_msg(cskb, offset, msg, copied)) { |
Hendrik Brueckner | 802788b | 2009-04-21 23:26:26 +0000 | [diff] [blame] | 1357 | if (!(flags & MSG_PEEK)) |
| 1358 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1359 | return -EFAULT; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1362 | /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */ |
| 1363 | if (sk->sk_type == SOCK_SEQPACKET) { |
| 1364 | if (copied < rlen) |
| 1365 | msg->msg_flags |= MSG_TRUNC; |
| 1366 | /* each iucv message contains a complete record */ |
| 1367 | msg->msg_flags |= MSG_EOR; |
| 1368 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1369 | |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1370 | /* create control message to store iucv msg target class: |
| 1371 | * get the trgcls from the control buffer of the skb due to |
| 1372 | * fragmentation of original iucv message. */ |
| 1373 | err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS, |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1374 | sizeof(IUCV_SKB_CB(skb)->class), |
| 1375 | (void *)&IUCV_SKB_CB(skb)->class); |
Hendrik Brueckner | 44b1e6b | 2009-04-21 23:26:24 +0000 | [diff] [blame] | 1376 | if (err) { |
| 1377 | if (!(flags & MSG_PEEK)) |
| 1378 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1379 | return err; |
| 1380 | } |
| 1381 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1382 | /* Mark read part of skb as used */ |
| 1383 | if (!(flags & MSG_PEEK)) { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1384 | |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1385 | /* SOCK_STREAM: re-queue skb if it contains unreceived data */ |
| 1386 | if (sk->sk_type == SOCK_STREAM) { |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1387 | if (copied < rlen) { |
| 1388 | IUCV_SKB_CB(skb)->offset = offset + copied; |
Ursula Braun | 2f139a5 | 2014-03-19 07:57:59 +0100 | [diff] [blame] | 1389 | skb_queue_head(&sk->sk_receive_queue, skb); |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1390 | goto done; |
| 1391 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | kfree_skb(skb); |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 1395 | if (iucv->transport == AF_IUCV_TRANS_HIPER) { |
| 1396 | atomic_inc(&iucv->msg_recv); |
| 1397 | if (atomic_read(&iucv->msg_recv) > iucv->msglimit) { |
| 1398 | WARN_ON(1); |
| 1399 | iucv_sock_close(sk); |
| 1400 | return -EFAULT; |
| 1401 | } |
| 1402 | } |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1403 | |
| 1404 | /* Queue backlog skbs */ |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1405 | spin_lock_bh(&iucv->message_q.lock); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1406 | rskb = skb_dequeue(&iucv->backlog_skb_q); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1407 | while (rskb) { |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 1408 | IUCV_SKB_CB(rskb)->offset = 0; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1409 | if (sock_queue_rcv_skb(sk, rskb)) { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1410 | skb_queue_head(&iucv->backlog_skb_q, |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1411 | rskb); |
| 1412 | break; |
| 1413 | } else { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1414 | rskb = skb_dequeue(&iucv->backlog_skb_q); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1415 | } |
| 1416 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1417 | if (skb_queue_empty(&iucv->backlog_skb_q)) { |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1418 | if (!list_empty(&iucv->message_q.list)) |
| 1419 | iucv_process_message_q(sk); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1420 | if (atomic_read(&iucv->msg_recv) >= |
| 1421 | iucv->msglimit / 2) { |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 1422 | err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1423 | if (err) { |
| 1424 | sk->sk_state = IUCV_DISCONN; |
| 1425 | sk->sk_state_change(sk); |
| 1426 | } |
| 1427 | } |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1428 | } |
Hendrik Brueckner | bf95d20 | 2009-09-16 04:37:28 +0000 | [diff] [blame] | 1429 | spin_unlock_bh(&iucv->message_q.lock); |
Hendrik Brueckner | 60d3705 | 2009-04-21 06:04:21 +0000 | [diff] [blame] | 1430 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1431 | |
| 1432 | done: |
Hendrik Brueckner | aa8e71f | 2009-04-21 23:26:25 +0000 | [diff] [blame] | 1433 | /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ |
| 1434 | if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) |
| 1435 | copied = rlen; |
| 1436 | |
| 1437 | return copied; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static inline unsigned int iucv_accept_poll(struct sock *parent) |
| 1441 | { |
| 1442 | struct iucv_sock *isk, *n; |
| 1443 | struct sock *sk; |
| 1444 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1445 | 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] | 1446 | sk = (struct sock *) isk; |
| 1447 | |
| 1448 | if (sk->sk_state == IUCV_CONNECTED) |
| 1449 | return POLLIN | POLLRDNORM; |
| 1450 | } |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | unsigned int iucv_sock_poll(struct file *file, struct socket *sock, |
| 1456 | poll_table *wait) |
| 1457 | { |
| 1458 | struct sock *sk = sock->sk; |
| 1459 | unsigned int mask = 0; |
| 1460 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1461 | sock_poll_wait(file, sk_sleep(sk), wait); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1462 | |
| 1463 | if (sk->sk_state == IUCV_LISTEN) |
| 1464 | return iucv_accept_poll(sk); |
| 1465 | |
| 1466 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 1467 | mask |= POLLERR | |
Jacob Keller | 8facd5f | 2013-04-02 13:55:40 -0700 | [diff] [blame] | 1468 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1469 | |
| 1470 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1471 | mask |= POLLRDHUP; |
| 1472 | |
| 1473 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 1474 | mask |= POLLHUP; |
| 1475 | |
| 1476 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1477 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1478 | mask |= POLLIN | POLLRDNORM; |
| 1479 | |
| 1480 | if (sk->sk_state == IUCV_CLOSED) |
| 1481 | mask |= POLLHUP; |
| 1482 | |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 1483 | if (sk->sk_state == IUCV_DISCONN) |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1484 | mask |= POLLIN; |
| 1485 | |
Ursula Braun | 7f1b0ea | 2012-02-08 00:19:46 +0000 | [diff] [blame] | 1486 | if (sock_writeable(sk) && iucv_below_msglim(sk)) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1487 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 1488 | else |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1489 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1490 | |
| 1491 | return mask; |
| 1492 | } |
| 1493 | |
| 1494 | static int iucv_sock_shutdown(struct socket *sock, int how) |
| 1495 | { |
| 1496 | struct sock *sk = sock->sk; |
| 1497 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1498 | struct iucv_message txmsg; |
| 1499 | int err = 0; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1500 | |
| 1501 | how++; |
| 1502 | |
| 1503 | if ((how & ~SHUTDOWN_MASK) || !how) |
| 1504 | return -EINVAL; |
| 1505 | |
| 1506 | lock_sock(sk); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1507 | switch (sk->sk_state) { |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1508 | case IUCV_LISTEN: |
Hendrik Brueckner | e14ad5f | 2009-04-21 06:04:23 +0000 | [diff] [blame] | 1509 | case IUCV_DISCONN: |
| 1510 | case IUCV_CLOSING: |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1511 | case IUCV_CLOSED: |
| 1512 | err = -ENOTCONN; |
| 1513 | goto fail; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1514 | default: |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) { |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1519 | if (iucv->transport == AF_IUCV_TRANS_IUCV) { |
| 1520 | txmsg.class = 0; |
| 1521 | txmsg.tag = 0; |
| 1522 | err = pr_iucv->message_send(iucv->path, &txmsg, |
| 1523 | IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8); |
| 1524 | if (err) { |
| 1525 | switch (err) { |
| 1526 | case 1: |
| 1527 | err = -ENOTCONN; |
| 1528 | break; |
| 1529 | case 2: |
| 1530 | err = -ECONNRESET; |
| 1531 | break; |
| 1532 | default: |
| 1533 | err = -ENOTCONN; |
| 1534 | break; |
| 1535 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1536 | } |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1537 | } else |
| 1538 | iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1539 | } |
| 1540 | |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1541 | sk->sk_shutdown |= how; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1542 | if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) { |
Ursula Braun | 1042cab | 2014-07-21 12:54:44 +0200 | [diff] [blame] | 1543 | if ((iucv->transport == AF_IUCV_TRANS_IUCV) && |
| 1544 | iucv->path) { |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 1545 | err = pr_iucv->path_quiesce(iucv->path, NULL); |
| 1546 | if (err) |
| 1547 | err = -ENOTCONN; |
| 1548 | /* skb_queue_purge(&sk->sk_receive_queue); */ |
| 1549 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1550 | skb_queue_purge(&sk->sk_receive_queue); |
| 1551 | } |
| 1552 | |
| 1553 | /* Wake up anyone sleeping in poll */ |
| 1554 | sk->sk_state_change(sk); |
| 1555 | |
| 1556 | fail: |
| 1557 | release_sock(sk); |
| 1558 | return err; |
| 1559 | } |
| 1560 | |
| 1561 | static int iucv_sock_release(struct socket *sock) |
| 1562 | { |
| 1563 | struct sock *sk = sock->sk; |
| 1564 | int err = 0; |
| 1565 | |
| 1566 | if (!sk) |
| 1567 | return 0; |
| 1568 | |
| 1569 | iucv_sock_close(sk); |
| 1570 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1571 | sock_orphan(sk); |
| 1572 | iucv_sock_kill(sk); |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1576 | /* getsockopt and setsockopt */ |
| 1577 | 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] | 1578 | char __user *optval, unsigned int optlen) |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1579 | { |
| 1580 | struct sock *sk = sock->sk; |
| 1581 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1582 | int val; |
| 1583 | int rc; |
| 1584 | |
| 1585 | if (level != SOL_IUCV) |
| 1586 | return -ENOPROTOOPT; |
| 1587 | |
| 1588 | if (optlen < sizeof(int)) |
| 1589 | return -EINVAL; |
| 1590 | |
| 1591 | if (get_user(val, (int __user *) optval)) |
| 1592 | return -EFAULT; |
| 1593 | |
| 1594 | rc = 0; |
| 1595 | |
| 1596 | lock_sock(sk); |
| 1597 | switch (optname) { |
| 1598 | case SO_IPRMDATA_MSG: |
| 1599 | if (val) |
| 1600 | iucv->flags |= IUCV_IPRMDATA; |
| 1601 | else |
| 1602 | iucv->flags &= ~IUCV_IPRMDATA; |
| 1603 | break; |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1604 | case SO_MSGLIMIT: |
| 1605 | switch (sk->sk_state) { |
| 1606 | case IUCV_OPEN: |
| 1607 | case IUCV_BOUND: |
| 1608 | if (val < 1 || val > (u16)(~0)) |
| 1609 | rc = -EINVAL; |
| 1610 | else |
| 1611 | iucv->msglimit = val; |
| 1612 | break; |
| 1613 | default: |
| 1614 | rc = -EINVAL; |
| 1615 | break; |
| 1616 | } |
| 1617 | break; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1618 | default: |
| 1619 | rc = -ENOPROTOOPT; |
| 1620 | break; |
| 1621 | } |
| 1622 | release_sock(sk); |
| 1623 | |
| 1624 | return rc; |
| 1625 | } |
| 1626 | |
| 1627 | static int iucv_sock_getsockopt(struct socket *sock, int level, int optname, |
| 1628 | char __user *optval, int __user *optlen) |
| 1629 | { |
| 1630 | struct sock *sk = sock->sk; |
| 1631 | struct iucv_sock *iucv = iucv_sk(sk); |
Ursula Braun | 51363b8 | 2012-02-08 00:19:48 +0000 | [diff] [blame] | 1632 | unsigned int val; |
| 1633 | int len; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (level != SOL_IUCV) |
| 1636 | return -ENOPROTOOPT; |
| 1637 | |
| 1638 | if (get_user(len, optlen)) |
| 1639 | return -EFAULT; |
| 1640 | |
| 1641 | if (len < 0) |
| 1642 | return -EINVAL; |
| 1643 | |
| 1644 | len = min_t(unsigned int, len, sizeof(int)); |
| 1645 | |
| 1646 | switch (optname) { |
| 1647 | case SO_IPRMDATA_MSG: |
| 1648 | val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0; |
| 1649 | break; |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1650 | case SO_MSGLIMIT: |
| 1651 | lock_sock(sk); |
| 1652 | val = (iucv->path != NULL) ? iucv->path->msglim /* connected */ |
| 1653 | : iucv->msglimit; /* default */ |
| 1654 | release_sock(sk); |
| 1655 | break; |
Ursula Braun | 51363b8 | 2012-02-08 00:19:48 +0000 | [diff] [blame] | 1656 | case SO_MSGSIZE: |
| 1657 | if (sk->sk_state == IUCV_OPEN) |
| 1658 | return -EBADFD; |
| 1659 | val = (iucv->hs_dev) ? iucv->hs_dev->mtu - |
| 1660 | sizeof(struct af_iucv_trans_hdr) - ETH_HLEN : |
| 1661 | 0x7fffffff; |
| 1662 | break; |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 1663 | default: |
| 1664 | return -ENOPROTOOPT; |
| 1665 | } |
| 1666 | |
| 1667 | if (put_user(len, optlen)) |
| 1668 | return -EFAULT; |
| 1669 | if (copy_to_user(optval, &val, len)) |
| 1670 | return -EFAULT; |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1676 | /* Callback wrappers - called from iucv base support */ |
| 1677 | static int iucv_callback_connreq(struct iucv_path *path, |
| 1678 | u8 ipvmid[8], u8 ipuser[16]) |
| 1679 | { |
| 1680 | unsigned char user_data[16]; |
| 1681 | unsigned char nuser_data[16]; |
| 1682 | unsigned char src_name[8]; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1683 | struct sock *sk, *nsk; |
| 1684 | struct iucv_sock *iucv, *niucv; |
| 1685 | int err; |
| 1686 | |
| 1687 | memcpy(src_name, ipuser, 8); |
| 1688 | EBCASC(src_name, 8); |
| 1689 | /* Find out if this path belongs to af_iucv. */ |
| 1690 | read_lock(&iucv_sk_list.lock); |
| 1691 | iucv = NULL; |
Ursula Braun | febca28 | 2007-07-14 19:04:25 -0700 | [diff] [blame] | 1692 | sk = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1693 | sk_for_each(sk, &iucv_sk_list.head) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1694 | if (sk->sk_state == IUCV_LISTEN && |
| 1695 | !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) { |
| 1696 | /* |
| 1697 | * Found a listening socket with |
| 1698 | * src_name == ipuser[0-7]. |
| 1699 | */ |
| 1700 | iucv = iucv_sk(sk); |
| 1701 | break; |
| 1702 | } |
| 1703 | read_unlock(&iucv_sk_list.lock); |
| 1704 | if (!iucv) |
| 1705 | /* No socket found, not one of our paths. */ |
| 1706 | return -EINVAL; |
| 1707 | |
| 1708 | bh_lock_sock(sk); |
| 1709 | |
| 1710 | /* Check if parent socket is listening */ |
| 1711 | low_nmcpy(user_data, iucv->src_name); |
| 1712 | high_nmcpy(user_data, iucv->dst_name); |
| 1713 | ASCEBC(user_data, sizeof(user_data)); |
| 1714 | if (sk->sk_state != IUCV_LISTEN) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1715 | err = pr_iucv->path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1716 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1717 | goto fail; |
| 1718 | } |
| 1719 | |
| 1720 | /* Check for backlog size */ |
| 1721 | if (sk_acceptq_is_full(sk)) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1722 | err = pr_iucv->path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1723 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1724 | goto fail; |
| 1725 | } |
| 1726 | |
| 1727 | /* Create the new socket */ |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1728 | nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1729 | if (!nsk) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1730 | err = pr_iucv->path_sever(path, user_data); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1731 | iucv_path_free(path); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1732 | goto fail; |
| 1733 | } |
| 1734 | |
| 1735 | niucv = iucv_sk(nsk); |
| 1736 | iucv_sock_init(nsk, sk); |
| 1737 | |
| 1738 | /* Set the new iucv_sock */ |
| 1739 | memcpy(niucv->dst_name, ipuser + 8, 8); |
| 1740 | EBCASC(niucv->dst_name, 8); |
| 1741 | memcpy(niucv->dst_user_id, ipvmid, 8); |
| 1742 | memcpy(niucv->src_name, iucv->src_name, 8); |
| 1743 | memcpy(niucv->src_user_id, iucv->src_user_id, 8); |
| 1744 | niucv->path = path; |
| 1745 | |
| 1746 | /* Call iucv_accept */ |
| 1747 | high_nmcpy(nuser_data, ipuser + 8); |
| 1748 | memcpy(nuser_data + 8, niucv->src_name, 8); |
| 1749 | ASCEBC(nuser_data + 8, 8); |
| 1750 | |
Hendrik Brueckner | 09488e2 | 2009-04-21 23:26:27 +0000 | [diff] [blame] | 1751 | /* set message limit for path based on msglimit of accepting socket */ |
| 1752 | niucv->msglimit = iucv->msglimit; |
| 1753 | path->msglim = iucv->msglimit; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1754 | err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk); |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1755 | if (err) { |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 1756 | iucv_sever_path(nsk, 1); |
Hendrik Brueckner | 65dbd7c | 2009-01-05 18:08:23 -0800 | [diff] [blame] | 1757 | iucv_sock_kill(nsk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1758 | goto fail; |
| 1759 | } |
| 1760 | |
| 1761 | iucv_accept_enqueue(sk, nsk); |
| 1762 | |
| 1763 | /* Wake up accept */ |
| 1764 | nsk->sk_state = IUCV_CONNECTED; |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1765 | sk->sk_data_ready(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1766 | err = 0; |
| 1767 | fail: |
| 1768 | bh_unlock_sock(sk); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16]) |
| 1773 | { |
| 1774 | struct sock *sk = path->private; |
| 1775 | |
| 1776 | sk->sk_state = IUCV_CONNECTED; |
| 1777 | sk->sk_state_change(sk); |
| 1778 | } |
| 1779 | |
| 1780 | static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg) |
| 1781 | { |
| 1782 | struct sock *sk = path->private; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1783 | struct iucv_sock *iucv = iucv_sk(sk); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1784 | struct sk_buff *skb; |
| 1785 | struct sock_msg_q *save_msg; |
| 1786 | int len; |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1787 | |
Hendrik Brueckner | fe86e54 | 2009-04-21 06:04:22 +0000 | [diff] [blame] | 1788 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 1789 | pr_iucv->message_reject(path, msg); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1790 | return; |
Hendrik Brueckner | fe86e54 | 2009-04-21 06:04:22 +0000 | [diff] [blame] | 1791 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1792 | |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1793 | spin_lock(&iucv->message_q.lock); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1794 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1795 | if (!list_empty(&iucv->message_q.list) || |
| 1796 | !skb_queue_empty(&iucv->backlog_skb_q)) |
| 1797 | goto save_message; |
| 1798 | |
| 1799 | len = atomic_read(&sk->sk_rmem_alloc); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 1800 | len += SKB_TRUESIZE(iucv_msg_length(msg)); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1801 | if (len > sk->sk_rcvbuf) |
| 1802 | goto save_message; |
| 1803 | |
Hendrik Brueckner | b8942e3 | 2009-04-21 23:26:23 +0000 | [diff] [blame] | 1804 | skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1805 | if (!skb) |
| 1806 | goto save_message; |
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 | iucv_process_message(sk, skb, path, msg); |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1809 | goto out_unlock; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1810 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1811 | save_message: |
| 1812 | save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA); |
Ursula Braun | d444472 | 2008-02-07 18:07:19 -0800 | [diff] [blame] | 1813 | if (!save_msg) |
Julia Lawall | a56635a | 2010-05-26 05:56:48 +0000 | [diff] [blame] | 1814 | goto out_unlock; |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1815 | save_msg->path = path; |
| 1816 | save_msg->msg = *msg; |
| 1817 | |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1818 | list_add_tail(&save_msg->list, &iucv->message_q.list); |
Hendrik Brueckner | 3fa6b5a | 2009-04-21 06:04:24 +0000 | [diff] [blame] | 1819 | |
| 1820 | out_unlock: |
Ursula Braun | f0703c8 | 2007-10-08 02:03:31 -0700 | [diff] [blame] | 1821 | spin_unlock(&iucv->message_q.lock); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | static void iucv_callback_txdone(struct iucv_path *path, |
| 1825 | struct iucv_message *msg) |
| 1826 | { |
| 1827 | struct sock *sk = path->private; |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1828 | struct sk_buff *this = NULL; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1829 | struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q; |
| 1830 | struct sk_buff *list_skb = list->next; |
| 1831 | unsigned long flags; |
| 1832 | |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 1833 | bh_lock_sock(sk); |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1834 | if (!skb_queue_empty(list)) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1835 | spin_lock_irqsave(&list->lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1836 | |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1837 | while (list_skb != (struct sk_buff *)list) { |
Ursula Braun | f5738e2 | 2014-05-13 14:38:02 +0200 | [diff] [blame] | 1838 | if (msg->tag == IUCV_SKB_CB(list_skb)->tag) { |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1839 | this = list_skb; |
| 1840 | break; |
| 1841 | } |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1842 | list_skb = list_skb->next; |
Ursula Braun | f2a7799 | 2008-02-07 18:07:44 -0800 | [diff] [blame] | 1843 | } |
| 1844 | if (this) |
| 1845 | __skb_unlink(this, list); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1846 | |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1847 | spin_unlock_irqrestore(&list->lock, flags); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1848 | |
Hendrik Brueckner | 0ea920d | 2009-06-17 21:54:48 +0000 | [diff] [blame] | 1849 | if (this) { |
| 1850 | kfree_skb(this); |
| 1851 | /* wake up any process waiting for sending */ |
| 1852 | iucv_sock_wake_msglim(sk); |
| 1853 | } |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 1856 | if (sk->sk_state == IUCV_CLOSING) { |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1857 | if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { |
| 1858 | sk->sk_state = IUCV_CLOSED; |
| 1859 | sk->sk_state_change(sk); |
| 1860 | } |
| 1861 | } |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 1862 | bh_unlock_sock(sk); |
Jennifer Hunt | 561e036 | 2007-05-04 12:22:07 -0700 | [diff] [blame] | 1863 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16]) |
| 1867 | { |
| 1868 | struct sock *sk = path->private; |
| 1869 | |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 1870 | if (sk->sk_state == IUCV_CLOSED) |
| 1871 | return; |
| 1872 | |
| 1873 | bh_lock_sock(sk); |
| 1874 | iucv_sever_path(sk, 1); |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 1875 | sk->sk_state = IUCV_DISCONN; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1876 | |
| 1877 | sk->sk_state_change(sk); |
Ursula Braun | 7d316b9 | 2012-02-08 00:19:45 +0000 | [diff] [blame] | 1878 | bh_unlock_sock(sk); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 1879 | } |
| 1880 | |
Hendrik Brueckner | af88b52 | 2009-04-21 23:26:21 +0000 | [diff] [blame] | 1881 | /* called if the other communication side shuts down its RECV direction; |
| 1882 | * in turn, the callback sets SEND_SHUTDOWN to disable sending of data. |
| 1883 | */ |
| 1884 | static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16]) |
| 1885 | { |
| 1886 | struct sock *sk = path->private; |
| 1887 | |
| 1888 | bh_lock_sock(sk); |
| 1889 | if (sk->sk_state != IUCV_CLOSED) { |
| 1890 | sk->sk_shutdown |= SEND_SHUTDOWN; |
| 1891 | sk->sk_state_change(sk); |
| 1892 | } |
| 1893 | bh_unlock_sock(sk); |
| 1894 | } |
| 1895 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1896 | /***************** HiperSockets transport callbacks ********************/ |
| 1897 | static void afiucv_swap_src_dest(struct sk_buff *skb) |
| 1898 | { |
| 1899 | struct af_iucv_trans_hdr *trans_hdr = |
| 1900 | (struct af_iucv_trans_hdr *)skb->data; |
| 1901 | char tmpID[8]; |
| 1902 | char tmpName[8]; |
| 1903 | |
| 1904 | ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); |
| 1905 | ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); |
| 1906 | ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); |
| 1907 | ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); |
| 1908 | memcpy(tmpID, trans_hdr->srcUserID, 8); |
| 1909 | memcpy(tmpName, trans_hdr->srcAppName, 8); |
| 1910 | memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8); |
| 1911 | memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8); |
| 1912 | memcpy(trans_hdr->destUserID, tmpID, 8); |
| 1913 | memcpy(trans_hdr->destAppName, tmpName, 8); |
| 1914 | skb_push(skb, ETH_HLEN); |
| 1915 | memset(skb->data, 0, ETH_HLEN); |
| 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * afiucv_hs_callback_syn - react on received SYN |
| 1920 | **/ |
| 1921 | static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb) |
| 1922 | { |
| 1923 | struct sock *nsk; |
| 1924 | struct iucv_sock *iucv, *niucv; |
| 1925 | struct af_iucv_trans_hdr *trans_hdr; |
| 1926 | int err; |
| 1927 | |
| 1928 | iucv = iucv_sk(sk); |
| 1929 | trans_hdr = (struct af_iucv_trans_hdr *)skb->data; |
| 1930 | if (!iucv) { |
| 1931 | /* no sock - connection refused */ |
| 1932 | afiucv_swap_src_dest(skb); |
| 1933 | trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; |
| 1934 | err = dev_queue_xmit(skb); |
| 1935 | goto out; |
| 1936 | } |
| 1937 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1938 | nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1939 | bh_lock_sock(sk); |
| 1940 | if ((sk->sk_state != IUCV_LISTEN) || |
| 1941 | sk_acceptq_is_full(sk) || |
| 1942 | !nsk) { |
| 1943 | /* error on server socket - connection refused */ |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1944 | afiucv_swap_src_dest(skb); |
| 1945 | trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; |
| 1946 | err = dev_queue_xmit(skb); |
Ursula Braun | 4d520f6 | 2014-05-28 10:22:29 +0200 | [diff] [blame] | 1947 | iucv_sock_kill(nsk); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1948 | bh_unlock_sock(sk); |
| 1949 | goto out; |
| 1950 | } |
| 1951 | |
| 1952 | niucv = iucv_sk(nsk); |
| 1953 | iucv_sock_init(nsk, sk); |
| 1954 | niucv->transport = AF_IUCV_TRANS_HIPER; |
| 1955 | niucv->msglimit = iucv->msglimit; |
| 1956 | if (!trans_hdr->window) |
| 1957 | niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT; |
| 1958 | else |
| 1959 | niucv->msglimit_peer = trans_hdr->window; |
| 1960 | memcpy(niucv->dst_name, trans_hdr->srcAppName, 8); |
| 1961 | memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8); |
| 1962 | memcpy(niucv->src_name, iucv->src_name, 8); |
| 1963 | memcpy(niucv->src_user_id, iucv->src_user_id, 8); |
| 1964 | nsk->sk_bound_dev_if = sk->sk_bound_dev_if; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 1965 | niucv->hs_dev = iucv->hs_dev; |
| 1966 | dev_hold(niucv->hs_dev); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1967 | afiucv_swap_src_dest(skb); |
| 1968 | trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK; |
| 1969 | trans_hdr->window = niucv->msglimit; |
| 1970 | /* if receiver acks the xmit connection is established */ |
| 1971 | err = dev_queue_xmit(skb); |
| 1972 | if (!err) { |
| 1973 | iucv_accept_enqueue(sk, nsk); |
| 1974 | nsk->sk_state = IUCV_CONNECTED; |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1975 | sk->sk_data_ready(sk); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 1976 | } else |
| 1977 | iucv_sock_kill(nsk); |
| 1978 | bh_unlock_sock(sk); |
| 1979 | |
| 1980 | out: |
| 1981 | return NET_RX_SUCCESS; |
| 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * afiucv_hs_callback_synack() - react on received SYN-ACK |
| 1986 | **/ |
| 1987 | static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb) |
| 1988 | { |
| 1989 | struct iucv_sock *iucv = iucv_sk(sk); |
| 1990 | struct af_iucv_trans_hdr *trans_hdr = |
| 1991 | (struct af_iucv_trans_hdr *)skb->data; |
| 1992 | |
| 1993 | if (!iucv) |
| 1994 | goto out; |
| 1995 | if (sk->sk_state != IUCV_BOUND) |
| 1996 | goto out; |
| 1997 | bh_lock_sock(sk); |
| 1998 | iucv->msglimit_peer = trans_hdr->window; |
| 1999 | sk->sk_state = IUCV_CONNECTED; |
| 2000 | sk->sk_state_change(sk); |
| 2001 | bh_unlock_sock(sk); |
| 2002 | out: |
| 2003 | kfree_skb(skb); |
| 2004 | return NET_RX_SUCCESS; |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * afiucv_hs_callback_synfin() - react on received SYN_FIN |
| 2009 | **/ |
| 2010 | static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb) |
| 2011 | { |
| 2012 | struct iucv_sock *iucv = iucv_sk(sk); |
| 2013 | |
| 2014 | if (!iucv) |
| 2015 | goto out; |
| 2016 | if (sk->sk_state != IUCV_BOUND) |
| 2017 | goto out; |
| 2018 | bh_lock_sock(sk); |
| 2019 | sk->sk_state = IUCV_DISCONN; |
| 2020 | sk->sk_state_change(sk); |
| 2021 | bh_unlock_sock(sk); |
| 2022 | out: |
| 2023 | kfree_skb(skb); |
| 2024 | return NET_RX_SUCCESS; |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * afiucv_hs_callback_fin() - react on received FIN |
| 2029 | **/ |
| 2030 | static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb) |
| 2031 | { |
| 2032 | struct iucv_sock *iucv = iucv_sk(sk); |
| 2033 | |
| 2034 | /* other end of connection closed */ |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2035 | if (!iucv) |
| 2036 | goto out; |
| 2037 | bh_lock_sock(sk); |
| 2038 | if (sk->sk_state == IUCV_CONNECTED) { |
Ursula Braun | aac6399 | 2011-12-19 22:56:31 +0000 | [diff] [blame] | 2039 | sk->sk_state = IUCV_DISCONN; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2040 | sk->sk_state_change(sk); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2041 | } |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2042 | bh_unlock_sock(sk); |
| 2043 | out: |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2044 | kfree_skb(skb); |
| 2045 | return NET_RX_SUCCESS; |
| 2046 | } |
| 2047 | |
| 2048 | /** |
| 2049 | * afiucv_hs_callback_win() - react on received WIN |
| 2050 | **/ |
| 2051 | static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb) |
| 2052 | { |
| 2053 | struct iucv_sock *iucv = iucv_sk(sk); |
| 2054 | struct af_iucv_trans_hdr *trans_hdr = |
| 2055 | (struct af_iucv_trans_hdr *)skb->data; |
| 2056 | |
| 2057 | if (!iucv) |
| 2058 | return NET_RX_SUCCESS; |
| 2059 | |
| 2060 | if (sk->sk_state != IUCV_CONNECTED) |
| 2061 | return NET_RX_SUCCESS; |
| 2062 | |
| 2063 | atomic_sub(trans_hdr->window, &iucv->msg_sent); |
| 2064 | iucv_sock_wake_msglim(sk); |
| 2065 | return NET_RX_SUCCESS; |
| 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * afiucv_hs_callback_rx() - react on received data |
| 2070 | **/ |
| 2071 | static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb) |
| 2072 | { |
| 2073 | struct iucv_sock *iucv = iucv_sk(sk); |
| 2074 | |
| 2075 | if (!iucv) { |
| 2076 | kfree_skb(skb); |
| 2077 | return NET_RX_SUCCESS; |
| 2078 | } |
| 2079 | |
| 2080 | if (sk->sk_state != IUCV_CONNECTED) { |
| 2081 | kfree_skb(skb); |
| 2082 | return NET_RX_SUCCESS; |
| 2083 | } |
| 2084 | |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 2085 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
| 2086 | kfree_skb(skb); |
| 2087 | return NET_RX_SUCCESS; |
| 2088 | } |
| 2089 | |
Eugene Crosser | 979f66b | 2015-12-11 12:27:51 +0100 | [diff] [blame] | 2090 | /* write stuff from iucv_msg to skb cb */ |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2091 | skb_pull(skb, sizeof(struct af_iucv_trans_hdr)); |
| 2092 | skb_reset_transport_header(skb); |
| 2093 | skb_reset_network_header(skb); |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 2094 | IUCV_SKB_CB(skb)->offset = 0; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2095 | spin_lock(&iucv->message_q.lock); |
| 2096 | if (skb_queue_empty(&iucv->backlog_skb_q)) { |
| 2097 | if (sock_queue_rcv_skb(sk, skb)) { |
| 2098 | /* handle rcv queue full */ |
| 2099 | skb_queue_tail(&iucv->backlog_skb_q, skb); |
| 2100 | } |
| 2101 | } else |
| 2102 | skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb); |
| 2103 | spin_unlock(&iucv->message_q.lock); |
| 2104 | return NET_RX_SUCCESS; |
| 2105 | } |
| 2106 | |
| 2107 | /** |
| 2108 | * afiucv_hs_rcv() - base function for arriving data through HiperSockets |
| 2109 | * transport |
| 2110 | * called from netif RX softirq |
| 2111 | **/ |
| 2112 | static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev, |
| 2113 | struct packet_type *pt, struct net_device *orig_dev) |
| 2114 | { |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2115 | struct sock *sk; |
| 2116 | struct iucv_sock *iucv; |
| 2117 | struct af_iucv_trans_hdr *trans_hdr; |
| 2118 | char nullstring[8]; |
| 2119 | int err = 0; |
| 2120 | |
Eugene Crosser | 979f66b | 2015-12-11 12:27:51 +0100 | [diff] [blame] | 2121 | if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) { |
| 2122 | WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d", |
| 2123 | (int)skb->len, |
| 2124 | (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr))); |
| 2125 | kfree_skb(skb); |
| 2126 | return NET_RX_SUCCESS; |
| 2127 | } |
| 2128 | if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) |
| 2129 | if (skb_linearize(skb)) { |
| 2130 | WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d", |
| 2131 | (int)skb->len); |
| 2132 | kfree_skb(skb); |
| 2133 | return NET_RX_SUCCESS; |
| 2134 | } |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2135 | skb_pull(skb, ETH_HLEN); |
| 2136 | trans_hdr = (struct af_iucv_trans_hdr *)skb->data; |
| 2137 | EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); |
| 2138 | EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); |
| 2139 | EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); |
| 2140 | EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); |
| 2141 | memset(nullstring, 0, sizeof(nullstring)); |
| 2142 | iucv = NULL; |
| 2143 | sk = NULL; |
| 2144 | read_lock(&iucv_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2145 | sk_for_each(sk, &iucv_sk_list.head) { |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2146 | if (trans_hdr->flags == AF_IUCV_FLAG_SYN) { |
| 2147 | if ((!memcmp(&iucv_sk(sk)->src_name, |
| 2148 | trans_hdr->destAppName, 8)) && |
| 2149 | (!memcmp(&iucv_sk(sk)->src_user_id, |
| 2150 | trans_hdr->destUserID, 8)) && |
| 2151 | (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) && |
| 2152 | (!memcmp(&iucv_sk(sk)->dst_user_id, |
| 2153 | nullstring, 8))) { |
| 2154 | iucv = iucv_sk(sk); |
| 2155 | break; |
| 2156 | } |
| 2157 | } else { |
| 2158 | if ((!memcmp(&iucv_sk(sk)->src_name, |
| 2159 | trans_hdr->destAppName, 8)) && |
| 2160 | (!memcmp(&iucv_sk(sk)->src_user_id, |
| 2161 | trans_hdr->destUserID, 8)) && |
| 2162 | (!memcmp(&iucv_sk(sk)->dst_name, |
| 2163 | trans_hdr->srcAppName, 8)) && |
| 2164 | (!memcmp(&iucv_sk(sk)->dst_user_id, |
| 2165 | trans_hdr->srcUserID, 8))) { |
| 2166 | iucv = iucv_sk(sk); |
| 2167 | break; |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | read_unlock(&iucv_sk_list.lock); |
| 2172 | if (!iucv) |
| 2173 | sk = NULL; |
| 2174 | |
| 2175 | /* no sock |
| 2176 | how should we send with no sock |
| 2177 | 1) send without sock no send rc checking? |
| 2178 | 2) introduce default sock to handle this cases |
| 2179 | |
| 2180 | SYN -> send SYN|ACK in good case, send SYN|FIN in bad case |
| 2181 | data -> send FIN |
| 2182 | SYN|ACK, SYN|FIN, FIN -> no action? */ |
| 2183 | |
| 2184 | switch (trans_hdr->flags) { |
| 2185 | case AF_IUCV_FLAG_SYN: |
| 2186 | /* connect request */ |
| 2187 | err = afiucv_hs_callback_syn(sk, skb); |
| 2188 | break; |
| 2189 | case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK): |
| 2190 | /* connect request confirmed */ |
| 2191 | err = afiucv_hs_callback_synack(sk, skb); |
| 2192 | break; |
| 2193 | case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN): |
| 2194 | /* connect request refused */ |
| 2195 | err = afiucv_hs_callback_synfin(sk, skb); |
| 2196 | break; |
| 2197 | case (AF_IUCV_FLAG_FIN): |
| 2198 | /* close request */ |
| 2199 | err = afiucv_hs_callback_fin(sk, skb); |
| 2200 | break; |
| 2201 | case (AF_IUCV_FLAG_WIN): |
| 2202 | err = afiucv_hs_callback_win(sk, skb); |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2203 | if (skb->len == sizeof(struct af_iucv_trans_hdr)) { |
| 2204 | kfree_skb(skb); |
| 2205 | break; |
| 2206 | } |
Ursula Braun | 82492a3 | 2012-03-07 02:06:24 +0000 | [diff] [blame] | 2207 | /* fall through and receive non-zero length data */ |
| 2208 | case (AF_IUCV_FLAG_SHT): |
| 2209 | /* shutdown request */ |
| 2210 | /* fall through and receive zero length data */ |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2211 | case 0: |
| 2212 | /* plain data frame */ |
Ursula Braun | f9c41a6 | 2013-04-07 22:19:26 +0000 | [diff] [blame] | 2213 | IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2214 | err = afiucv_hs_callback_rx(sk, skb); |
| 2215 | break; |
| 2216 | default: |
| 2217 | ; |
| 2218 | } |
| 2219 | |
| 2220 | return err; |
| 2221 | } |
| 2222 | |
| 2223 | /** |
| 2224 | * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets |
| 2225 | * transport |
| 2226 | **/ |
| 2227 | static void afiucv_hs_callback_txnotify(struct sk_buff *skb, |
| 2228 | enum iucv_tx_notify n) |
| 2229 | { |
| 2230 | struct sock *isk = skb->sk; |
| 2231 | struct sock *sk = NULL; |
| 2232 | struct iucv_sock *iucv = NULL; |
| 2233 | struct sk_buff_head *list; |
| 2234 | struct sk_buff *list_skb; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2235 | struct sk_buff *nskb; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2236 | unsigned long flags; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2237 | |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2238 | read_lock_irqsave(&iucv_sk_list.lock, flags); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2239 | sk_for_each(sk, &iucv_sk_list.head) |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2240 | if (sk == isk) { |
| 2241 | iucv = iucv_sk(sk); |
| 2242 | break; |
| 2243 | } |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2244 | read_unlock_irqrestore(&iucv_sk_list.lock, flags); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2245 | |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2246 | if (!iucv || sock_flag(sk, SOCK_ZAPPED)) |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2247 | return; |
| 2248 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2249 | list = &iucv->send_skb_q; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2250 | spin_lock_irqsave(&list->lock, flags); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2251 | if (skb_queue_empty(list)) |
| 2252 | goto out_unlock; |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2253 | list_skb = list->next; |
| 2254 | nskb = list_skb->next; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2255 | while (list_skb != (struct sk_buff *)list) { |
| 2256 | if (skb_shinfo(list_skb) == skb_shinfo(skb)) { |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2257 | switch (n) { |
| 2258 | case TX_NOTIFY_OK: |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2259 | __skb_unlink(list_skb, list); |
| 2260 | kfree_skb(list_skb); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2261 | iucv_sock_wake_msglim(sk); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2262 | break; |
| 2263 | case TX_NOTIFY_PENDING: |
| 2264 | atomic_inc(&iucv->pendings); |
| 2265 | break; |
| 2266 | case TX_NOTIFY_DELAYED_OK: |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2267 | __skb_unlink(list_skb, list); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2268 | atomic_dec(&iucv->pendings); |
| 2269 | if (atomic_read(&iucv->pendings) <= 0) |
| 2270 | iucv_sock_wake_msglim(sk); |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2271 | kfree_skb(list_skb); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | case TX_NOTIFY_UNREACHABLE: |
| 2274 | case TX_NOTIFY_DELAYED_UNREACHABLE: |
| 2275 | case TX_NOTIFY_TPQFULL: /* not yet used */ |
| 2276 | case TX_NOTIFY_GENERALERROR: |
| 2277 | case TX_NOTIFY_DELAYED_GENERALERROR: |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2278 | __skb_unlink(list_skb, list); |
| 2279 | kfree_skb(list_skb); |
| 2280 | if (sk->sk_state == IUCV_CONNECTED) { |
| 2281 | sk->sk_state = IUCV_DISCONN; |
| 2282 | sk->sk_state_change(sk); |
| 2283 | } |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2284 | break; |
| 2285 | } |
| 2286 | break; |
| 2287 | } |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2288 | list_skb = nskb; |
| 2289 | nskb = nskb->next; |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2290 | } |
Ursula Braun | 800c5eb | 2012-02-08 00:19:47 +0000 | [diff] [blame] | 2291 | out_unlock: |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2292 | spin_unlock_irqrestore(&list->lock, flags); |
| 2293 | |
Ursula Braun | 42bd48e | 2011-12-19 22:56:28 +0000 | [diff] [blame] | 2294 | if (sk->sk_state == IUCV_CLOSING) { |
| 2295 | if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { |
| 2296 | sk->sk_state = IUCV_CLOSED; |
| 2297 | sk->sk_state_change(sk); |
| 2298 | } |
| 2299 | } |
| 2300 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2301 | } |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 2302 | |
| 2303 | /* |
| 2304 | * afiucv_netdev_event: handle netdev notifier chain events |
| 2305 | */ |
| 2306 | static int afiucv_netdev_event(struct notifier_block *this, |
| 2307 | unsigned long event, void *ptr) |
| 2308 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 2309 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 2310 | struct sock *sk; |
| 2311 | struct iucv_sock *iucv; |
| 2312 | |
| 2313 | switch (event) { |
| 2314 | case NETDEV_REBOOT: |
| 2315 | case NETDEV_GOING_DOWN: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2316 | sk_for_each(sk, &iucv_sk_list.head) { |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 2317 | iucv = iucv_sk(sk); |
| 2318 | if ((iucv->hs_dev == event_dev) && |
| 2319 | (sk->sk_state == IUCV_CONNECTED)) { |
| 2320 | if (event == NETDEV_GOING_DOWN) |
| 2321 | iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN); |
| 2322 | sk->sk_state = IUCV_DISCONN; |
| 2323 | sk->sk_state_change(sk); |
| 2324 | } |
| 2325 | } |
| 2326 | break; |
| 2327 | case NETDEV_DOWN: |
| 2328 | case NETDEV_UNREGISTER: |
| 2329 | default: |
| 2330 | break; |
| 2331 | } |
| 2332 | return NOTIFY_DONE; |
| 2333 | } |
| 2334 | |
| 2335 | static struct notifier_block afiucv_netdev_notifier = { |
| 2336 | .notifier_call = afiucv_netdev_event, |
| 2337 | }; |
| 2338 | |
Alexey Dobriyan | 5708e86 | 2009-09-14 12:23:23 +0000 | [diff] [blame] | 2339 | static const struct proto_ops iucv_sock_ops = { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2340 | .family = PF_IUCV, |
| 2341 | .owner = THIS_MODULE, |
| 2342 | .release = iucv_sock_release, |
| 2343 | .bind = iucv_sock_bind, |
| 2344 | .connect = iucv_sock_connect, |
| 2345 | .listen = iucv_sock_listen, |
| 2346 | .accept = iucv_sock_accept, |
| 2347 | .getname = iucv_sock_getname, |
| 2348 | .sendmsg = iucv_sock_sendmsg, |
| 2349 | .recvmsg = iucv_sock_recvmsg, |
| 2350 | .poll = iucv_sock_poll, |
| 2351 | .ioctl = sock_no_ioctl, |
| 2352 | .mmap = sock_no_mmap, |
| 2353 | .socketpair = sock_no_socketpair, |
| 2354 | .shutdown = iucv_sock_shutdown, |
Hendrik Brueckner | 9d5c5d8 | 2009-04-21 23:26:22 +0000 | [diff] [blame] | 2355 | .setsockopt = iucv_sock_setsockopt, |
| 2356 | .getsockopt = iucv_sock_getsockopt, |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2357 | }; |
| 2358 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 2359 | static const struct net_proto_family iucv_sock_family_ops = { |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2360 | .family = AF_IUCV, |
| 2361 | .owner = THIS_MODULE, |
| 2362 | .create = iucv_sock_create, |
| 2363 | }; |
| 2364 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2365 | static struct packet_type iucv_packet_type = { |
| 2366 | .type = cpu_to_be16(ETH_P_AF_IUCV), |
| 2367 | .func = afiucv_hs_rcv, |
| 2368 | }; |
| 2369 | |
| 2370 | static int afiucv_iucv_init(void) |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 2371 | { |
| 2372 | int err; |
| 2373 | |
| 2374 | err = pr_iucv->iucv_register(&af_iucv_handler, 0); |
| 2375 | if (err) |
| 2376 | goto out; |
| 2377 | /* establish dummy device */ |
| 2378 | af_iucv_driver.bus = pr_iucv->bus; |
| 2379 | err = driver_register(&af_iucv_driver); |
| 2380 | if (err) |
| 2381 | goto out_iucv; |
| 2382 | af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 2383 | if (!af_iucv_dev) { |
| 2384 | err = -ENOMEM; |
| 2385 | goto out_driver; |
| 2386 | } |
| 2387 | dev_set_name(af_iucv_dev, "af_iucv"); |
| 2388 | af_iucv_dev->bus = pr_iucv->bus; |
| 2389 | af_iucv_dev->parent = pr_iucv->root; |
| 2390 | af_iucv_dev->release = (void (*)(struct device *))kfree; |
| 2391 | af_iucv_dev->driver = &af_iucv_driver; |
| 2392 | err = device_register(af_iucv_dev); |
| 2393 | if (err) |
| 2394 | goto out_driver; |
| 2395 | return 0; |
| 2396 | |
| 2397 | out_driver: |
| 2398 | driver_unregister(&af_iucv_driver); |
| 2399 | out_iucv: |
| 2400 | pr_iucv->iucv_unregister(&af_iucv_handler, 0); |
| 2401 | out: |
| 2402 | return err; |
| 2403 | } |
| 2404 | |
Heiko Carstens | da99f05 | 2007-05-04 12:23:27 -0700 | [diff] [blame] | 2405 | static int __init afiucv_init(void) |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2406 | { |
| 2407 | int err; |
| 2408 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2409 | if (MACHINE_IS_VM) { |
| 2410 | cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err); |
| 2411 | if (unlikely(err)) { |
| 2412 | WARN_ON(err); |
| 2413 | err = -EPROTONOSUPPORT; |
| 2414 | goto out; |
| 2415 | } |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2416 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2417 | pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv"); |
| 2418 | if (!pr_iucv) { |
| 2419 | printk(KERN_WARNING "iucv_if lookup failed\n"); |
| 2420 | memset(&iucv_userid, 0, sizeof(iucv_userid)); |
| 2421 | } |
| 2422 | } else { |
| 2423 | memset(&iucv_userid, 0, sizeof(iucv_userid)); |
| 2424 | pr_iucv = NULL; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2427 | err = proto_register(&iucv_proto, 0); |
| 2428 | if (err) |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 2429 | goto out; |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2430 | err = sock_register(&iucv_sock_family_ops); |
| 2431 | if (err) |
| 2432 | goto out_proto; |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 2433 | |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2434 | if (pr_iucv) { |
| 2435 | err = afiucv_iucv_init(); |
| 2436 | if (err) |
| 2437 | goto out_sock; |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 2438 | } else |
| 2439 | register_netdevice_notifier(&afiucv_netdev_notifier); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2440 | dev_add_pack(&iucv_packet_type); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2441 | return 0; |
| 2442 | |
Ursula Braun | c23cad9 | 2009-06-16 10:30:44 +0200 | [diff] [blame] | 2443 | out_sock: |
| 2444 | sock_unregister(PF_IUCV); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2445 | out_proto: |
| 2446 | proto_unregister(&iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2447 | out: |
Frank Blaschka | 6fcd61f | 2011-08-08 01:33:51 +0000 | [diff] [blame] | 2448 | if (pr_iucv) |
| 2449 | symbol_put(iucv_if); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2450 | return err; |
| 2451 | } |
| 2452 | |
| 2453 | static void __exit afiucv_exit(void) |
| 2454 | { |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2455 | if (pr_iucv) { |
| 2456 | device_unregister(af_iucv_dev); |
| 2457 | driver_unregister(&af_iucv_driver); |
| 2458 | pr_iucv->iucv_unregister(&af_iucv_handler, 0); |
| 2459 | symbol_put(iucv_if); |
Ursula Braun | 9fbd87d | 2012-03-07 02:06:23 +0000 | [diff] [blame] | 2460 | } else |
| 2461 | unregister_netdevice_notifier(&afiucv_netdev_notifier); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2462 | dev_remove_pack(&iucv_packet_type); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2463 | sock_unregister(PF_IUCV); |
| 2464 | proto_unregister(&iucv_proto); |
Jennifer Hunt | eac3731 | 2007-02-08 13:51:54 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | module_init(afiucv_init); |
| 2468 | module_exit(afiucv_exit); |
| 2469 | |
| 2470 | MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>"); |
| 2471 | MODULE_DESCRIPTION("IUCV Sockets ver " VERSION); |
| 2472 | MODULE_VERSION(VERSION); |
| 2473 | MODULE_LICENSE("GPL"); |
| 2474 | MODULE_ALIAS_NETPROTO(PF_IUCV); |
Ursula Braun | 3881ac4 | 2011-08-08 01:33:54 +0000 | [diff] [blame] | 2475 | |