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