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