af_iucv.c 59 KB

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