~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/iucv/af_iucv.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp