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