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