af_iucv.c 58 KB

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