af_iucv.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465
  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 *);
  84. static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
  85. static void iucv_callback_connrej(struct iucv_path *, u8 *);
  86. static void iucv_callback_shutdown(struct iucv_path *, u8 *);
  87. static struct iucv_sock_list iucv_sk_list = {
  88. .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
  89. .autobind_name = ATOMIC_INIT(0)
  90. };
  91. static struct iucv_handler af_iucv_handler = {
  92. .path_pending = iucv_callback_connreq,
  93. .path_complete = iucv_callback_connack,
  94. .path_severed = iucv_callback_connrej,
  95. .message_pending = iucv_callback_rx,
  96. .message_complete = iucv_callback_txdone,
  97. .path_quiesced = iucv_callback_shutdown,
  98. };
  99. static inline void high_nmcpy(unsigned char *dst, char *src)
  100. {
  101. memcpy(dst, src, 8);
  102. }
  103. static inline void low_nmcpy(unsigned char *dst, char *src)
  104. {
  105. memcpy(&dst[8], src, 8);
  106. }
  107. static int afiucv_pm_prepare(struct device *dev)
  108. {
  109. #ifdef CONFIG_PM_DEBUG
  110. printk(KERN_WARNING "afiucv_pm_prepare\n");
  111. #endif
  112. return 0;
  113. }
  114. static void afiucv_pm_complete(struct device *dev)
  115. {
  116. #ifdef CONFIG_PM_DEBUG
  117. printk(KERN_WARNING "afiucv_pm_complete\n");
  118. #endif
  119. }
  120. /**
  121. * afiucv_pm_freeze() - Freeze PM callback
  122. * @dev: AFIUCV dummy device
  123. *
  124. * Sever all established IUCV communication pathes
  125. */
  126. static int afiucv_pm_freeze(struct device *dev)
  127. {
  128. struct iucv_sock *iucv;
  129. struct sock *sk;
  130. int err = 0;
  131. #ifdef CONFIG_PM_DEBUG
  132. printk(KERN_WARNING "afiucv_pm_freeze\n");
  133. #endif
  134. read_lock(&iucv_sk_list.lock);
  135. sk_for_each(sk, &iucv_sk_list.head) {
  136. iucv = iucv_sk(sk);
  137. switch (sk->sk_state) {
  138. case IUCV_DISCONN:
  139. case IUCV_CLOSING:
  140. case IUCV_CONNECTED:
  141. iucv_sever_path(sk, 0);
  142. break;
  143. case IUCV_OPEN:
  144. case IUCV_BOUND:
  145. case IUCV_LISTEN:
  146. case IUCV_CLOSED:
  147. default:
  148. break;
  149. }
  150. skb_queue_purge(&iucv->send_skb_q);
  151. skb_queue_purge(&iucv->backlog_skb_q);
  152. }
  153. read_unlock(&iucv_sk_list.lock);
  154. return err;
  155. }
  156. /**
  157. * afiucv_pm_restore_thaw() - Thaw and restore PM callback
  158. * @dev: AFIUCV dummy device
  159. *
  160. * socket clean up after freeze
  161. */
  162. static int afiucv_pm_restore_thaw(struct device *dev)
  163. {
  164. struct sock *sk;
  165. #ifdef CONFIG_PM_DEBUG
  166. printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
  167. #endif
  168. read_lock(&iucv_sk_list.lock);
  169. sk_for_each(sk, &iucv_sk_list.head) {
  170. switch (sk->sk_state) {
  171. case IUCV_CONNECTED:
  172. sk->sk_err = EPIPE;
  173. sk->sk_state = IUCV_DISCONN;
  174. sk->sk_state_change(sk);
  175. break;
  176. case IUCV_DISCONN:
  177. case IUCV_CLOSING:
  178. case IUCV_LISTEN:
  179. case IUCV_BOUND:
  180. case IUCV_OPEN:
  181. default:
  182. break;
  183. }
  184. }
  185. read_unlock(&iucv_sk_list.lock);
  186. return 0;
  187. }
  188. static const struct dev_pm_ops afiucv_pm_ops = {
  189. .prepare = afiucv_pm_prepare,
  190. .complete = afiucv_pm_complete,
  191. .freeze = afiucv_pm_freeze,
  192. .thaw = afiucv_pm_restore_thaw,
  193. .restore = afiucv_pm_restore_thaw,
  194. };
  195. static struct device_driver af_iucv_driver = {
  196. .owner = THIS_MODULE,
  197. .name = "afiucv",
  198. .bus = NULL,
  199. .pm = &afiucv_pm_ops,
  200. };
  201. /* dummy device used as trigger for PM functions */
  202. static struct device *af_iucv_dev;
  203. /**
  204. * iucv_msg_length() - Returns the length of an iucv message.
  205. * @msg: Pointer to struct iucv_message, MUST NOT be NULL
  206. *
  207. * The function returns the length of the specified iucv message @msg of data
  208. * stored in a buffer and of data stored in the parameter list (PRMDATA).
  209. *
  210. * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
  211. * data:
  212. * PRMDATA[0..6] socket data (max 7 bytes);
  213. * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
  214. *
  215. * The socket data length is computed by subtracting the socket data length
  216. * value from 0xFF.
  217. * If the socket data len is greater 7, then PRMDATA can be used for special
  218. * notifications (see iucv_sock_shutdown); and further,
  219. * if the socket data len is > 7, the function returns 8.
  220. *
  221. * Use this function to allocate socket buffers to store iucv message data.
  222. */
  223. static inline size_t iucv_msg_length(struct iucv_message *msg)
  224. {
  225. size_t datalen;
  226. if (msg->flags & IUCV_IPRMDATA) {
  227. datalen = 0xff - msg->rmmsg[7];
  228. return (datalen < 8) ? datalen : 8;
  229. }
  230. return msg->length;
  231. }
  232. /**
  233. * iucv_sock_in_state() - check for specific states
  234. * @sk: sock structure
  235. * @state: first iucv sk state
  236. * @state: second iucv sk state
  237. *
  238. * Returns true if the socket in either in the first or second state.
  239. */
  240. static int iucv_sock_in_state(struct sock *sk, int state, int state2)
  241. {
  242. return (sk->sk_state == state || sk->sk_state == state2);
  243. }
  244. /**
  245. * iucv_below_msglim() - function to check if messages can be sent
  246. * @sk: sock structure
  247. *
  248. * Returns true if the send queue length is lower than the message limit.
  249. * Always returns true if the socket is not connected (no iucv path for
  250. * checking the message limit).
  251. */
  252. static inline int iucv_below_msglim(struct sock *sk)
  253. {
  254. struct iucv_sock *iucv = iucv_sk(sk);
  255. if (sk->sk_state != IUCV_CONNECTED)
  256. return 1;
  257. if (iucv->transport == AF_IUCV_TRANS_IUCV)
  258. return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
  259. else
  260. return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
  261. (atomic_read(&iucv->pendings) <= 0));
  262. }
  263. /**
  264. * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
  265. */
  266. static void iucv_sock_wake_msglim(struct sock *sk)
  267. {
  268. struct socket_wq *wq;
  269. rcu_read_lock();
  270. wq = rcu_dereference(sk->sk_wq);
  271. if (wq_has_sleeper(wq))
  272. wake_up_interruptible_all(&wq->wait);
  273. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  274. rcu_read_unlock();
  275. }
  276. /**
  277. * afiucv_hs_send() - send a message through HiperSockets transport
  278. */
  279. static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
  280. struct sk_buff *skb, u8 flags)
  281. {
  282. struct iucv_sock *iucv = iucv_sk(sock);
  283. struct af_iucv_trans_hdr *phs_hdr;
  284. struct sk_buff *nskb;
  285. int err, confirm_recv = 0;
  286. memset(skb->head, 0, ETH_HLEN);
  287. phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
  288. 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. return -ENODEV;
  318. if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
  319. return -ENETDOWN;
  320. if (skb->len > skb->dev->mtu) {
  321. if (sock->sk_type == SOCK_SEQPACKET)
  322. return -EMSGSIZE;
  323. else
  324. skb_trim(skb, skb->dev->mtu);
  325. }
  326. skb->protocol = ETH_P_AF_IUCV;
  327. nskb = skb_clone(skb, GFP_ATOMIC);
  328. if (!nskb)
  329. return -ENOMEM;
  330. skb_queue_tail(&iucv->send_skb_q, nskb);
  331. err = dev_queue_xmit(skb);
  332. if (net_xmit_eval(err)) {
  333. skb_unlink(nskb, &iucv->send_skb_q);
  334. kfree_skb(nskb);
  335. } else {
  336. atomic_sub(confirm_recv, &iucv->msg_recv);
  337. WARN_ON(atomic_read(&iucv->msg_recv) < 0);
  338. }
  339. return net_xmit_eval(err);
  340. }
  341. static struct sock *__iucv_get_sock_by_name(char *nm)
  342. {
  343. struct sock *sk;
  344. sk_for_each(sk, &iucv_sk_list.head)
  345. if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
  346. return sk;
  347. return NULL;
  348. }
  349. static void iucv_sock_destruct(struct sock *sk)
  350. {
  351. skb_queue_purge(&sk->sk_receive_queue);
  352. skb_queue_purge(&sk->sk_error_queue);
  353. sk_mem_reclaim(sk);
  354. if (!sock_flag(sk, SOCK_DEAD)) {
  355. pr_err("Attempt to release alive iucv socket %p\n", sk);
  356. return;
  357. }
  358. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  359. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  360. WARN_ON(sk->sk_wmem_queued);
  361. WARN_ON(sk->sk_forward_alloc);
  362. }
  363. /* Cleanup Listen */
  364. static void iucv_sock_cleanup_listen(struct sock *parent)
  365. {
  366. struct sock *sk;
  367. /* Close non-accepted connections */
  368. while ((sk = iucv_accept_dequeue(parent, NULL))) {
  369. iucv_sock_close(sk);
  370. iucv_sock_kill(sk);
  371. }
  372. parent->sk_state = IUCV_CLOSED;
  373. }
  374. /* Kill socket (only if zapped and orphaned) */
  375. static void iucv_sock_kill(struct sock *sk)
  376. {
  377. if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
  378. return;
  379. iucv_sock_unlink(&iucv_sk_list, sk);
  380. sock_set_flag(sk, SOCK_DEAD);
  381. sock_put(sk);
  382. }
  383. /* Terminate an IUCV path */
  384. static void iucv_sever_path(struct sock *sk, int with_user_data)
  385. {
  386. unsigned char user_data[16];
  387. struct iucv_sock *iucv = iucv_sk(sk);
  388. struct iucv_path *path = iucv->path;
  389. if (iucv->path) {
  390. iucv->path = NULL;
  391. if (with_user_data) {
  392. low_nmcpy(user_data, iucv->src_name);
  393. high_nmcpy(user_data, iucv->dst_name);
  394. ASCEBC(user_data, sizeof(user_data));
  395. pr_iucv->path_sever(path, user_data);
  396. } else
  397. pr_iucv->path_sever(path, NULL);
  398. iucv_path_free(path);
  399. }
  400. }
  401. /* Send FIN through an IUCV socket for HIPER transport */
  402. static int iucv_send_ctrl(struct sock *sk, u8 flags)
  403. {
  404. int err = 0;
  405. int blen;
  406. struct sk_buff *skb;
  407. blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
  408. skb = sock_alloc_send_skb(sk, blen, 1, &err);
  409. if (skb) {
  410. skb_reserve(skb, blen);
  411. err = afiucv_hs_send(NULL, sk, skb, flags);
  412. }
  413. return err;
  414. }
  415. /* Close an IUCV socket */
  416. static void iucv_sock_close(struct sock *sk)
  417. {
  418. struct iucv_sock *iucv = iucv_sk(sk);
  419. unsigned long timeo;
  420. int err = 0;
  421. lock_sock(sk);
  422. switch (sk->sk_state) {
  423. case IUCV_LISTEN:
  424. iucv_sock_cleanup_listen(sk);
  425. break;
  426. case IUCV_CONNECTED:
  427. if (iucv->transport == AF_IUCV_TRANS_HIPER) {
  428. err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
  429. sk->sk_state = IUCV_DISCONN;
  430. sk->sk_state_change(sk);
  431. }
  432. case IUCV_DISCONN: /* fall through */
  433. sk->sk_state = IUCV_CLOSING;
  434. sk->sk_state_change(sk);
  435. if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
  436. if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
  437. timeo = sk->sk_lingertime;
  438. else
  439. timeo = IUCV_DISCONN_TIMEOUT;
  440. iucv_sock_wait(sk,
  441. iucv_sock_in_state(sk, IUCV_CLOSED, 0),
  442. timeo);
  443. }
  444. case IUCV_CLOSING: /* fall through */
  445. sk->sk_state = IUCV_CLOSED;
  446. sk->sk_state_change(sk);
  447. sk->sk_err = ECONNRESET;
  448. sk->sk_state_change(sk);
  449. skb_queue_purge(&iucv->send_skb_q);
  450. skb_queue_purge(&iucv->backlog_skb_q);
  451. default: /* fall through */
  452. iucv_sever_path(sk, 1);
  453. }
  454. if (iucv->hs_dev) {
  455. dev_put(iucv->hs_dev);
  456. iucv->hs_dev = NULL;
  457. sk->sk_bound_dev_if = 0;
  458. }
  459. /* mark socket for deletion by iucv_sock_kill() */
  460. sock_set_flag(sk, SOCK_ZAPPED);
  461. release_sock(sk);
  462. }
  463. static void iucv_sock_init(struct sock *sk, struct sock *parent)
  464. {
  465. if (parent)
  466. sk->sk_type = parent->sk_type;
  467. }
  468. static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
  469. {
  470. struct sock *sk;
  471. struct iucv_sock *iucv;
  472. sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
  473. if (!sk)
  474. return NULL;
  475. iucv = iucv_sk(sk);
  476. sock_init_data(sock, sk);
  477. INIT_LIST_HEAD(&iucv->accept_q);
  478. spin_lock_init(&iucv->accept_q_lock);
  479. skb_queue_head_init(&iucv->send_skb_q);
  480. INIT_LIST_HEAD(&iucv->message_q.list);
  481. spin_lock_init(&iucv->message_q.lock);
  482. skb_queue_head_init(&iucv->backlog_skb_q);
  483. iucv->send_tag = 0;
  484. atomic_set(&iucv->pendings, 0);
  485. iucv->flags = 0;
  486. iucv->msglimit = 0;
  487. atomic_set(&iucv->msg_sent, 0);
  488. atomic_set(&iucv->msg_recv, 0);
  489. iucv->path = NULL;
  490. iucv->sk_txnotify = afiucv_hs_callback_txnotify;
  491. memset(&iucv->src_user_id , 0, 32);
  492. if (pr_iucv)
  493. iucv->transport = AF_IUCV_TRANS_IUCV;
  494. else
  495. iucv->transport = AF_IUCV_TRANS_HIPER;
  496. sk->sk_destruct = iucv_sock_destruct;
  497. sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
  498. sk->sk_allocation = GFP_DMA;
  499. sock_reset_flag(sk, SOCK_ZAPPED);
  500. sk->sk_protocol = proto;
  501. sk->sk_state = IUCV_OPEN;
  502. iucv_sock_link(&iucv_sk_list, sk);
  503. return sk;
  504. }
  505. /* Create an IUCV socket */
  506. static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
  507. int kern)
  508. {
  509. struct sock *sk;
  510. if (protocol && protocol != PF_IUCV)
  511. return -EPROTONOSUPPORT;
  512. sock->state = SS_UNCONNECTED;
  513. switch (sock->type) {
  514. case SOCK_STREAM:
  515. sock->ops = &iucv_sock_ops;
  516. break;
  517. case SOCK_SEQPACKET:
  518. /* currently, proto ops can handle both sk types */
  519. sock->ops = &iucv_sock_ops;
  520. break;
  521. default:
  522. return -ESOCKTNOSUPPORT;
  523. }
  524. sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
  525. if (!sk)
  526. return -ENOMEM;
  527. iucv_sock_init(sk, NULL);
  528. return 0;
  529. }
  530. void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
  531. {
  532. write_lock_bh(&l->lock);
  533. sk_add_node(sk, &l->head);
  534. write_unlock_bh(&l->lock);
  535. }
  536. void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
  537. {
  538. write_lock_bh(&l->lock);
  539. sk_del_node_init(sk);
  540. write_unlock_bh(&l->lock);
  541. }
  542. void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
  543. {
  544. unsigned long flags;
  545. struct iucv_sock *par = iucv_sk(parent);
  546. sock_hold(sk);
  547. spin_lock_irqsave(&par->accept_q_lock, flags);
  548. list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
  549. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  550. iucv_sk(sk)->parent = parent;
  551. sk_acceptq_added(parent);
  552. }
  553. void iucv_accept_unlink(struct sock *sk)
  554. {
  555. unsigned long flags;
  556. struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
  557. spin_lock_irqsave(&par->accept_q_lock, flags);
  558. list_del_init(&iucv_sk(sk)->accept_q);
  559. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  560. sk_acceptq_removed(iucv_sk(sk)->parent);
  561. iucv_sk(sk)->parent = NULL;
  562. sock_put(sk);
  563. }
  564. struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
  565. {
  566. struct iucv_sock *isk, *n;
  567. struct sock *sk;
  568. list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
  569. sk = (struct sock *) isk;
  570. lock_sock(sk);
  571. if (sk->sk_state == IUCV_CLOSED) {
  572. iucv_accept_unlink(sk);
  573. release_sock(sk);
  574. continue;
  575. }
  576. if (sk->sk_state == IUCV_CONNECTED ||
  577. sk->sk_state == IUCV_DISCONN ||
  578. !newsock) {
  579. iucv_accept_unlink(sk);
  580. if (newsock)
  581. sock_graft(sk, newsock);
  582. release_sock(sk);
  583. return sk;
  584. }
  585. release_sock(sk);
  586. }
  587. return NULL;
  588. }
  589. static void __iucv_auto_name(struct iucv_sock *iucv)
  590. {
  591. char name[12];
  592. sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
  593. while (__iucv_get_sock_by_name(name)) {
  594. sprintf(name, "%08x",
  595. atomic_inc_return(&iucv_sk_list.autobind_name));
  596. }
  597. memcpy(iucv->src_name, name, 8);
  598. }
  599. /* Bind an unbound socket */
  600. static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
  601. int addr_len)
  602. {
  603. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  604. struct sock *sk = sock->sk;
  605. struct iucv_sock *iucv;
  606. int err = 0;
  607. struct net_device *dev;
  608. char uid[9];
  609. /* Verify the input sockaddr */
  610. if (addr_len < sizeof(struct sockaddr_iucv) ||
  611. 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 (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_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. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  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_iucv_dev;
  2058. return 0;
  2059. out_iucv_dev:
  2060. put_device(af_iucv_dev);
  2061. out_driver:
  2062. driver_unregister(&af_iucv_driver);
  2063. out_iucv:
  2064. pr_iucv->iucv_unregister(&af_iucv_handler, 0);
  2065. out:
  2066. return err;
  2067. }
  2068. static int __init afiucv_init(void)
  2069. {
  2070. int err;
  2071. if (MACHINE_IS_VM) {
  2072. cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
  2073. if (unlikely(err)) {
  2074. WARN_ON(err);
  2075. err = -EPROTONOSUPPORT;
  2076. goto out;
  2077. }
  2078. pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
  2079. if (!pr_iucv) {
  2080. printk(KERN_WARNING "iucv_if lookup failed\n");
  2081. memset(&iucv_userid, 0, sizeof(iucv_userid));
  2082. }
  2083. } else {
  2084. memset(&iucv_userid, 0, sizeof(iucv_userid));
  2085. pr_iucv = NULL;
  2086. }
  2087. err = proto_register(&iucv_proto, 0);
  2088. if (err)
  2089. goto out;
  2090. err = sock_register(&iucv_sock_family_ops);
  2091. if (err)
  2092. goto out_proto;
  2093. if (pr_iucv) {
  2094. err = afiucv_iucv_init();
  2095. if (err)
  2096. goto out_sock;
  2097. } else
  2098. register_netdevice_notifier(&afiucv_netdev_notifier);
  2099. dev_add_pack(&iucv_packet_type);
  2100. return 0;
  2101. out_sock:
  2102. sock_unregister(PF_IUCV);
  2103. out_proto:
  2104. proto_unregister(&iucv_proto);
  2105. out:
  2106. if (pr_iucv)
  2107. symbol_put(iucv_if);
  2108. return err;
  2109. }
  2110. static void __exit afiucv_exit(void)
  2111. {
  2112. if (pr_iucv) {
  2113. device_unregister(af_iucv_dev);
  2114. driver_unregister(&af_iucv_driver);
  2115. pr_iucv->iucv_unregister(&af_iucv_handler, 0);
  2116. symbol_put(iucv_if);
  2117. } else
  2118. unregister_netdevice_notifier(&afiucv_netdev_notifier);
  2119. dev_remove_pack(&iucv_packet_type);
  2120. sock_unregister(PF_IUCV);
  2121. proto_unregister(&iucv_proto);
  2122. }
  2123. module_init(afiucv_init);
  2124. module_exit(afiucv_exit);
  2125. MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
  2126. MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
  2127. MODULE_VERSION(VERSION);
  2128. MODULE_LICENSE("GPL");
  2129. MODULE_ALIAS_NETPROTO(PF_IUCV);