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