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