af_iucv.c 60 KB

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