af_iucv.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  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. #define __iucv_sock_wait(sk, condition, timeo, ret) \
  42. do { \
  43. DEFINE_WAIT(__wait); \
  44. long __timeo = timeo; \
  45. ret = 0; \
  46. prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
  47. while (!(condition)) { \
  48. if (!__timeo) { \
  49. ret = -EAGAIN; \
  50. break; \
  51. } \
  52. if (signal_pending(current)) { \
  53. ret = sock_intr_errno(__timeo); \
  54. break; \
  55. } \
  56. release_sock(sk); \
  57. __timeo = schedule_timeout(__timeo); \
  58. lock_sock(sk); \
  59. ret = sock_error(sk); \
  60. if (ret) \
  61. break; \
  62. } \
  63. finish_wait(sk_sleep(sk), &__wait); \
  64. } while (0)
  65. #define iucv_sock_wait(sk, condition, timeo) \
  66. ({ \
  67. int __ret = 0; \
  68. if (!(condition)) \
  69. __iucv_sock_wait(sk, condition, timeo, __ret); \
  70. __ret; \
  71. })
  72. static void iucv_sock_kill(struct sock *sk);
  73. static void iucv_sock_close(struct sock *sk);
  74. static void iucv_sever_path(struct sock *, int);
  75. static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
  76. struct packet_type *pt, struct net_device *orig_dev);
  77. static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
  78. struct sk_buff *skb, u8 flags);
  79. static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
  80. /* Call Back functions */
  81. static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
  82. static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
  83. static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
  84. static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
  85. u8 ipuser[16]);
  86. static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
  87. static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
  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 (wq_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. }
  469. static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
  470. {
  471. struct sock *sk;
  472. struct iucv_sock *iucv;
  473. sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
  474. if (!sk)
  475. return NULL;
  476. iucv = iucv_sk(sk);
  477. sock_init_data(sock, sk);
  478. INIT_LIST_HEAD(&iucv->accept_q);
  479. spin_lock_init(&iucv->accept_q_lock);
  480. skb_queue_head_init(&iucv->send_skb_q);
  481. INIT_LIST_HEAD(&iucv->message_q.list);
  482. spin_lock_init(&iucv->message_q.lock);
  483. skb_queue_head_init(&iucv->backlog_skb_q);
  484. iucv->send_tag = 0;
  485. atomic_set(&iucv->pendings, 0);
  486. iucv->flags = 0;
  487. iucv->msglimit = 0;
  488. atomic_set(&iucv->msg_sent, 0);
  489. atomic_set(&iucv->msg_recv, 0);
  490. iucv->path = NULL;
  491. iucv->sk_txnotify = afiucv_hs_callback_txnotify;
  492. memset(&iucv->src_user_id , 0, 32);
  493. if (pr_iucv)
  494. iucv->transport = AF_IUCV_TRANS_IUCV;
  495. else
  496. iucv->transport = AF_IUCV_TRANS_HIPER;
  497. sk->sk_destruct = iucv_sock_destruct;
  498. sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
  499. sk->sk_allocation = GFP_DMA;
  500. sock_reset_flag(sk, SOCK_ZAPPED);
  501. sk->sk_protocol = proto;
  502. sk->sk_state = IUCV_OPEN;
  503. iucv_sock_link(&iucv_sk_list, sk);
  504. return sk;
  505. }
  506. /* Create an IUCV socket */
  507. static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
  508. int kern)
  509. {
  510. struct sock *sk;
  511. if (protocol && protocol != PF_IUCV)
  512. return -EPROTONOSUPPORT;
  513. sock->state = SS_UNCONNECTED;
  514. switch (sock->type) {
  515. case SOCK_STREAM:
  516. sock->ops = &iucv_sock_ops;
  517. break;
  518. case SOCK_SEQPACKET:
  519. /* currently, proto ops can handle both sk types */
  520. sock->ops = &iucv_sock_ops;
  521. break;
  522. default:
  523. return -ESOCKTNOSUPPORT;
  524. }
  525. sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
  526. if (!sk)
  527. return -ENOMEM;
  528. iucv_sock_init(sk, NULL);
  529. return 0;
  530. }
  531. void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
  532. {
  533. write_lock_bh(&l->lock);
  534. sk_add_node(sk, &l->head);
  535. write_unlock_bh(&l->lock);
  536. }
  537. void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
  538. {
  539. write_lock_bh(&l->lock);
  540. sk_del_node_init(sk);
  541. write_unlock_bh(&l->lock);
  542. }
  543. void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
  544. {
  545. unsigned long flags;
  546. struct iucv_sock *par = iucv_sk(parent);
  547. sock_hold(sk);
  548. spin_lock_irqsave(&par->accept_q_lock, flags);
  549. list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
  550. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  551. iucv_sk(sk)->parent = parent;
  552. sk_acceptq_added(parent);
  553. }
  554. void iucv_accept_unlink(struct sock *sk)
  555. {
  556. unsigned long flags;
  557. struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
  558. spin_lock_irqsave(&par->accept_q_lock, flags);
  559. list_del_init(&iucv_sk(sk)->accept_q);
  560. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  561. sk_acceptq_removed(iucv_sk(sk)->parent);
  562. iucv_sk(sk)->parent = NULL;
  563. sock_put(sk);
  564. }
  565. struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
  566. {
  567. struct iucv_sock *isk, *n;
  568. struct sock *sk;
  569. list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
  570. sk = (struct sock *) isk;
  571. lock_sock(sk);
  572. if (sk->sk_state == IUCV_CLOSED) {
  573. iucv_accept_unlink(sk);
  574. release_sock(sk);
  575. continue;
  576. }
  577. if (sk->sk_state == IUCV_CONNECTED ||
  578. sk->sk_state == IUCV_DISCONN ||
  579. !newsock) {
  580. iucv_accept_unlink(sk);
  581. if (newsock)
  582. sock_graft(sk, newsock);
  583. release_sock(sk);
  584. return sk;
  585. }
  586. release_sock(sk);
  587. }
  588. return NULL;
  589. }
  590. static void __iucv_auto_name(struct iucv_sock *iucv)
  591. {
  592. char name[12];
  593. sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
  594. while (__iucv_get_sock_by_name(name)) {
  595. sprintf(name, "%08x",
  596. atomic_inc_return(&iucv_sk_list.autobind_name));
  597. }
  598. memcpy(iucv->src_name, name, 8);
  599. }
  600. /* Bind an unbound socket */
  601. static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
  602. int addr_len)
  603. {
  604. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  605. struct sock *sk = sock->sk;
  606. struct iucv_sock *iucv;
  607. int err = 0;
  608. struct net_device *dev;
  609. char uid[9];
  610. /* Verify the input sockaddr */
  611. if (!addr || addr->sa_family != AF_IUCV)
  612. return -EINVAL;
  613. lock_sock(sk);
  614. if (sk->sk_state != IUCV_OPEN) {
  615. err = -EBADFD;
  616. goto done;
  617. }
  618. write_lock_bh(&iucv_sk_list.lock);
  619. iucv = iucv_sk(sk);
  620. if (__iucv_get_sock_by_name(sa->siucv_name)) {
  621. err = -EADDRINUSE;
  622. goto done_unlock;
  623. }
  624. if (iucv->path)
  625. goto done_unlock;
  626. /* Bind the socket */
  627. if (pr_iucv)
  628. if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
  629. goto vm_bind; /* VM IUCV transport */
  630. /* try hiper transport */
  631. memcpy(uid, sa->siucv_user_id, sizeof(uid));
  632. ASCEBC(uid, 8);
  633. rcu_read_lock();
  634. for_each_netdev_rcu(&init_net, dev) {
  635. if (!memcmp(dev->perm_addr, uid, 8)) {
  636. memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
  637. /* Check for unitialized siucv_name */
  638. if (strncmp(sa->siucv_name, " ", 8) == 0)
  639. __iucv_auto_name(iucv);
  640. else
  641. memcpy(iucv->src_name, sa->siucv_name, 8);
  642. sk->sk_bound_dev_if = dev->ifindex;
  643. iucv->hs_dev = dev;
  644. dev_hold(dev);
  645. sk->sk_state = IUCV_BOUND;
  646. iucv->transport = AF_IUCV_TRANS_HIPER;
  647. if (!iucv->msglimit)
  648. iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
  649. rcu_read_unlock();
  650. goto done_unlock;
  651. }
  652. }
  653. rcu_read_unlock();
  654. vm_bind:
  655. if (pr_iucv) {
  656. /* use local userid for backward compat */
  657. memcpy(iucv->src_name, sa->siucv_name, 8);
  658. memcpy(iucv->src_user_id, iucv_userid, 8);
  659. sk->sk_state = IUCV_BOUND;
  660. iucv->transport = AF_IUCV_TRANS_IUCV;
  661. if (!iucv->msglimit)
  662. iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
  663. goto done_unlock;
  664. }
  665. /* found no dev to bind */
  666. err = -ENODEV;
  667. done_unlock:
  668. /* Release the socket list lock */
  669. write_unlock_bh(&iucv_sk_list.lock);
  670. done:
  671. release_sock(sk);
  672. return err;
  673. }
  674. /* Automatically bind an unbound socket */
  675. static int iucv_sock_autobind(struct sock *sk)
  676. {
  677. struct iucv_sock *iucv = iucv_sk(sk);
  678. int err = 0;
  679. if (unlikely(!pr_iucv))
  680. return -EPROTO;
  681. memcpy(iucv->src_user_id, iucv_userid, 8);
  682. write_lock_bh(&iucv_sk_list.lock);
  683. __iucv_auto_name(iucv);
  684. write_unlock_bh(&iucv_sk_list.lock);
  685. if (!iucv->msglimit)
  686. iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
  687. return err;
  688. }
  689. static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
  690. {
  691. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  692. struct sock *sk = sock->sk;
  693. struct iucv_sock *iucv = iucv_sk(sk);
  694. unsigned char user_data[16];
  695. int err;
  696. high_nmcpy(user_data, sa->siucv_name);
  697. low_nmcpy(user_data, iucv->src_name);
  698. ASCEBC(user_data, sizeof(user_data));
  699. /* Create path. */
  700. iucv->path = iucv_path_alloc(iucv->msglimit,
  701. IUCV_IPRMDATA, GFP_KERNEL);
  702. if (!iucv->path) {
  703. err = -ENOMEM;
  704. goto done;
  705. }
  706. err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
  707. sa->siucv_user_id, NULL, user_data,
  708. sk);
  709. if (err) {
  710. iucv_path_free(iucv->path);
  711. iucv->path = NULL;
  712. switch (err) {
  713. case 0x0b: /* Target communicator is not logged on */
  714. err = -ENETUNREACH;
  715. break;
  716. case 0x0d: /* Max connections for this guest exceeded */
  717. case 0x0e: /* Max connections for target guest exceeded */
  718. err = -EAGAIN;
  719. break;
  720. case 0x0f: /* Missing IUCV authorization */
  721. err = -EACCES;
  722. break;
  723. default:
  724. err = -ECONNREFUSED;
  725. break;
  726. }
  727. }
  728. done:
  729. return err;
  730. }
  731. /* Connect an unconnected socket */
  732. static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
  733. int alen, int flags)
  734. {
  735. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  736. struct sock *sk = sock->sk;
  737. struct iucv_sock *iucv = iucv_sk(sk);
  738. int err;
  739. if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
  740. return -EINVAL;
  741. if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
  742. return -EBADFD;
  743. if (sk->sk_state == IUCV_OPEN &&
  744. iucv->transport == AF_IUCV_TRANS_HIPER)
  745. return -EBADFD; /* explicit bind required */
  746. if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
  747. return -EINVAL;
  748. if (sk->sk_state == IUCV_OPEN) {
  749. err = iucv_sock_autobind(sk);
  750. if (unlikely(err))
  751. return err;
  752. }
  753. lock_sock(sk);
  754. /* Set the destination information */
  755. memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
  756. memcpy(iucv->dst_name, sa->siucv_name, 8);
  757. if (iucv->transport == AF_IUCV_TRANS_HIPER)
  758. err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
  759. else
  760. err = afiucv_path_connect(sock, addr);
  761. if (err)
  762. goto done;
  763. if (sk->sk_state != IUCV_CONNECTED)
  764. err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
  765. IUCV_DISCONN),
  766. sock_sndtimeo(sk, flags & O_NONBLOCK));
  767. if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
  768. err = -ECONNREFUSED;
  769. if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
  770. iucv_sever_path(sk, 0);
  771. done:
  772. release_sock(sk);
  773. return err;
  774. }
  775. /* Move a socket into listening state. */
  776. static int iucv_sock_listen(struct socket *sock, int backlog)
  777. {
  778. struct sock *sk = sock->sk;
  779. int err;
  780. lock_sock(sk);
  781. err = -EINVAL;
  782. if (sk->sk_state != IUCV_BOUND)
  783. goto done;
  784. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  785. goto done;
  786. sk->sk_max_ack_backlog = backlog;
  787. sk->sk_ack_backlog = 0;
  788. sk->sk_state = IUCV_LISTEN;
  789. err = 0;
  790. done:
  791. release_sock(sk);
  792. return err;
  793. }
  794. /* Accept a pending connection */
  795. static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
  796. int flags)
  797. {
  798. DECLARE_WAITQUEUE(wait, current);
  799. struct sock *sk = sock->sk, *nsk;
  800. long timeo;
  801. int err = 0;
  802. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  803. if (sk->sk_state != IUCV_LISTEN) {
  804. err = -EBADFD;
  805. goto done;
  806. }
  807. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  808. /* Wait for an incoming connection */
  809. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  810. while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
  811. set_current_state(TASK_INTERRUPTIBLE);
  812. if (!timeo) {
  813. err = -EAGAIN;
  814. break;
  815. }
  816. release_sock(sk);
  817. timeo = schedule_timeout(timeo);
  818. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  819. if (sk->sk_state != IUCV_LISTEN) {
  820. err = -EBADFD;
  821. break;
  822. }
  823. if (signal_pending(current)) {
  824. err = sock_intr_errno(timeo);
  825. break;
  826. }
  827. }
  828. set_current_state(TASK_RUNNING);
  829. remove_wait_queue(sk_sleep(sk), &wait);
  830. if (err)
  831. goto done;
  832. newsock->state = SS_CONNECTED;
  833. done:
  834. release_sock(sk);
  835. return err;
  836. }
  837. static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
  838. int *len, int peer)
  839. {
  840. struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
  841. struct sock *sk = sock->sk;
  842. struct iucv_sock *iucv = iucv_sk(sk);
  843. addr->sa_family = AF_IUCV;
  844. *len = sizeof(struct sockaddr_iucv);
  845. if (peer) {
  846. memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
  847. memcpy(siucv->siucv_name, iucv->dst_name, 8);
  848. } else {
  849. memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
  850. memcpy(siucv->siucv_name, iucv->src_name, 8);
  851. }
  852. memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
  853. memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
  854. memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
  855. return 0;
  856. }
  857. /**
  858. * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
  859. * @path: IUCV path
  860. * @msg: Pointer to a struct iucv_message
  861. * @skb: The socket data to send, skb->len MUST BE <= 7
  862. *
  863. * Send the socket data in the parameter list in the iucv message
  864. * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
  865. * list and the socket data len at index 7 (last byte).
  866. * See also iucv_msg_length().
  867. *
  868. * Returns the error code from the iucv_message_send() call.
  869. */
  870. static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
  871. struct sk_buff *skb)
  872. {
  873. u8 prmdata[8];
  874. memcpy(prmdata, (void *) skb->data, skb->len);
  875. prmdata[7] = 0xff - (u8) skb->len;
  876. return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
  877. (void *) prmdata, 8);
  878. }
  879. static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  880. size_t len)
  881. {
  882. struct sock *sk = sock->sk;
  883. struct iucv_sock *iucv = iucv_sk(sk);
  884. struct sk_buff *skb;
  885. struct iucv_message txmsg;
  886. struct cmsghdr *cmsg;
  887. int cmsg_done;
  888. long timeo;
  889. char user_id[9];
  890. char appl_id[9];
  891. int err;
  892. int noblock = msg->msg_flags & MSG_DONTWAIT;
  893. err = sock_error(sk);
  894. if (err)
  895. return err;
  896. if (msg->msg_flags & MSG_OOB)
  897. return -EOPNOTSUPP;
  898. /* SOCK_SEQPACKET: we do not support segmented records */
  899. if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
  900. return -EOPNOTSUPP;
  901. lock_sock(sk);
  902. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  903. err = -EPIPE;
  904. goto out;
  905. }
  906. /* Return if the socket is not in connected state */
  907. if (sk->sk_state != IUCV_CONNECTED) {
  908. err = -ENOTCONN;
  909. goto out;
  910. }
  911. /* initialize defaults */
  912. cmsg_done = 0; /* check for duplicate headers */
  913. txmsg.class = 0;
  914. /* iterate over control messages */
  915. for_each_cmsghdr(cmsg, msg) {
  916. if (!CMSG_OK(msg, cmsg)) {
  917. err = -EINVAL;
  918. goto out;
  919. }
  920. if (cmsg->cmsg_level != SOL_IUCV)
  921. continue;
  922. if (cmsg->cmsg_type & cmsg_done) {
  923. err = -EINVAL;
  924. goto out;
  925. }
  926. cmsg_done |= cmsg->cmsg_type;
  927. switch (cmsg->cmsg_type) {
  928. case SCM_IUCV_TRGCLS:
  929. if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
  930. err = -EINVAL;
  931. goto out;
  932. }
  933. /* set iucv message target class */
  934. memcpy(&txmsg.class,
  935. (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
  936. break;
  937. default:
  938. err = -EINVAL;
  939. goto out;
  940. }
  941. }
  942. /* allocate one skb for each iucv message:
  943. * this is fine for SOCK_SEQPACKET (unless we want to support
  944. * segmented records using the MSG_EOR flag), but
  945. * for SOCK_STREAM we might want to improve it in future */
  946. if (iucv->transport == AF_IUCV_TRANS_HIPER)
  947. skb = sock_alloc_send_skb(sk,
  948. len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
  949. noblock, &err);
  950. else
  951. skb = sock_alloc_send_skb(sk, len, noblock, &err);
  952. if (!skb)
  953. goto out;
  954. if (iucv->transport == AF_IUCV_TRANS_HIPER)
  955. skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
  956. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  957. err = -EFAULT;
  958. goto fail;
  959. }
  960. /* wait if outstanding messages for iucv path has reached */
  961. timeo = sock_sndtimeo(sk, noblock);
  962. err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
  963. if (err)
  964. goto fail;
  965. /* return -ECONNRESET if the socket is no longer connected */
  966. if (sk->sk_state != IUCV_CONNECTED) {
  967. err = -ECONNRESET;
  968. goto fail;
  969. }
  970. /* increment and save iucv message tag for msg_completion cbk */
  971. txmsg.tag = iucv->send_tag++;
  972. IUCV_SKB_CB(skb)->tag = txmsg.tag;
  973. if (iucv->transport == AF_IUCV_TRANS_HIPER) {
  974. atomic_inc(&iucv->msg_sent);
  975. err = afiucv_hs_send(&txmsg, sk, skb, 0);
  976. if (err) {
  977. atomic_dec(&iucv->msg_sent);
  978. goto fail;
  979. }
  980. goto release;
  981. }
  982. skb_queue_tail(&iucv->send_skb_q, skb);
  983. if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
  984. && skb->len <= 7) {
  985. err = iucv_send_iprm(iucv->path, &txmsg, skb);
  986. /* on success: there is no message_complete callback
  987. * for an IPRMDATA msg; remove skb from send queue */
  988. if (err == 0) {
  989. skb_unlink(skb, &iucv->send_skb_q);
  990. kfree_skb(skb);
  991. }
  992. /* this error should never happen since the
  993. * IUCV_IPRMDATA path flag is set... sever path */
  994. if (err == 0x15) {
  995. pr_iucv->path_sever(iucv->path, NULL);
  996. skb_unlink(skb, &iucv->send_skb_q);
  997. err = -EPIPE;
  998. goto fail;
  999. }
  1000. } else
  1001. err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
  1002. (void *) skb->data, skb->len);
  1003. if (err) {
  1004. if (err == 3) {
  1005. user_id[8] = 0;
  1006. memcpy(user_id, iucv->dst_user_id, 8);
  1007. appl_id[8] = 0;
  1008. memcpy(appl_id, iucv->dst_name, 8);
  1009. pr_err("Application %s on z/VM guest %s"
  1010. " exceeds message limit\n",
  1011. appl_id, user_id);
  1012. err = -EAGAIN;
  1013. } else
  1014. err = -EPIPE;
  1015. skb_unlink(skb, &iucv->send_skb_q);
  1016. goto fail;
  1017. }
  1018. release:
  1019. release_sock(sk);
  1020. return len;
  1021. fail:
  1022. kfree_skb(skb);
  1023. out:
  1024. release_sock(sk);
  1025. return err;
  1026. }
  1027. /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
  1028. *
  1029. * Locking: must be called with message_q.lock held
  1030. */
  1031. static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
  1032. {
  1033. int dataleft, size, copied = 0;
  1034. struct sk_buff *nskb;
  1035. dataleft = len;
  1036. while (dataleft) {
  1037. if (dataleft >= sk->sk_rcvbuf / 4)
  1038. size = sk->sk_rcvbuf / 4;
  1039. else
  1040. size = dataleft;
  1041. nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
  1042. if (!nskb)
  1043. return -ENOMEM;
  1044. /* copy target class to control buffer of new skb */
  1045. IUCV_SKB_CB(nskb)->class = IUCV_SKB_CB(skb)->class;
  1046. /* copy data fragment */
  1047. memcpy(nskb->data, skb->data + copied, size);
  1048. copied += size;
  1049. dataleft -= size;
  1050. skb_reset_transport_header(nskb);
  1051. skb_reset_network_header(nskb);
  1052. nskb->len = size;
  1053. skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
  1054. }
  1055. return 0;
  1056. }
  1057. /* iucv_process_message() - Receive a single outstanding IUCV message
  1058. *
  1059. * Locking: must be called with message_q.lock held
  1060. */
  1061. static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
  1062. struct iucv_path *path,
  1063. struct iucv_message *msg)
  1064. {
  1065. int rc;
  1066. unsigned int len;
  1067. len = iucv_msg_length(msg);
  1068. /* store msg target class in the second 4 bytes of skb ctrl buffer */
  1069. /* Note: the first 4 bytes are reserved for msg tag */
  1070. IUCV_SKB_CB(skb)->class = msg->class;
  1071. /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
  1072. if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
  1073. if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
  1074. skb->data = NULL;
  1075. skb->len = 0;
  1076. }
  1077. } else {
  1078. rc = pr_iucv->message_receive(path, msg,
  1079. msg->flags & IUCV_IPRMDATA,
  1080. skb->data, len, NULL);
  1081. if (rc) {
  1082. kfree_skb(skb);
  1083. return;
  1084. }
  1085. /* we need to fragment iucv messages for SOCK_STREAM only;
  1086. * for SOCK_SEQPACKET, it is only relevant if we support
  1087. * record segmentation using MSG_EOR (see also recvmsg()) */
  1088. if (sk->sk_type == SOCK_STREAM &&
  1089. skb->truesize >= sk->sk_rcvbuf / 4) {
  1090. rc = iucv_fragment_skb(sk, skb, len);
  1091. kfree_skb(skb);
  1092. skb = NULL;
  1093. if (rc) {
  1094. pr_iucv->path_sever(path, NULL);
  1095. return;
  1096. }
  1097. skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
  1098. } else {
  1099. skb_reset_transport_header(skb);
  1100. skb_reset_network_header(skb);
  1101. skb->len = len;
  1102. }
  1103. }
  1104. IUCV_SKB_CB(skb)->offset = 0;
  1105. if (sock_queue_rcv_skb(sk, skb))
  1106. skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
  1107. }
  1108. /* iucv_process_message_q() - Process outstanding IUCV messages
  1109. *
  1110. * Locking: must be called with message_q.lock held
  1111. */
  1112. static void iucv_process_message_q(struct sock *sk)
  1113. {
  1114. struct iucv_sock *iucv = iucv_sk(sk);
  1115. struct sk_buff *skb;
  1116. struct sock_msg_q *p, *n;
  1117. list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
  1118. skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
  1119. if (!skb)
  1120. break;
  1121. iucv_process_message(sk, skb, p->path, &p->msg);
  1122. list_del(&p->list);
  1123. kfree(p);
  1124. if (!skb_queue_empty(&iucv->backlog_skb_q))
  1125. break;
  1126. }
  1127. }
  1128. static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  1129. size_t len, int flags)
  1130. {
  1131. int noblock = flags & MSG_DONTWAIT;
  1132. struct sock *sk = sock->sk;
  1133. struct iucv_sock *iucv = iucv_sk(sk);
  1134. unsigned int copied, rlen;
  1135. struct sk_buff *skb, *rskb, *cskb;
  1136. int err = 0;
  1137. u32 offset;
  1138. if ((sk->sk_state == IUCV_DISCONN) &&
  1139. skb_queue_empty(&iucv->backlog_skb_q) &&
  1140. skb_queue_empty(&sk->sk_receive_queue) &&
  1141. list_empty(&iucv->message_q.list))
  1142. return 0;
  1143. if (flags & (MSG_OOB))
  1144. return -EOPNOTSUPP;
  1145. /* receive/dequeue next skb:
  1146. * the function understands MSG_PEEK and, thus, does not dequeue skb */
  1147. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1148. if (!skb) {
  1149. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1150. return 0;
  1151. return err;
  1152. }
  1153. offset = IUCV_SKB_CB(skb)->offset;
  1154. rlen = skb->len - offset; /* real length of skb */
  1155. copied = min_t(unsigned int, rlen, len);
  1156. if (!rlen)
  1157. sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
  1158. cskb = skb;
  1159. if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
  1160. if (!(flags & MSG_PEEK))
  1161. skb_queue_head(&sk->sk_receive_queue, skb);
  1162. return -EFAULT;
  1163. }
  1164. /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
  1165. if (sk->sk_type == SOCK_SEQPACKET) {
  1166. if (copied < rlen)
  1167. msg->msg_flags |= MSG_TRUNC;
  1168. /* each iucv message contains a complete record */
  1169. msg->msg_flags |= MSG_EOR;
  1170. }
  1171. /* create control message to store iucv msg target class:
  1172. * get the trgcls from the control buffer of the skb due to
  1173. * fragmentation of original iucv message. */
  1174. err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
  1175. sizeof(IUCV_SKB_CB(skb)->class),
  1176. (void *)&IUCV_SKB_CB(skb)->class);
  1177. if (err) {
  1178. if (!(flags & MSG_PEEK))
  1179. skb_queue_head(&sk->sk_receive_queue, skb);
  1180. return err;
  1181. }
  1182. /* Mark read part of skb as used */
  1183. if (!(flags & MSG_PEEK)) {
  1184. /* SOCK_STREAM: re-queue skb if it contains unreceived data */
  1185. if (sk->sk_type == SOCK_STREAM) {
  1186. if (copied < rlen) {
  1187. IUCV_SKB_CB(skb)->offset = offset + copied;
  1188. skb_queue_head(&sk->sk_receive_queue, skb);
  1189. goto done;
  1190. }
  1191. }
  1192. kfree_skb(skb);
  1193. if (iucv->transport == AF_IUCV_TRANS_HIPER) {
  1194. atomic_inc(&iucv->msg_recv);
  1195. if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
  1196. WARN_ON(1);
  1197. iucv_sock_close(sk);
  1198. return -EFAULT;
  1199. }
  1200. }
  1201. /* Queue backlog skbs */
  1202. spin_lock_bh(&iucv->message_q.lock);
  1203. rskb = skb_dequeue(&iucv->backlog_skb_q);
  1204. while (rskb) {
  1205. IUCV_SKB_CB(rskb)->offset = 0;
  1206. if (sock_queue_rcv_skb(sk, rskb)) {
  1207. skb_queue_head(&iucv->backlog_skb_q,
  1208. rskb);
  1209. break;
  1210. } else {
  1211. rskb = skb_dequeue(&iucv->backlog_skb_q);
  1212. }
  1213. }
  1214. if (skb_queue_empty(&iucv->backlog_skb_q)) {
  1215. if (!list_empty(&iucv->message_q.list))
  1216. iucv_process_message_q(sk);
  1217. if (atomic_read(&iucv->msg_recv) >=
  1218. iucv->msglimit / 2) {
  1219. err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
  1220. if (err) {
  1221. sk->sk_state = IUCV_DISCONN;
  1222. sk->sk_state_change(sk);
  1223. }
  1224. }
  1225. }
  1226. spin_unlock_bh(&iucv->message_q.lock);
  1227. }
  1228. done:
  1229. /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
  1230. if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
  1231. copied = rlen;
  1232. return copied;
  1233. }
  1234. static inline unsigned int iucv_accept_poll(struct sock *parent)
  1235. {
  1236. struct iucv_sock *isk, *n;
  1237. struct sock *sk;
  1238. list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
  1239. sk = (struct sock *) isk;
  1240. if (sk->sk_state == IUCV_CONNECTED)
  1241. return POLLIN | POLLRDNORM;
  1242. }
  1243. return 0;
  1244. }
  1245. unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
  1246. poll_table *wait)
  1247. {
  1248. struct sock *sk = sock->sk;
  1249. unsigned int mask = 0;
  1250. sock_poll_wait(file, sk_sleep(sk), wait);
  1251. if (sk->sk_state == IUCV_LISTEN)
  1252. return iucv_accept_poll(sk);
  1253. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  1254. mask |= POLLERR |
  1255. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  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. iucv->path) {
  1319. err = pr_iucv->path_quiesce(iucv->path, NULL);
  1320. if (err)
  1321. err = -ENOTCONN;
  1322. /* skb_queue_purge(&sk->sk_receive_queue); */
  1323. }
  1324. skb_queue_purge(&sk->sk_receive_queue);
  1325. }
  1326. /* Wake up anyone sleeping in poll */
  1327. sk->sk_state_change(sk);
  1328. fail:
  1329. release_sock(sk);
  1330. return err;
  1331. }
  1332. static int iucv_sock_release(struct socket *sock)
  1333. {
  1334. struct sock *sk = sock->sk;
  1335. int err = 0;
  1336. if (!sk)
  1337. return 0;
  1338. iucv_sock_close(sk);
  1339. sock_orphan(sk);
  1340. iucv_sock_kill(sk);
  1341. return err;
  1342. }
  1343. /* getsockopt and setsockopt */
  1344. static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
  1345. char __user *optval, unsigned int optlen)
  1346. {
  1347. struct sock *sk = sock->sk;
  1348. struct iucv_sock *iucv = iucv_sk(sk);
  1349. int val;
  1350. int rc;
  1351. if (level != SOL_IUCV)
  1352. return -ENOPROTOOPT;
  1353. if (optlen < sizeof(int))
  1354. return -EINVAL;
  1355. if (get_user(val, (int __user *) optval))
  1356. return -EFAULT;
  1357. rc = 0;
  1358. lock_sock(sk);
  1359. switch (optname) {
  1360. case SO_IPRMDATA_MSG:
  1361. if (val)
  1362. iucv->flags |= IUCV_IPRMDATA;
  1363. else
  1364. iucv->flags &= ~IUCV_IPRMDATA;
  1365. break;
  1366. case SO_MSGLIMIT:
  1367. switch (sk->sk_state) {
  1368. case IUCV_OPEN:
  1369. case IUCV_BOUND:
  1370. if (val < 1 || val > (u16)(~0))
  1371. rc = -EINVAL;
  1372. else
  1373. iucv->msglimit = val;
  1374. break;
  1375. default:
  1376. rc = -EINVAL;
  1377. break;
  1378. }
  1379. break;
  1380. default:
  1381. rc = -ENOPROTOOPT;
  1382. break;
  1383. }
  1384. release_sock(sk);
  1385. return rc;
  1386. }
  1387. static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
  1388. char __user *optval, int __user *optlen)
  1389. {
  1390. struct sock *sk = sock->sk;
  1391. struct iucv_sock *iucv = iucv_sk(sk);
  1392. unsigned int val;
  1393. int len;
  1394. if (level != SOL_IUCV)
  1395. return -ENOPROTOOPT;
  1396. if (get_user(len, optlen))
  1397. return -EFAULT;
  1398. if (len < 0)
  1399. return -EINVAL;
  1400. len = min_t(unsigned int, len, sizeof(int));
  1401. switch (optname) {
  1402. case SO_IPRMDATA_MSG:
  1403. val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
  1404. break;
  1405. case SO_MSGLIMIT:
  1406. lock_sock(sk);
  1407. val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
  1408. : iucv->msglimit; /* default */
  1409. release_sock(sk);
  1410. break;
  1411. case SO_MSGSIZE:
  1412. if (sk->sk_state == IUCV_OPEN)
  1413. return -EBADFD;
  1414. val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
  1415. sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
  1416. 0x7fffffff;
  1417. break;
  1418. default:
  1419. return -ENOPROTOOPT;
  1420. }
  1421. if (put_user(len, optlen))
  1422. return -EFAULT;
  1423. if (copy_to_user(optval, &val, len))
  1424. return -EFAULT;
  1425. return 0;
  1426. }
  1427. /* Callback wrappers - called from iucv base support */
  1428. static int iucv_callback_connreq(struct iucv_path *path,
  1429. u8 ipvmid[8], u8 ipuser[16])
  1430. {
  1431. unsigned char user_data[16];
  1432. unsigned char nuser_data[16];
  1433. unsigned char src_name[8];
  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, &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, 0);
  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);
  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 (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
  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, 0);
  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. afiucv_swap_src_dest(skb);
  1655. trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
  1656. err = dev_queue_xmit(skb);
  1657. iucv_sock_kill(nsk);
  1658. bh_unlock_sock(sk);
  1659. goto out;
  1660. }
  1661. niucv = iucv_sk(nsk);
  1662. iucv_sock_init(nsk, sk);
  1663. niucv->transport = AF_IUCV_TRANS_HIPER;
  1664. niucv->msglimit = iucv->msglimit;
  1665. if (!trans_hdr->window)
  1666. niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
  1667. else
  1668. niucv->msglimit_peer = trans_hdr->window;
  1669. memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
  1670. memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
  1671. memcpy(niucv->src_name, iucv->src_name, 8);
  1672. memcpy(niucv->src_user_id, iucv->src_user_id, 8);
  1673. nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  1674. niucv->hs_dev = iucv->hs_dev;
  1675. dev_hold(niucv->hs_dev);
  1676. afiucv_swap_src_dest(skb);
  1677. trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
  1678. trans_hdr->window = niucv->msglimit;
  1679. /* if receiver acks the xmit connection is established */
  1680. err = dev_queue_xmit(skb);
  1681. if (!err) {
  1682. iucv_accept_enqueue(sk, nsk);
  1683. nsk->sk_state = IUCV_CONNECTED;
  1684. sk->sk_data_ready(sk);
  1685. } else
  1686. iucv_sock_kill(nsk);
  1687. bh_unlock_sock(sk);
  1688. out:
  1689. return NET_RX_SUCCESS;
  1690. }
  1691. /**
  1692. * afiucv_hs_callback_synack() - react on received SYN-ACK
  1693. **/
  1694. static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
  1695. {
  1696. struct iucv_sock *iucv = iucv_sk(sk);
  1697. struct af_iucv_trans_hdr *trans_hdr =
  1698. (struct af_iucv_trans_hdr *)skb->data;
  1699. if (!iucv)
  1700. goto out;
  1701. if (sk->sk_state != IUCV_BOUND)
  1702. goto out;
  1703. bh_lock_sock(sk);
  1704. iucv->msglimit_peer = trans_hdr->window;
  1705. sk->sk_state = IUCV_CONNECTED;
  1706. sk->sk_state_change(sk);
  1707. bh_unlock_sock(sk);
  1708. out:
  1709. kfree_skb(skb);
  1710. return NET_RX_SUCCESS;
  1711. }
  1712. /**
  1713. * afiucv_hs_callback_synfin() - react on received SYN_FIN
  1714. **/
  1715. static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
  1716. {
  1717. struct iucv_sock *iucv = iucv_sk(sk);
  1718. if (!iucv)
  1719. goto out;
  1720. if (sk->sk_state != IUCV_BOUND)
  1721. goto out;
  1722. bh_lock_sock(sk);
  1723. sk->sk_state = IUCV_DISCONN;
  1724. sk->sk_state_change(sk);
  1725. bh_unlock_sock(sk);
  1726. out:
  1727. kfree_skb(skb);
  1728. return NET_RX_SUCCESS;
  1729. }
  1730. /**
  1731. * afiucv_hs_callback_fin() - react on received FIN
  1732. **/
  1733. static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
  1734. {
  1735. struct iucv_sock *iucv = iucv_sk(sk);
  1736. /* other end of connection closed */
  1737. if (!iucv)
  1738. goto out;
  1739. bh_lock_sock(sk);
  1740. if (sk->sk_state == IUCV_CONNECTED) {
  1741. sk->sk_state = IUCV_DISCONN;
  1742. sk->sk_state_change(sk);
  1743. }
  1744. bh_unlock_sock(sk);
  1745. out:
  1746. kfree_skb(skb);
  1747. return NET_RX_SUCCESS;
  1748. }
  1749. /**
  1750. * afiucv_hs_callback_win() - react on received WIN
  1751. **/
  1752. static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
  1753. {
  1754. struct iucv_sock *iucv = iucv_sk(sk);
  1755. struct af_iucv_trans_hdr *trans_hdr =
  1756. (struct af_iucv_trans_hdr *)skb->data;
  1757. if (!iucv)
  1758. return NET_RX_SUCCESS;
  1759. if (sk->sk_state != IUCV_CONNECTED)
  1760. return NET_RX_SUCCESS;
  1761. atomic_sub(trans_hdr->window, &iucv->msg_sent);
  1762. iucv_sock_wake_msglim(sk);
  1763. return NET_RX_SUCCESS;
  1764. }
  1765. /**
  1766. * afiucv_hs_callback_rx() - react on received data
  1767. **/
  1768. static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
  1769. {
  1770. struct iucv_sock *iucv = iucv_sk(sk);
  1771. if (!iucv) {
  1772. kfree_skb(skb);
  1773. return NET_RX_SUCCESS;
  1774. }
  1775. if (sk->sk_state != IUCV_CONNECTED) {
  1776. kfree_skb(skb);
  1777. return NET_RX_SUCCESS;
  1778. }
  1779. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1780. kfree_skb(skb);
  1781. return NET_RX_SUCCESS;
  1782. }
  1783. /* write stuff from iucv_msg to skb cb */
  1784. if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
  1785. kfree_skb(skb);
  1786. return NET_RX_SUCCESS;
  1787. }
  1788. skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
  1789. skb_reset_transport_header(skb);
  1790. skb_reset_network_header(skb);
  1791. IUCV_SKB_CB(skb)->offset = 0;
  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 sock *sk;
  1812. struct iucv_sock *iucv;
  1813. struct af_iucv_trans_hdr *trans_hdr;
  1814. char nullstring[8];
  1815. int err = 0;
  1816. skb_pull(skb, ETH_HLEN);
  1817. trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
  1818. EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
  1819. EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
  1820. EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
  1821. EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
  1822. memset(nullstring, 0, sizeof(nullstring));
  1823. iucv = NULL;
  1824. sk = NULL;
  1825. read_lock(&iucv_sk_list.lock);
  1826. sk_for_each(sk, &iucv_sk_list.head) {
  1827. if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
  1828. if ((!memcmp(&iucv_sk(sk)->src_name,
  1829. trans_hdr->destAppName, 8)) &&
  1830. (!memcmp(&iucv_sk(sk)->src_user_id,
  1831. trans_hdr->destUserID, 8)) &&
  1832. (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
  1833. (!memcmp(&iucv_sk(sk)->dst_user_id,
  1834. nullstring, 8))) {
  1835. iucv = iucv_sk(sk);
  1836. break;
  1837. }
  1838. } else {
  1839. if ((!memcmp(&iucv_sk(sk)->src_name,
  1840. trans_hdr->destAppName, 8)) &&
  1841. (!memcmp(&iucv_sk(sk)->src_user_id,
  1842. trans_hdr->destUserID, 8)) &&
  1843. (!memcmp(&iucv_sk(sk)->dst_name,
  1844. trans_hdr->srcAppName, 8)) &&
  1845. (!memcmp(&iucv_sk(sk)->dst_user_id,
  1846. trans_hdr->srcUserID, 8))) {
  1847. iucv = iucv_sk(sk);
  1848. break;
  1849. }
  1850. }
  1851. }
  1852. read_unlock(&iucv_sk_list.lock);
  1853. if (!iucv)
  1854. sk = NULL;
  1855. /* no sock
  1856. how should we send with no sock
  1857. 1) send without sock no send rc checking?
  1858. 2) introduce default sock to handle this cases
  1859. SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
  1860. data -> send FIN
  1861. SYN|ACK, SYN|FIN, FIN -> no action? */
  1862. switch (trans_hdr->flags) {
  1863. case AF_IUCV_FLAG_SYN:
  1864. /* connect request */
  1865. err = afiucv_hs_callback_syn(sk, skb);
  1866. break;
  1867. case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
  1868. /* connect request confirmed */
  1869. err = afiucv_hs_callback_synack(sk, skb);
  1870. break;
  1871. case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
  1872. /* connect request refused */
  1873. err = afiucv_hs_callback_synfin(sk, skb);
  1874. break;
  1875. case (AF_IUCV_FLAG_FIN):
  1876. /* close request */
  1877. err = afiucv_hs_callback_fin(sk, skb);
  1878. break;
  1879. case (AF_IUCV_FLAG_WIN):
  1880. err = afiucv_hs_callback_win(sk, skb);
  1881. if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
  1882. kfree_skb(skb);
  1883. break;
  1884. }
  1885. /* fall through and receive non-zero length data */
  1886. case (AF_IUCV_FLAG_SHT):
  1887. /* shutdown request */
  1888. /* fall through and receive zero length data */
  1889. case 0:
  1890. /* plain data frame */
  1891. IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
  1892. err = afiucv_hs_callback_rx(sk, skb);
  1893. break;
  1894. default:
  1895. ;
  1896. }
  1897. return err;
  1898. }
  1899. /**
  1900. * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
  1901. * transport
  1902. **/
  1903. static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
  1904. enum iucv_tx_notify n)
  1905. {
  1906. struct sock *isk = skb->sk;
  1907. struct sock *sk = NULL;
  1908. struct iucv_sock *iucv = NULL;
  1909. struct sk_buff_head *list;
  1910. struct sk_buff *list_skb;
  1911. struct sk_buff *nskb;
  1912. unsigned long flags;
  1913. read_lock_irqsave(&iucv_sk_list.lock, flags);
  1914. sk_for_each(sk, &iucv_sk_list.head)
  1915. if (sk == isk) {
  1916. iucv = iucv_sk(sk);
  1917. break;
  1918. }
  1919. read_unlock_irqrestore(&iucv_sk_list.lock, flags);
  1920. if (!iucv || sock_flag(sk, SOCK_ZAPPED))
  1921. return;
  1922. list = &iucv->send_skb_q;
  1923. spin_lock_irqsave(&list->lock, flags);
  1924. if (skb_queue_empty(list))
  1925. goto out_unlock;
  1926. list_skb = list->next;
  1927. nskb = list_skb->next;
  1928. while (list_skb != (struct sk_buff *)list) {
  1929. if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
  1930. switch (n) {
  1931. case TX_NOTIFY_OK:
  1932. __skb_unlink(list_skb, list);
  1933. kfree_skb(list_skb);
  1934. iucv_sock_wake_msglim(sk);
  1935. break;
  1936. case TX_NOTIFY_PENDING:
  1937. atomic_inc(&iucv->pendings);
  1938. break;
  1939. case TX_NOTIFY_DELAYED_OK:
  1940. __skb_unlink(list_skb, list);
  1941. atomic_dec(&iucv->pendings);
  1942. if (atomic_read(&iucv->pendings) <= 0)
  1943. iucv_sock_wake_msglim(sk);
  1944. kfree_skb(list_skb);
  1945. break;
  1946. case TX_NOTIFY_UNREACHABLE:
  1947. case TX_NOTIFY_DELAYED_UNREACHABLE:
  1948. case TX_NOTIFY_TPQFULL: /* not yet used */
  1949. case TX_NOTIFY_GENERALERROR:
  1950. case TX_NOTIFY_DELAYED_GENERALERROR:
  1951. __skb_unlink(list_skb, list);
  1952. kfree_skb(list_skb);
  1953. if (sk->sk_state == IUCV_CONNECTED) {
  1954. sk->sk_state = IUCV_DISCONN;
  1955. sk->sk_state_change(sk);
  1956. }
  1957. break;
  1958. }
  1959. break;
  1960. }
  1961. list_skb = nskb;
  1962. nskb = nskb->next;
  1963. }
  1964. out_unlock:
  1965. spin_unlock_irqrestore(&list->lock, flags);
  1966. if (sk->sk_state == IUCV_CLOSING) {
  1967. if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
  1968. sk->sk_state = IUCV_CLOSED;
  1969. sk->sk_state_change(sk);
  1970. }
  1971. }
  1972. }
  1973. /*
  1974. * afiucv_netdev_event: handle netdev notifier chain events
  1975. */
  1976. static int afiucv_netdev_event(struct notifier_block *this,
  1977. unsigned long event, void *ptr)
  1978. {
  1979. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1980. struct sock *sk;
  1981. struct iucv_sock *iucv;
  1982. switch (event) {
  1983. case NETDEV_REBOOT:
  1984. case NETDEV_GOING_DOWN:
  1985. sk_for_each(sk, &iucv_sk_list.head) {
  1986. iucv = iucv_sk(sk);
  1987. if ((iucv->hs_dev == event_dev) &&
  1988. (sk->sk_state == IUCV_CONNECTED)) {
  1989. if (event == NETDEV_GOING_DOWN)
  1990. iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
  1991. sk->sk_state = IUCV_DISCONN;
  1992. sk->sk_state_change(sk);
  1993. }
  1994. }
  1995. break;
  1996. case NETDEV_DOWN:
  1997. case NETDEV_UNREGISTER:
  1998. default:
  1999. break;
  2000. }
  2001. return NOTIFY_DONE;
  2002. }
  2003. static struct notifier_block afiucv_netdev_notifier = {
  2004. .notifier_call = afiucv_netdev_event,
  2005. };
  2006. static const struct proto_ops iucv_sock_ops = {
  2007. .family = PF_IUCV,
  2008. .owner = THIS_MODULE,
  2009. .release = iucv_sock_release,
  2010. .bind = iucv_sock_bind,
  2011. .connect = iucv_sock_connect,
  2012. .listen = iucv_sock_listen,
  2013. .accept = iucv_sock_accept,
  2014. .getname = iucv_sock_getname,
  2015. .sendmsg = iucv_sock_sendmsg,
  2016. .recvmsg = iucv_sock_recvmsg,
  2017. .poll = iucv_sock_poll,
  2018. .ioctl = sock_no_ioctl,
  2019. .mmap = sock_no_mmap,
  2020. .socketpair = sock_no_socketpair,
  2021. .shutdown = iucv_sock_shutdown,
  2022. .setsockopt = iucv_sock_setsockopt,
  2023. .getsockopt = iucv_sock_getsockopt,
  2024. };
  2025. static const struct net_proto_family iucv_sock_family_ops = {
  2026. .family = AF_IUCV,
  2027. .owner = THIS_MODULE,
  2028. .create = iucv_sock_create,
  2029. };
  2030. static struct packet_type iucv_packet_type = {
  2031. .type = cpu_to_be16(ETH_P_AF_IUCV),
  2032. .func = afiucv_hs_rcv,
  2033. };
  2034. static int afiucv_iucv_init(void)
  2035. {
  2036. int err;
  2037. err = pr_iucv->iucv_register(&af_iucv_handler, 0);
  2038. if (err)
  2039. goto out;
  2040. /* establish dummy device */
  2041. af_iucv_driver.bus = pr_iucv->bus;
  2042. err = driver_register(&af_iucv_driver);
  2043. if (err)
  2044. goto out_iucv;
  2045. af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  2046. if (!af_iucv_dev) {
  2047. err = -ENOMEM;
  2048. goto out_driver;
  2049. }
  2050. dev_set_name(af_iucv_dev, "af_iucv");
  2051. af_iucv_dev->bus = pr_iucv->bus;
  2052. af_iucv_dev->parent = pr_iucv->root;
  2053. af_iucv_dev->release = (void (*)(struct device *))kfree;
  2054. af_iucv_dev->driver = &af_iucv_driver;
  2055. err = device_register(af_iucv_dev);
  2056. if (err)
  2057. goto out_driver;
  2058. return 0;
  2059. out_driver:
  2060. driver_unregister(&af_iucv_driver);
  2061. out_iucv:
  2062. pr_iucv->iucv_unregister(&af_iucv_handler, 0);
  2063. out:
  2064. return err;
  2065. }
  2066. static int __init afiucv_init(void)
  2067. {
  2068. int err;
  2069. if (MACHINE_IS_VM) {
  2070. cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
  2071. if (unlikely(err)) {
  2072. WARN_ON(err);
  2073. err = -EPROTONOSUPPORT;
  2074. goto out;
  2075. }
  2076. pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
  2077. if (!pr_iucv) {
  2078. printk(KERN_WARNING "iucv_if lookup failed\n");
  2079. memset(&iucv_userid, 0, sizeof(iucv_userid));
  2080. }
  2081. } else {
  2082. memset(&iucv_userid, 0, sizeof(iucv_userid));
  2083. pr_iucv = NULL;
  2084. }
  2085. err = proto_register(&iucv_proto, 0);
  2086. if (err)
  2087. goto out;
  2088. err = sock_register(&iucv_sock_family_ops);
  2089. if (err)
  2090. goto out_proto;
  2091. if (pr_iucv) {
  2092. err = afiucv_iucv_init();
  2093. if (err)
  2094. goto out_sock;
  2095. } else
  2096. register_netdevice_notifier(&afiucv_netdev_notifier);
  2097. dev_add_pack(&iucv_packet_type);
  2098. return 0;
  2099. out_sock:
  2100. sock_unregister(PF_IUCV);
  2101. out_proto:
  2102. proto_unregister(&iucv_proto);
  2103. out:
  2104. if (pr_iucv)
  2105. symbol_put(iucv_if);
  2106. return err;
  2107. }
  2108. static void __exit afiucv_exit(void)
  2109. {
  2110. if (pr_iucv) {
  2111. device_unregister(af_iucv_dev);
  2112. driver_unregister(&af_iucv_driver);
  2113. pr_iucv->iucv_unregister(&af_iucv_handler, 0);
  2114. symbol_put(iucv_if);
  2115. } else
  2116. unregister_netdevice_notifier(&afiucv_netdev_notifier);
  2117. dev_remove_pack(&iucv_packet_type);
  2118. sock_unregister(PF_IUCV);
  2119. proto_unregister(&iucv_proto);
  2120. }
  2121. module_init(afiucv_init);
  2122. module_exit(afiucv_exit);
  2123. MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
  2124. MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
  2125. MODULE_VERSION(VERSION);
  2126. MODULE_LICENSE("GPL");
  2127. MODULE_ALIAS_NETPROTO(PF_IUCV);