12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796 |
- /*
- * IUCV protocol stack for Linux on zSeries
- *
- * Copyright IBM Corp. 2006, 2009
- *
- * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
- * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
- * PM functions:
- * Ursula Braun <ursula.braun@de.ibm.com>
- */
- #define KMSG_COMPONENT "af_iucv"
- #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/list.h>
- #include <linux/errno.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/skbuff.h>
- #include <linux/init.h>
- #include <linux/poll.h>
- #include <net/sock.h>
- #include <asm/ebcdic.h>
- #include <asm/cpcmd.h>
- #include <linux/kmod.h>
- #include <net/iucv/iucv.h>
- #include <net/iucv/af_iucv.h>
- #define VERSION "1.1"
- static char iucv_userid[80];
- static const struct proto_ops iucv_sock_ops;
- static struct proto iucv_proto = {
- .name = "AF_IUCV",
- .owner = THIS_MODULE,
- .obj_size = sizeof(struct iucv_sock),
- };
- /* special AF_IUCV IPRM messages */
- static const u8 iprm_shutdown[8] =
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
- #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
- /* macros to set/get socket control buffer at correct offset */
- #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
- #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
- #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
- #define CB_TRGCLS_LEN (TRGCLS_SIZE)
- #define __iucv_sock_wait(sk, condition, timeo, ret) \
- do { \
- DEFINE_WAIT(__wait); \
- long __timeo = timeo; \
- ret = 0; \
- prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
- while (!(condition)) { \
- if (!__timeo) { \
- ret = -EAGAIN; \
- break; \
- } \
- if (signal_pending(current)) { \
- ret = sock_intr_errno(__timeo); \
- break; \
- } \
- release_sock(sk); \
- __timeo = schedule_timeout(__timeo); \
- lock_sock(sk); \
- ret = sock_error(sk); \
- if (ret) \
- break; \
- } \
- finish_wait(sk_sleep(sk), &__wait); \
- } while (0)
- #define iucv_sock_wait(sk, condition, timeo) \
- ({ \
- int __ret = 0; \
- if (!(condition)) \
- __iucv_sock_wait(sk, condition, timeo, __ret); \
- __ret; \
- })
- static void iucv_sock_kill(struct sock *sk);
- static void iucv_sock_close(struct sock *sk);
- /* Call Back functions */
- static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
- static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
- static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
- static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
- u8 ipuser[16]);
- static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
- static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
- static struct iucv_sock_list iucv_sk_list = {
- .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
- .autobind_name = ATOMIC_INIT(0)
- };
- static struct iucv_handler af_iucv_handler = {
- .path_pending = iucv_callback_connreq,
- .path_complete = iucv_callback_connack,
- .path_severed = iucv_callback_connrej,
- .message_pending = iucv_callback_rx,
- .message_complete = iucv_callback_txdone,
- .path_quiesced = iucv_callback_shutdown,
- };
- static inline void high_nmcpy(unsigned char *dst, char *src)
- {
- memcpy(dst, src, 8);
- }
- static inline void low_nmcpy(unsigned char *dst, char *src)
- {
- memcpy(&dst[8], src, 8);
- }
- static int afiucv_pm_prepare(struct device *dev)
- {
- #ifdef CONFIG_PM_DEBUG
- printk(KERN_WARNING "afiucv_pm_prepare\n");
- #endif
- return 0;
- }
- static void afiucv_pm_complete(struct device *dev)
- {
- #ifdef CONFIG_PM_DEBUG
- printk(KERN_WARNING "afiucv_pm_complete\n");
- #endif
- }
- /**
- * afiucv_pm_freeze() - Freeze PM callback
- * @dev: AFIUCV dummy device
- *
- * Sever all established IUCV communication pathes
- */
- static int afiucv_pm_freeze(struct device *dev)
- {
- struct iucv_sock *iucv;
- struct sock *sk;
- struct hlist_node *node;
- int err = 0;
- #ifdef CONFIG_PM_DEBUG
- printk(KERN_WARNING "afiucv_pm_freeze\n");
- #endif
- read_lock(&iucv_sk_list.lock);
- sk_for_each(sk, node, &iucv_sk_list.head) {
- iucv = iucv_sk(sk);
- skb_queue_purge(&iucv->send_skb_q);
- skb_queue_purge(&iucv->backlog_skb_q);
- switch (sk->sk_state) {
- case IUCV_SEVERED:
- case IUCV_DISCONN:
- case IUCV_CLOSING:
- case IUCV_CONNECTED:
- if (iucv->path) {
- err = iucv_path_sever(iucv->path, NULL);
- iucv_path_free(iucv->path);
- iucv->path = NULL;
- }
- break;
- case IUCV_OPEN:
- case IUCV_BOUND:
- case IUCV_LISTEN:
- case IUCV_CLOSED:
- default:
- break;
- }
- }
- read_unlock(&iucv_sk_list.lock);
- return err;
- }
- /**
- * afiucv_pm_restore_thaw() - Thaw and restore PM callback
- * @dev: AFIUCV dummy device
- *
- * socket clean up after freeze
- */
- static int afiucv_pm_restore_thaw(struct device *dev)
- {
- struct sock *sk;
- struct hlist_node *node;
- #ifdef CONFIG_PM_DEBUG
- printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
- #endif
- read_lock(&iucv_sk_list.lock);
- sk_for_each(sk, node, &iucv_sk_list.head) {
- switch (sk->sk_state) {
- case IUCV_CONNECTED:
- sk->sk_err = EPIPE;
- sk->sk_state = IUCV_DISCONN;
- sk->sk_state_change(sk);
- break;
- case IUCV_DISCONN:
- case IUCV_SEVERED:
- case IUCV_CLOSING:
- case IUCV_LISTEN:
- case IUCV_BOUND:
- case IUCV_OPEN:
- default:
- break;
- }
- }
- read_unlock(&iucv_sk_list.lock);
- return 0;
- }
- static const struct dev_pm_ops afiucv_pm_ops = {
- .prepare = afiucv_pm_prepare,
- .complete = afiucv_pm_complete,
- .freeze = afiucv_pm_freeze,
- .thaw = afiucv_pm_restore_thaw,
- .restore = afiucv_pm_restore_thaw,
- };
- static struct device_driver af_iucv_driver = {
- .owner = THIS_MODULE,
- .name = "afiucv",
- .bus = &iucv_bus,
- .pm = &afiucv_pm_ops,
- };
- /* dummy device used as trigger for PM functions */
- static struct device *af_iucv_dev;
- /**
- * iucv_msg_length() - Returns the length of an iucv message.
- * @msg: Pointer to struct iucv_message, MUST NOT be NULL
- *
- * The function returns the length of the specified iucv message @msg of data
- * stored in a buffer and of data stored in the parameter list (PRMDATA).
- *
- * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
- * data:
- * PRMDATA[0..6] socket data (max 7 bytes);
- * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
- *
- * The socket data length is computed by subtracting the socket data length
- * value from 0xFF.
- * If the socket data len is greater 7, then PRMDATA can be used for special
- * notifications (see iucv_sock_shutdown); and further,
- * if the socket data len is > 7, the function returns 8.
- *
- * Use this function to allocate socket buffers to store iucv message data.
- */
- static inline size_t iucv_msg_length(struct iucv_message *msg)
- {
- size_t datalen;
- if (msg->flags & IUCV_IPRMDATA) {
- datalen = 0xff - msg->rmmsg[7];
- return (datalen < 8) ? datalen : 8;
- }
- return msg->length;
- }
- /**
- * iucv_sock_in_state() - check for specific states
- * @sk: sock structure
- * @state: first iucv sk state
- * @state: second iucv sk state
- *
- * Returns true if the socket in either in the first or second state.
- */
- static int iucv_sock_in_state(struct sock *sk, int state, int state2)
- {
- return (sk->sk_state == state || sk->sk_state == state2);
- }
- /**
- * iucv_below_msglim() - function to check if messages can be sent
- * @sk: sock structure
- *
- * Returns true if the send queue length is lower than the message limit.
- * Always returns true if the socket is not connected (no iucv path for
- * checking the message limit).
- */
- static inline int iucv_below_msglim(struct sock *sk)
- {
- struct iucv_sock *iucv = iucv_sk(sk);
- if (sk->sk_state != IUCV_CONNECTED)
- return 1;
- return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
- }
- /**
- * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
- */
- static void iucv_sock_wake_msglim(struct sock *sk)
- {
- struct socket_wq *wq;
- rcu_read_lock();
- wq = rcu_dereference(sk->sk_wq);
- if (wq_has_sleeper(wq))
- wake_up_interruptible_all(&wq->wait);
- sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
- rcu_read_unlock();
- }
- /* Timers */
- static void iucv_sock_timeout(unsigned long arg)
- {
- struct sock *sk = (struct sock *)arg;
- bh_lock_sock(sk);
- sk->sk_err = ETIMEDOUT;
- sk->sk_state_change(sk);
- bh_unlock_sock(sk);
- iucv_sock_kill(sk);
- sock_put(sk);
- }
- static void iucv_sock_clear_timer(struct sock *sk)
- {
- sk_stop_timer(sk, &sk->sk_timer);
- }
- static struct sock *__iucv_get_sock_by_name(char *nm)
- {
- struct sock *sk;
- struct hlist_node *node;
- sk_for_each(sk, node, &iucv_sk_list.head)
- if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
- return sk;
- return NULL;
- }
- static void iucv_sock_destruct(struct sock *sk)
- {
- skb_queue_purge(&sk->sk_receive_queue);
- skb_queue_purge(&sk->sk_write_queue);
- }
- /* Cleanup Listen */
- static void iucv_sock_cleanup_listen(struct sock *parent)
- {
- struct sock *sk;
- /* Close non-accepted connections */
- while ((sk = iucv_accept_dequeue(parent, NULL))) {
- iucv_sock_close(sk);
- iucv_sock_kill(sk);
- }
- parent->sk_state = IUCV_CLOSED;
- }
- /* Kill socket (only if zapped and orphaned) */
- static void iucv_sock_kill(struct sock *sk)
- {
- if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
- return;
- iucv_sock_unlink(&iucv_sk_list, sk);
- sock_set_flag(sk, SOCK_DEAD);
- sock_put(sk);
- }
- /* Close an IUCV socket */
- static void iucv_sock_close(struct sock *sk)
- {
- unsigned char user_data[16];
- struct iucv_sock *iucv = iucv_sk(sk);
- unsigned long timeo;
- iucv_sock_clear_timer(sk);
- lock_sock(sk);
- switch (sk->sk_state) {
- case IUCV_LISTEN:
- iucv_sock_cleanup_listen(sk);
- break;
- case IUCV_CONNECTED:
- case IUCV_DISCONN:
- sk->sk_state = IUCV_CLOSING;
- sk->sk_state_change(sk);
- if (!skb_queue_empty(&iucv->send_skb_q)) {
- if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
- timeo = sk->sk_lingertime;
- else
- timeo = IUCV_DISCONN_TIMEOUT;
- iucv_sock_wait(sk,
- iucv_sock_in_state(sk, IUCV_CLOSED, 0),
- timeo);
- }
- case IUCV_CLOSING: /* fall through */
- sk->sk_state = IUCV_CLOSED;
- sk->sk_state_change(sk);
- if (iucv->path) {
- low_nmcpy(user_data, iucv->src_name);
- high_nmcpy(user_data, iucv->dst_name);
- ASCEBC(user_data, sizeof(user_data));
- iucv_path_sever(iucv->path, user_data);
- iucv_path_free(iucv->path);
- iucv->path = NULL;
- }
- sk->sk_err = ECONNRESET;
- sk->sk_state_change(sk);
- skb_queue_purge(&iucv->send_skb_q);
- skb_queue_purge(&iucv->backlog_skb_q);
- break;
- default:
- /* nothing to do here */
- break;
- }
- /* mark socket for deletion by iucv_sock_kill() */
- sock_set_flag(sk, SOCK_ZAPPED);
- release_sock(sk);
- }
- static void iucv_sock_init(struct sock *sk, struct sock *parent)
- {
- if (parent)
- sk->sk_type = parent->sk_type;
- }
- static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
- {
- struct sock *sk;
- sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
- if (!sk)
- return NULL;
- sock_init_data(sock, sk);
- INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
- spin_lock_init(&iucv_sk(sk)->accept_q_lock);
- skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
- INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
- spin_lock_init(&iucv_sk(sk)->message_q.lock);
- skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
- iucv_sk(sk)->send_tag = 0;
- iucv_sk(sk)->flags = 0;
- iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
- iucv_sk(sk)->path = NULL;
- memset(&iucv_sk(sk)->src_user_id , 0, 32);
- sk->sk_destruct = iucv_sock_destruct;
- sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
- sk->sk_allocation = GFP_DMA;
- sock_reset_flag(sk, SOCK_ZAPPED);
- sk->sk_protocol = proto;
- sk->sk_state = IUCV_OPEN;
- setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
- iucv_sock_link(&iucv_sk_list, sk);
- return sk;
- }
- /* Create an IUCV socket */
- static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
- int kern)
- {
- struct sock *sk;
- if (protocol && protocol != PF_IUCV)
- return -EPROTONOSUPPORT;
- sock->state = SS_UNCONNECTED;
- switch (sock->type) {
- case SOCK_STREAM:
- sock->ops = &iucv_sock_ops;
- break;
- case SOCK_SEQPACKET:
- /* currently, proto ops can handle both sk types */
- sock->ops = &iucv_sock_ops;
- break;
- default:
- return -ESOCKTNOSUPPORT;
- }
- sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
- if (!sk)
- return -ENOMEM;
- iucv_sock_init(sk, NULL);
- return 0;
- }
- void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
- {
- write_lock_bh(&l->lock);
- sk_add_node(sk, &l->head);
- write_unlock_bh(&l->lock);
- }
- void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
- {
- write_lock_bh(&l->lock);
- sk_del_node_init(sk);
- write_unlock_bh(&l->lock);
- }
- void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
- {
- unsigned long flags;
- struct iucv_sock *par = iucv_sk(parent);
- sock_hold(sk);
- spin_lock_irqsave(&par->accept_q_lock, flags);
- list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
- spin_unlock_irqrestore(&par->accept_q_lock, flags);
- iucv_sk(sk)->parent = parent;
- sk_acceptq_added(parent);
- }
- void iucv_accept_unlink(struct sock *sk)
- {
- unsigned long flags;
- struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
- spin_lock_irqsave(&par->accept_q_lock, flags);
- list_del_init(&iucv_sk(sk)->accept_q);
- spin_unlock_irqrestore(&par->accept_q_lock, flags);
- sk_acceptq_removed(iucv_sk(sk)->parent);
- iucv_sk(sk)->parent = NULL;
- sock_put(sk);
- }
- struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
- {
- struct iucv_sock *isk, *n;
- struct sock *sk;
- list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
- sk = (struct sock *) isk;
- lock_sock(sk);
- if (sk->sk_state == IUCV_CLOSED) {
- iucv_accept_unlink(sk);
- release_sock(sk);
- continue;
- }
- if (sk->sk_state == IUCV_CONNECTED ||
- sk->sk_state == IUCV_SEVERED ||
- sk->sk_state == IUCV_DISCONN || /* due to PM restore */
- !newsock) {
- iucv_accept_unlink(sk);
- if (newsock)
- sock_graft(sk, newsock);
- if (sk->sk_state == IUCV_SEVERED)
- sk->sk_state = IUCV_DISCONN;
- release_sock(sk);
- return sk;
- }
- release_sock(sk);
- }
- return NULL;
- }
- /* Bind an unbound socket */
- static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
- int addr_len)
- {
- struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv;
- int err;
- /* Verify the input sockaddr */
- if (!addr || addr->sa_family != AF_IUCV)
- return -EINVAL;
- lock_sock(sk);
- if (sk->sk_state != IUCV_OPEN) {
- err = -EBADFD;
- goto done;
- }
- write_lock_bh(&iucv_sk_list.lock);
- iucv = iucv_sk(sk);
- if (__iucv_get_sock_by_name(sa->siucv_name)) {
- err = -EADDRINUSE;
- goto done_unlock;
- }
- if (iucv->path) {
- err = 0;
- goto done_unlock;
- }
- /* Bind the socket */
- memcpy(iucv->src_name, sa->siucv_name, 8);
- /* Copy the user id */
- memcpy(iucv->src_user_id, iucv_userid, 8);
- sk->sk_state = IUCV_BOUND;
- err = 0;
- done_unlock:
- /* Release the socket list lock */
- write_unlock_bh(&iucv_sk_list.lock);
- done:
- release_sock(sk);
- return err;
- }
- /* Automatically bind an unbound socket */
- static int iucv_sock_autobind(struct sock *sk)
- {
- struct iucv_sock *iucv = iucv_sk(sk);
- char query_buffer[80];
- char name[12];
- int err = 0;
- /* Set the userid and name */
- cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
- if (unlikely(err))
- return -EPROTO;
- memcpy(iucv->src_user_id, query_buffer, 8);
- write_lock_bh(&iucv_sk_list.lock);
- sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
- while (__iucv_get_sock_by_name(name)) {
- sprintf(name, "%08x",
- atomic_inc_return(&iucv_sk_list.autobind_name));
- }
- write_unlock_bh(&iucv_sk_list.lock);
- memcpy(&iucv->src_name, name, 8);
- return err;
- }
- /* Connect an unconnected socket */
- static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
- int alen, int flags)
- {
- struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv;
- unsigned char user_data[16];
- int err;
- if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
- return -EINVAL;
- if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
- return -EBADFD;
- if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
- return -EINVAL;
- if (sk->sk_state == IUCV_OPEN) {
- err = iucv_sock_autobind(sk);
- if (unlikely(err))
- return err;
- }
- lock_sock(sk);
- /* Set the destination information */
- memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
- memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
- high_nmcpy(user_data, sa->siucv_name);
- low_nmcpy(user_data, iucv_sk(sk)->src_name);
- ASCEBC(user_data, sizeof(user_data));
- iucv = iucv_sk(sk);
- /* Create path. */
- iucv->path = iucv_path_alloc(iucv->msglimit,
- IUCV_IPRMDATA, GFP_KERNEL);
- if (!iucv->path) {
- err = -ENOMEM;
- goto done;
- }
- err = iucv_path_connect(iucv->path, &af_iucv_handler,
- sa->siucv_user_id, NULL, user_data, sk);
- if (err) {
- iucv_path_free(iucv->path);
- iucv->path = NULL;
- switch (err) {
- case 0x0b: /* Target communicator is not logged on */
- err = -ENETUNREACH;
- break;
- case 0x0d: /* Max connections for this guest exceeded */
- case 0x0e: /* Max connections for target guest exceeded */
- err = -EAGAIN;
- break;
- case 0x0f: /* Missing IUCV authorization */
- err = -EACCES;
- break;
- default:
- err = -ECONNREFUSED;
- break;
- }
- goto done;
- }
- if (sk->sk_state != IUCV_CONNECTED) {
- err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
- IUCV_DISCONN),
- sock_sndtimeo(sk, flags & O_NONBLOCK));
- }
- if (sk->sk_state == IUCV_DISCONN) {
- err = -ECONNREFUSED;
- }
- if (err) {
- iucv_path_sever(iucv->path, NULL);
- iucv_path_free(iucv->path);
- iucv->path = NULL;
- }
- done:
- release_sock(sk);
- return err;
- }
- /* Move a socket into listening state. */
- static int iucv_sock_listen(struct socket *sock, int backlog)
- {
- struct sock *sk = sock->sk;
- int err;
- lock_sock(sk);
- err = -EINVAL;
- if (sk->sk_state != IUCV_BOUND)
- goto done;
- if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
- goto done;
- sk->sk_max_ack_backlog = backlog;
- sk->sk_ack_backlog = 0;
- sk->sk_state = IUCV_LISTEN;
- err = 0;
- done:
- release_sock(sk);
- return err;
- }
- /* Accept a pending connection */
- static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
- int flags)
- {
- DECLARE_WAITQUEUE(wait, current);
- struct sock *sk = sock->sk, *nsk;
- long timeo;
- int err = 0;
- lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
- if (sk->sk_state != IUCV_LISTEN) {
- err = -EBADFD;
- goto done;
- }
- timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
- /* Wait for an incoming connection */
- add_wait_queue_exclusive(sk_sleep(sk), &wait);
- while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
- set_current_state(TASK_INTERRUPTIBLE);
- if (!timeo) {
- err = -EAGAIN;
- break;
- }
- release_sock(sk);
- timeo = schedule_timeout(timeo);
- lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
- if (sk->sk_state != IUCV_LISTEN) {
- err = -EBADFD;
- break;
- }
- if (signal_pending(current)) {
- err = sock_intr_errno(timeo);
- break;
- }
- }
- set_current_state(TASK_RUNNING);
- remove_wait_queue(sk_sleep(sk), &wait);
- if (err)
- goto done;
- newsock->state = SS_CONNECTED;
- done:
- release_sock(sk);
- return err;
- }
- static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
- int *len, int peer)
- {
- struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
- struct sock *sk = sock->sk;
- addr->sa_family = AF_IUCV;
- *len = sizeof(struct sockaddr_iucv);
- if (peer) {
- memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
- memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
- } else {
- memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
- memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
- }
- memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
- memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
- memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
- return 0;
- }
- /**
- * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
- * @path: IUCV path
- * @msg: Pointer to a struct iucv_message
- * @skb: The socket data to send, skb->len MUST BE <= 7
- *
- * Send the socket data in the parameter list in the iucv message
- * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
- * list and the socket data len at index 7 (last byte).
- * See also iucv_msg_length().
- *
- * Returns the error code from the iucv_message_send() call.
- */
- static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
- struct sk_buff *skb)
- {
- u8 prmdata[8];
- memcpy(prmdata, (void *) skb->data, skb->len);
- prmdata[7] = 0xff - (u8) skb->len;
- return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
- (void *) prmdata, 8);
- }
- static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t len)
- {
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv = iucv_sk(sk);
- struct sk_buff *skb;
- struct iucv_message txmsg;
- struct cmsghdr *cmsg;
- int cmsg_done;
- long timeo;
- char user_id[9];
- char appl_id[9];
- int err;
- int noblock = msg->msg_flags & MSG_DONTWAIT;
- err = sock_error(sk);
- if (err)
- return err;
- if (msg->msg_flags & MSG_OOB)
- return -EOPNOTSUPP;
- /* SOCK_SEQPACKET: we do not support segmented records */
- if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
- return -EOPNOTSUPP;
- lock_sock(sk);
- if (sk->sk_shutdown & SEND_SHUTDOWN) {
- err = -EPIPE;
- goto out;
- }
- /* Return if the socket is not in connected state */
- if (sk->sk_state != IUCV_CONNECTED) {
- err = -ENOTCONN;
- goto out;
- }
- /* initialize defaults */
- cmsg_done = 0; /* check for duplicate headers */
- txmsg.class = 0;
- /* iterate over control messages */
- for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
- cmsg = CMSG_NXTHDR(msg, cmsg)) {
- if (!CMSG_OK(msg, cmsg)) {
- err = -EINVAL;
- goto out;
- }
- if (cmsg->cmsg_level != SOL_IUCV)
- continue;
- if (cmsg->cmsg_type & cmsg_done) {
- err = -EINVAL;
- goto out;
- }
- cmsg_done |= cmsg->cmsg_type;
- switch (cmsg->cmsg_type) {
- case SCM_IUCV_TRGCLS:
- if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
- err = -EINVAL;
- goto out;
- }
- /* set iucv message target class */
- memcpy(&txmsg.class,
- (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
- break;
- default:
- err = -EINVAL;
- goto out;
- break;
- }
- }
- /* allocate one skb for each iucv message:
- * this is fine for SOCK_SEQPACKET (unless we want to support
- * segmented records using the MSG_EOR flag), but
- * for SOCK_STREAM we might want to improve it in future */
- skb = sock_alloc_send_skb(sk, len, noblock, &err);
- if (!skb)
- goto out;
- if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
- err = -EFAULT;
- goto fail;
- }
- /* wait if outstanding messages for iucv path has reached */
- timeo = sock_sndtimeo(sk, noblock);
- err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
- if (err)
- goto fail;
- /* return -ECONNRESET if the socket is no longer connected */
- if (sk->sk_state != IUCV_CONNECTED) {
- err = -ECONNRESET;
- goto fail;
- }
- /* increment and save iucv message tag for msg_completion cbk */
- txmsg.tag = iucv->send_tag++;
- memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
- skb_queue_tail(&iucv->send_skb_q, skb);
- if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
- && skb->len <= 7) {
- err = iucv_send_iprm(iucv->path, &txmsg, skb);
- /* on success: there is no message_complete callback
- * for an IPRMDATA msg; remove skb from send queue */
- if (err == 0) {
- skb_unlink(skb, &iucv->send_skb_q);
- kfree_skb(skb);
- }
- /* this error should never happen since the
- * IUCV_IPRMDATA path flag is set... sever path */
- if (err == 0x15) {
- iucv_path_sever(iucv->path, NULL);
- skb_unlink(skb, &iucv->send_skb_q);
- err = -EPIPE;
- goto fail;
- }
- } else
- err = iucv_message_send(iucv->path, &txmsg, 0, 0,
- (void *) skb->data, skb->len);
- if (err) {
- if (err == 3) {
- user_id[8] = 0;
- memcpy(user_id, iucv->dst_user_id, 8);
- appl_id[8] = 0;
- memcpy(appl_id, iucv->dst_name, 8);
- pr_err("Application %s on z/VM guest %s"
- " exceeds message limit\n",
- appl_id, user_id);
- err = -EAGAIN;
- } else
- err = -EPIPE;
- skb_unlink(skb, &iucv->send_skb_q);
- goto fail;
- }
- release_sock(sk);
- return len;
- fail:
- kfree_skb(skb);
- out:
- release_sock(sk);
- return err;
- }
- /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
- *
- * Locking: must be called with message_q.lock held
- */
- static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
- {
- int dataleft, size, copied = 0;
- struct sk_buff *nskb;
- dataleft = len;
- while (dataleft) {
- if (dataleft >= sk->sk_rcvbuf / 4)
- size = sk->sk_rcvbuf / 4;
- else
- size = dataleft;
- nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
- if (!nskb)
- return -ENOMEM;
- /* copy target class to control buffer of new skb */
- memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
- /* copy data fragment */
- memcpy(nskb->data, skb->data + copied, size);
- copied += size;
- dataleft -= size;
- skb_reset_transport_header(nskb);
- skb_reset_network_header(nskb);
- nskb->len = size;
- skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
- }
- return 0;
- }
- /* iucv_process_message() - Receive a single outstanding IUCV message
- *
- * Locking: must be called with message_q.lock held
- */
- static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
- struct iucv_path *path,
- struct iucv_message *msg)
- {
- int rc;
- unsigned int len;
- len = iucv_msg_length(msg);
- /* store msg target class in the second 4 bytes of skb ctrl buffer */
- /* Note: the first 4 bytes are reserved for msg tag */
- memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
- /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
- if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
- if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
- skb->data = NULL;
- skb->len = 0;
- }
- } else {
- rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
- skb->data, len, NULL);
- if (rc) {
- kfree_skb(skb);
- return;
- }
- /* we need to fragment iucv messages for SOCK_STREAM only;
- * for SOCK_SEQPACKET, it is only relevant if we support
- * record segmentation using MSG_EOR (see also recvmsg()) */
- if (sk->sk_type == SOCK_STREAM &&
- skb->truesize >= sk->sk_rcvbuf / 4) {
- rc = iucv_fragment_skb(sk, skb, len);
- kfree_skb(skb);
- skb = NULL;
- if (rc) {
- iucv_path_sever(path, NULL);
- return;
- }
- skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
- } else {
- skb_reset_transport_header(skb);
- skb_reset_network_header(skb);
- skb->len = len;
- }
- }
- if (sock_queue_rcv_skb(sk, skb))
- skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
- }
- /* iucv_process_message_q() - Process outstanding IUCV messages
- *
- * Locking: must be called with message_q.lock held
- */
- static void iucv_process_message_q(struct sock *sk)
- {
- struct iucv_sock *iucv = iucv_sk(sk);
- struct sk_buff *skb;
- struct sock_msg_q *p, *n;
- list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
- skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
- if (!skb)
- break;
- iucv_process_message(sk, skb, p->path, &p->msg);
- list_del(&p->list);
- kfree(p);
- if (!skb_queue_empty(&iucv->backlog_skb_q))
- break;
- }
- }
- static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t len, int flags)
- {
- int noblock = flags & MSG_DONTWAIT;
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv = iucv_sk(sk);
- unsigned int copied, rlen;
- struct sk_buff *skb, *rskb, *cskb;
- int err = 0;
- if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
- skb_queue_empty(&iucv->backlog_skb_q) &&
- skb_queue_empty(&sk->sk_receive_queue) &&
- list_empty(&iucv->message_q.list))
- return 0;
- if (flags & (MSG_OOB))
- return -EOPNOTSUPP;
- /* receive/dequeue next skb:
- * the function understands MSG_PEEK and, thus, does not dequeue skb */
- skb = skb_recv_datagram(sk, flags, noblock, &err);
- if (!skb) {
- if (sk->sk_shutdown & RCV_SHUTDOWN)
- return 0;
- return err;
- }
- rlen = skb->len; /* real length of skb */
- copied = min_t(unsigned int, rlen, len);
- cskb = skb;
- if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
- if (!(flags & MSG_PEEK))
- skb_queue_head(&sk->sk_receive_queue, skb);
- return -EFAULT;
- }
- /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
- if (sk->sk_type == SOCK_SEQPACKET) {
- if (copied < rlen)
- msg->msg_flags |= MSG_TRUNC;
- /* each iucv message contains a complete record */
- msg->msg_flags |= MSG_EOR;
- }
- /* create control message to store iucv msg target class:
- * get the trgcls from the control buffer of the skb due to
- * fragmentation of original iucv message. */
- err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
- CB_TRGCLS_LEN, CB_TRGCLS(skb));
- if (err) {
- if (!(flags & MSG_PEEK))
- skb_queue_head(&sk->sk_receive_queue, skb);
- return err;
- }
- /* Mark read part of skb as used */
- if (!(flags & MSG_PEEK)) {
- /* SOCK_STREAM: re-queue skb if it contains unreceived data */
- if (sk->sk_type == SOCK_STREAM) {
- skb_pull(skb, copied);
- if (skb->len) {
- skb_queue_head(&sk->sk_receive_queue, skb);
- goto done;
- }
- }
- kfree_skb(skb);
- /* Queue backlog skbs */
- spin_lock_bh(&iucv->message_q.lock);
- rskb = skb_dequeue(&iucv->backlog_skb_q);
- while (rskb) {
- if (sock_queue_rcv_skb(sk, rskb)) {
- skb_queue_head(&iucv->backlog_skb_q,
- rskb);
- break;
- } else {
- rskb = skb_dequeue(&iucv->backlog_skb_q);
- }
- }
- if (skb_queue_empty(&iucv->backlog_skb_q)) {
- if (!list_empty(&iucv->message_q.list))
- iucv_process_message_q(sk);
- }
- spin_unlock_bh(&iucv->message_q.lock);
- }
- done:
- /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
- if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
- copied = rlen;
- return copied;
- }
- static inline unsigned int iucv_accept_poll(struct sock *parent)
- {
- struct iucv_sock *isk, *n;
- struct sock *sk;
- list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
- sk = (struct sock *) isk;
- if (sk->sk_state == IUCV_CONNECTED)
- return POLLIN | POLLRDNORM;
- }
- return 0;
- }
- unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
- poll_table *wait)
- {
- struct sock *sk = sock->sk;
- unsigned int mask = 0;
- sock_poll_wait(file, sk_sleep(sk), wait);
- if (sk->sk_state == IUCV_LISTEN)
- return iucv_accept_poll(sk);
- if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
- mask |= POLLERR;
- if (sk->sk_shutdown & RCV_SHUTDOWN)
- mask |= POLLRDHUP;
- if (sk->sk_shutdown == SHUTDOWN_MASK)
- mask |= POLLHUP;
- if (!skb_queue_empty(&sk->sk_receive_queue) ||
- (sk->sk_shutdown & RCV_SHUTDOWN))
- mask |= POLLIN | POLLRDNORM;
- if (sk->sk_state == IUCV_CLOSED)
- mask |= POLLHUP;
- if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
- mask |= POLLIN;
- if (sock_writeable(sk))
- mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
- else
- set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
- return mask;
- }
- static int iucv_sock_shutdown(struct socket *sock, int how)
- {
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv = iucv_sk(sk);
- struct iucv_message txmsg;
- int err = 0;
- how++;
- if ((how & ~SHUTDOWN_MASK) || !how)
- return -EINVAL;
- lock_sock(sk);
- switch (sk->sk_state) {
- case IUCV_DISCONN:
- case IUCV_CLOSING:
- case IUCV_SEVERED:
- case IUCV_CLOSED:
- err = -ENOTCONN;
- goto fail;
- default:
- sk->sk_shutdown |= how;
- break;
- }
- if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
- txmsg.class = 0;
- txmsg.tag = 0;
- err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
- (void *) iprm_shutdown, 8);
- if (err) {
- switch (err) {
- case 1:
- err = -ENOTCONN;
- break;
- case 2:
- err = -ECONNRESET;
- break;
- default:
- err = -ENOTCONN;
- break;
- }
- }
- }
- if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
- err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
- if (err)
- err = -ENOTCONN;
- skb_queue_purge(&sk->sk_receive_queue);
- }
- /* Wake up anyone sleeping in poll */
- sk->sk_state_change(sk);
- fail:
- release_sock(sk);
- return err;
- }
- static int iucv_sock_release(struct socket *sock)
- {
- struct sock *sk = sock->sk;
- int err = 0;
- if (!sk)
- return 0;
- iucv_sock_close(sk);
- /* Unregister with IUCV base support */
- if (iucv_sk(sk)->path) {
- iucv_path_sever(iucv_sk(sk)->path, NULL);
- iucv_path_free(iucv_sk(sk)->path);
- iucv_sk(sk)->path = NULL;
- }
- sock_orphan(sk);
- iucv_sock_kill(sk);
- return err;
- }
- /* getsockopt and setsockopt */
- static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
- char __user *optval, unsigned int optlen)
- {
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv = iucv_sk(sk);
- int val;
- int rc;
- if (level != SOL_IUCV)
- return -ENOPROTOOPT;
- if (optlen < sizeof(int))
- return -EINVAL;
- if (get_user(val, (int __user *) optval))
- return -EFAULT;
- rc = 0;
- lock_sock(sk);
- switch (optname) {
- case SO_IPRMDATA_MSG:
- if (val)
- iucv->flags |= IUCV_IPRMDATA;
- else
- iucv->flags &= ~IUCV_IPRMDATA;
- break;
- case SO_MSGLIMIT:
- switch (sk->sk_state) {
- case IUCV_OPEN:
- case IUCV_BOUND:
- if (val < 1 || val > (u16)(~0))
- rc = -EINVAL;
- else
- iucv->msglimit = val;
- break;
- default:
- rc = -EINVAL;
- break;
- }
- break;
- default:
- rc = -ENOPROTOOPT;
- break;
- }
- release_sock(sk);
- return rc;
- }
- static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
- char __user *optval, int __user *optlen)
- {
- struct sock *sk = sock->sk;
- struct iucv_sock *iucv = iucv_sk(sk);
- int val, len;
- if (level != SOL_IUCV)
- return -ENOPROTOOPT;
- if (get_user(len, optlen))
- return -EFAULT;
- if (len < 0)
- return -EINVAL;
- len = min_t(unsigned int, len, sizeof(int));
- switch (optname) {
- case SO_IPRMDATA_MSG:
- val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
- break;
- case SO_MSGLIMIT:
- lock_sock(sk);
- val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
- : iucv->msglimit; /* default */
- release_sock(sk);
- break;
- default:
- return -ENOPROTOOPT;
- }
- if (put_user(len, optlen))
- return -EFAULT;
- if (copy_to_user(optval, &val, len))
- return -EFAULT;
- return 0;
- }
- /* Callback wrappers - called from iucv base support */
- static int iucv_callback_connreq(struct iucv_path *path,
- u8 ipvmid[8], u8 ipuser[16])
- {
- unsigned char user_data[16];
- unsigned char nuser_data[16];
- unsigned char src_name[8];
- struct hlist_node *node;
- struct sock *sk, *nsk;
- struct iucv_sock *iucv, *niucv;
- int err;
- memcpy(src_name, ipuser, 8);
- EBCASC(src_name, 8);
- /* Find out if this path belongs to af_iucv. */
- read_lock(&iucv_sk_list.lock);
- iucv = NULL;
- sk = NULL;
- sk_for_each(sk, node, &iucv_sk_list.head)
- if (sk->sk_state == IUCV_LISTEN &&
- !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
- /*
- * Found a listening socket with
- * src_name == ipuser[0-7].
- */
- iucv = iucv_sk(sk);
- break;
- }
- read_unlock(&iucv_sk_list.lock);
- if (!iucv)
- /* No socket found, not one of our paths. */
- return -EINVAL;
- bh_lock_sock(sk);
- /* Check if parent socket is listening */
- low_nmcpy(user_data, iucv->src_name);
- high_nmcpy(user_data, iucv->dst_name);
- ASCEBC(user_data, sizeof(user_data));
- if (sk->sk_state != IUCV_LISTEN) {
- err = iucv_path_sever(path, user_data);
- iucv_path_free(path);
- goto fail;
- }
- /* Check for backlog size */
- if (sk_acceptq_is_full(sk)) {
- err = iucv_path_sever(path, user_data);
- iucv_path_free(path);
- goto fail;
- }
- /* Create the new socket */
- nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
- if (!nsk) {
- err = iucv_path_sever(path, user_data);
- iucv_path_free(path);
- goto fail;
- }
- niucv = iucv_sk(nsk);
- iucv_sock_init(nsk, sk);
- /* Set the new iucv_sock */
- memcpy(niucv->dst_name, ipuser + 8, 8);
- EBCASC(niucv->dst_name, 8);
- memcpy(niucv->dst_user_id, ipvmid, 8);
- memcpy(niucv->src_name, iucv->src_name, 8);
- memcpy(niucv->src_user_id, iucv->src_user_id, 8);
- niucv->path = path;
- /* Call iucv_accept */
- high_nmcpy(nuser_data, ipuser + 8);
- memcpy(nuser_data + 8, niucv->src_name, 8);
- ASCEBC(nuser_data + 8, 8);
- /* set message limit for path based on msglimit of accepting socket */
- niucv->msglimit = iucv->msglimit;
- path->msglim = iucv->msglimit;
- err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
- if (err) {
- err = iucv_path_sever(path, user_data);
- iucv_path_free(path);
- iucv_sock_kill(nsk);
- goto fail;
- }
- iucv_accept_enqueue(sk, nsk);
- /* Wake up accept */
- nsk->sk_state = IUCV_CONNECTED;
- sk->sk_data_ready(sk, 1);
- err = 0;
- fail:
- bh_unlock_sock(sk);
- return 0;
- }
- static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
- {
- struct sock *sk = path->private;
- sk->sk_state = IUCV_CONNECTED;
- sk->sk_state_change(sk);
- }
- static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
- {
- struct sock *sk = path->private;
- struct iucv_sock *iucv = iucv_sk(sk);
- struct sk_buff *skb;
- struct sock_msg_q *save_msg;
- int len;
- if (sk->sk_shutdown & RCV_SHUTDOWN) {
- iucv_message_reject(path, msg);
- return;
- }
- spin_lock(&iucv->message_q.lock);
- if (!list_empty(&iucv->message_q.list) ||
- !skb_queue_empty(&iucv->backlog_skb_q))
- goto save_message;
- len = atomic_read(&sk->sk_rmem_alloc);
- len += iucv_msg_length(msg) + sizeof(struct sk_buff);
- if (len > sk->sk_rcvbuf)
- goto save_message;
- skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
- if (!skb)
- goto save_message;
- iucv_process_message(sk, skb, path, msg);
- goto out_unlock;
- save_message:
- save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
- if (!save_msg)
- goto out_unlock;
- save_msg->path = path;
- save_msg->msg = *msg;
- list_add_tail(&save_msg->list, &iucv->message_q.list);
- out_unlock:
- spin_unlock(&iucv->message_q.lock);
- }
- static void iucv_callback_txdone(struct iucv_path *path,
- struct iucv_message *msg)
- {
- struct sock *sk = path->private;
- struct sk_buff *this = NULL;
- struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
- struct sk_buff *list_skb = list->next;
- unsigned long flags;
- if (!skb_queue_empty(list)) {
- spin_lock_irqsave(&list->lock, flags);
- while (list_skb != (struct sk_buff *)list) {
- if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
- this = list_skb;
- break;
- }
- list_skb = list_skb->next;
- }
- if (this)
- __skb_unlink(this, list);
- spin_unlock_irqrestore(&list->lock, flags);
- if (this) {
- kfree_skb(this);
- /* wake up any process waiting for sending */
- iucv_sock_wake_msglim(sk);
- }
- }
- BUG_ON(!this);
- if (sk->sk_state == IUCV_CLOSING) {
- if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
- sk->sk_state = IUCV_CLOSED;
- sk->sk_state_change(sk);
- }
- }
- }
- static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
- {
- struct sock *sk = path->private;
- if (!list_empty(&iucv_sk(sk)->accept_q))
- sk->sk_state = IUCV_SEVERED;
- else
- sk->sk_state = IUCV_DISCONN;
- sk->sk_state_change(sk);
- }
- /* called if the other communication side shuts down its RECV direction;
- * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
- */
- static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
- {
- struct sock *sk = path->private;
- bh_lock_sock(sk);
- if (sk->sk_state != IUCV_CLOSED) {
- sk->sk_shutdown |= SEND_SHUTDOWN;
- sk->sk_state_change(sk);
- }
- bh_unlock_sock(sk);
- }
- static const struct proto_ops iucv_sock_ops = {
- .family = PF_IUCV,
- .owner = THIS_MODULE,
- .release = iucv_sock_release,
- .bind = iucv_sock_bind,
- .connect = iucv_sock_connect,
- .listen = iucv_sock_listen,
- .accept = iucv_sock_accept,
- .getname = iucv_sock_getname,
- .sendmsg = iucv_sock_sendmsg,
- .recvmsg = iucv_sock_recvmsg,
- .poll = iucv_sock_poll,
- .ioctl = sock_no_ioctl,
- .mmap = sock_no_mmap,
- .socketpair = sock_no_socketpair,
- .shutdown = iucv_sock_shutdown,
- .setsockopt = iucv_sock_setsockopt,
- .getsockopt = iucv_sock_getsockopt,
- };
- static const struct net_proto_family iucv_sock_family_ops = {
- .family = AF_IUCV,
- .owner = THIS_MODULE,
- .create = iucv_sock_create,
- };
- static int __init afiucv_init(void)
- {
- int err;
- if (!MACHINE_IS_VM) {
- pr_err("The af_iucv module cannot be loaded"
- " without z/VM\n");
- err = -EPROTONOSUPPORT;
- goto out;
- }
- cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
- if (unlikely(err)) {
- WARN_ON(err);
- err = -EPROTONOSUPPORT;
- goto out;
- }
- err = iucv_register(&af_iucv_handler, 0);
- if (err)
- goto out;
- err = proto_register(&iucv_proto, 0);
- if (err)
- goto out_iucv;
- err = sock_register(&iucv_sock_family_ops);
- if (err)
- goto out_proto;
- /* establish dummy device */
- err = driver_register(&af_iucv_driver);
- if (err)
- goto out_sock;
- af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
- if (!af_iucv_dev) {
- err = -ENOMEM;
- goto out_driver;
- }
- dev_set_name(af_iucv_dev, "af_iucv");
- af_iucv_dev->bus = &iucv_bus;
- af_iucv_dev->parent = iucv_root;
- af_iucv_dev->release = (void (*)(struct device *))kfree;
- af_iucv_dev->driver = &af_iucv_driver;
- err = device_register(af_iucv_dev);
- if (err)
- goto out_driver;
- return 0;
- out_driver:
- driver_unregister(&af_iucv_driver);
- out_sock:
- sock_unregister(PF_IUCV);
- out_proto:
- proto_unregister(&iucv_proto);
- out_iucv:
- iucv_unregister(&af_iucv_handler, 0);
- out:
- return err;
- }
- static void __exit afiucv_exit(void)
- {
- device_unregister(af_iucv_dev);
- driver_unregister(&af_iucv_driver);
- sock_unregister(PF_IUCV);
- proto_unregister(&iucv_proto);
- iucv_unregister(&af_iucv_handler, 0);
- }
- module_init(afiucv_init);
- module_exit(afiucv_exit);
- MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
- MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
- MODULE_VERSION(VERSION);
- MODULE_LICENSE("GPL");
- MODULE_ALIAS_NETPROTO(PF_IUCV);
|