af_iucv.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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/iucv.h>
  28. #include <net/iucv/af_iucv.h>
  29. #define VERSION "1.1"
  30. static char iucv_userid[80];
  31. static const struct proto_ops iucv_sock_ops;
  32. static struct proto iucv_proto = {
  33. .name = "AF_IUCV",
  34. .owner = THIS_MODULE,
  35. .obj_size = sizeof(struct iucv_sock),
  36. };
  37. /* special AF_IUCV IPRM messages */
  38. static const u8 iprm_shutdown[8] =
  39. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  40. #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
  41. /* macros to set/get socket control buffer at correct offset */
  42. #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
  43. #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
  44. #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
  45. #define CB_TRGCLS_LEN (TRGCLS_SIZE)
  46. #define __iucv_sock_wait(sk, condition, timeo, ret) \
  47. do { \
  48. DEFINE_WAIT(__wait); \
  49. long __timeo = timeo; \
  50. ret = 0; \
  51. prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
  52. while (!(condition)) { \
  53. if (!__timeo) { \
  54. ret = -EAGAIN; \
  55. break; \
  56. } \
  57. if (signal_pending(current)) { \
  58. ret = sock_intr_errno(__timeo); \
  59. break; \
  60. } \
  61. release_sock(sk); \
  62. __timeo = schedule_timeout(__timeo); \
  63. lock_sock(sk); \
  64. ret = sock_error(sk); \
  65. if (ret) \
  66. break; \
  67. } \
  68. finish_wait(sk_sleep(sk), &__wait); \
  69. } while (0)
  70. #define iucv_sock_wait(sk, condition, timeo) \
  71. ({ \
  72. int __ret = 0; \
  73. if (!(condition)) \
  74. __iucv_sock_wait(sk, condition, timeo, __ret); \
  75. __ret; \
  76. })
  77. static void iucv_sock_kill(struct sock *sk);
  78. static void iucv_sock_close(struct sock *sk);
  79. /* Call Back functions */
  80. static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
  81. static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
  82. static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
  83. static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
  84. u8 ipuser[16]);
  85. static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
  86. static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
  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. struct hlist_node *node;
  131. int err = 0;
  132. #ifdef CONFIG_PM_DEBUG
  133. printk(KERN_WARNING "afiucv_pm_freeze\n");
  134. #endif
  135. read_lock(&iucv_sk_list.lock);
  136. sk_for_each(sk, node, &iucv_sk_list.head) {
  137. iucv = iucv_sk(sk);
  138. skb_queue_purge(&iucv->send_skb_q);
  139. skb_queue_purge(&iucv->backlog_skb_q);
  140. switch (sk->sk_state) {
  141. case IUCV_SEVERED:
  142. case IUCV_DISCONN:
  143. case IUCV_CLOSING:
  144. case IUCV_CONNECTED:
  145. if (iucv->path) {
  146. err = iucv_path_sever(iucv->path, NULL);
  147. iucv_path_free(iucv->path);
  148. iucv->path = NULL;
  149. }
  150. break;
  151. case IUCV_OPEN:
  152. case IUCV_BOUND:
  153. case IUCV_LISTEN:
  154. case IUCV_CLOSED:
  155. default:
  156. break;
  157. }
  158. }
  159. read_unlock(&iucv_sk_list.lock);
  160. return err;
  161. }
  162. /**
  163. * afiucv_pm_restore_thaw() - Thaw and restore PM callback
  164. * @dev: AFIUCV dummy device
  165. *
  166. * socket clean up after freeze
  167. */
  168. static int afiucv_pm_restore_thaw(struct device *dev)
  169. {
  170. struct sock *sk;
  171. struct hlist_node *node;
  172. #ifdef CONFIG_PM_DEBUG
  173. printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
  174. #endif
  175. read_lock(&iucv_sk_list.lock);
  176. sk_for_each(sk, node, &iucv_sk_list.head) {
  177. switch (sk->sk_state) {
  178. case IUCV_CONNECTED:
  179. sk->sk_err = EPIPE;
  180. sk->sk_state = IUCV_DISCONN;
  181. sk->sk_state_change(sk);
  182. break;
  183. case IUCV_DISCONN:
  184. case IUCV_SEVERED:
  185. case IUCV_CLOSING:
  186. case IUCV_LISTEN:
  187. case IUCV_BOUND:
  188. case IUCV_OPEN:
  189. default:
  190. break;
  191. }
  192. }
  193. read_unlock(&iucv_sk_list.lock);
  194. return 0;
  195. }
  196. static const struct dev_pm_ops afiucv_pm_ops = {
  197. .prepare = afiucv_pm_prepare,
  198. .complete = afiucv_pm_complete,
  199. .freeze = afiucv_pm_freeze,
  200. .thaw = afiucv_pm_restore_thaw,
  201. .restore = afiucv_pm_restore_thaw,
  202. };
  203. static struct device_driver af_iucv_driver = {
  204. .owner = THIS_MODULE,
  205. .name = "afiucv",
  206. .bus = &iucv_bus,
  207. .pm = &afiucv_pm_ops,
  208. };
  209. /* dummy device used as trigger for PM functions */
  210. static struct device *af_iucv_dev;
  211. /**
  212. * iucv_msg_length() - Returns the length of an iucv message.
  213. * @msg: Pointer to struct iucv_message, MUST NOT be NULL
  214. *
  215. * The function returns the length of the specified iucv message @msg of data
  216. * stored in a buffer and of data stored in the parameter list (PRMDATA).
  217. *
  218. * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
  219. * data:
  220. * PRMDATA[0..6] socket data (max 7 bytes);
  221. * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
  222. *
  223. * The socket data length is computed by subtracting the socket data length
  224. * value from 0xFF.
  225. * If the socket data len is greater 7, then PRMDATA can be used for special
  226. * notifications (see iucv_sock_shutdown); and further,
  227. * if the socket data len is > 7, the function returns 8.
  228. *
  229. * Use this function to allocate socket buffers to store iucv message data.
  230. */
  231. static inline size_t iucv_msg_length(struct iucv_message *msg)
  232. {
  233. size_t datalen;
  234. if (msg->flags & IUCV_IPRMDATA) {
  235. datalen = 0xff - msg->rmmsg[7];
  236. return (datalen < 8) ? datalen : 8;
  237. }
  238. return msg->length;
  239. }
  240. /**
  241. * iucv_sock_in_state() - check for specific states
  242. * @sk: sock structure
  243. * @state: first iucv sk state
  244. * @state: second iucv sk state
  245. *
  246. * Returns true if the socket in either in the first or second state.
  247. */
  248. static int iucv_sock_in_state(struct sock *sk, int state, int state2)
  249. {
  250. return (sk->sk_state == state || sk->sk_state == state2);
  251. }
  252. /**
  253. * iucv_below_msglim() - function to check if messages can be sent
  254. * @sk: sock structure
  255. *
  256. * Returns true if the send queue length is lower than the message limit.
  257. * Always returns true if the socket is not connected (no iucv path for
  258. * checking the message limit).
  259. */
  260. static inline int iucv_below_msglim(struct sock *sk)
  261. {
  262. struct iucv_sock *iucv = iucv_sk(sk);
  263. if (sk->sk_state != IUCV_CONNECTED)
  264. return 1;
  265. return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
  266. }
  267. /**
  268. * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
  269. */
  270. static void iucv_sock_wake_msglim(struct sock *sk)
  271. {
  272. struct socket_wq *wq;
  273. rcu_read_lock();
  274. wq = rcu_dereference(sk->sk_wq);
  275. if (wq_has_sleeper(wq))
  276. wake_up_interruptible_all(&wq->wait);
  277. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  278. rcu_read_unlock();
  279. }
  280. /* Timers */
  281. static void iucv_sock_timeout(unsigned long arg)
  282. {
  283. struct sock *sk = (struct sock *)arg;
  284. bh_lock_sock(sk);
  285. sk->sk_err = ETIMEDOUT;
  286. sk->sk_state_change(sk);
  287. bh_unlock_sock(sk);
  288. iucv_sock_kill(sk);
  289. sock_put(sk);
  290. }
  291. static void iucv_sock_clear_timer(struct sock *sk)
  292. {
  293. sk_stop_timer(sk, &sk->sk_timer);
  294. }
  295. static struct sock *__iucv_get_sock_by_name(char *nm)
  296. {
  297. struct sock *sk;
  298. struct hlist_node *node;
  299. sk_for_each(sk, node, &iucv_sk_list.head)
  300. if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
  301. return sk;
  302. return NULL;
  303. }
  304. static void iucv_sock_destruct(struct sock *sk)
  305. {
  306. skb_queue_purge(&sk->sk_receive_queue);
  307. skb_queue_purge(&sk->sk_write_queue);
  308. }
  309. /* Cleanup Listen */
  310. static void iucv_sock_cleanup_listen(struct sock *parent)
  311. {
  312. struct sock *sk;
  313. /* Close non-accepted connections */
  314. while ((sk = iucv_accept_dequeue(parent, NULL))) {
  315. iucv_sock_close(sk);
  316. iucv_sock_kill(sk);
  317. }
  318. parent->sk_state = IUCV_CLOSED;
  319. }
  320. /* Kill socket (only if zapped and orphaned) */
  321. static void iucv_sock_kill(struct sock *sk)
  322. {
  323. if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
  324. return;
  325. iucv_sock_unlink(&iucv_sk_list, sk);
  326. sock_set_flag(sk, SOCK_DEAD);
  327. sock_put(sk);
  328. }
  329. /* Close an IUCV socket */
  330. static void iucv_sock_close(struct sock *sk)
  331. {
  332. unsigned char user_data[16];
  333. struct iucv_sock *iucv = iucv_sk(sk);
  334. unsigned long timeo;
  335. iucv_sock_clear_timer(sk);
  336. lock_sock(sk);
  337. switch (sk->sk_state) {
  338. case IUCV_LISTEN:
  339. iucv_sock_cleanup_listen(sk);
  340. break;
  341. case IUCV_CONNECTED:
  342. case IUCV_DISCONN:
  343. sk->sk_state = IUCV_CLOSING;
  344. sk->sk_state_change(sk);
  345. if (!skb_queue_empty(&iucv->send_skb_q)) {
  346. if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
  347. timeo = sk->sk_lingertime;
  348. else
  349. timeo = IUCV_DISCONN_TIMEOUT;
  350. iucv_sock_wait(sk,
  351. iucv_sock_in_state(sk, IUCV_CLOSED, 0),
  352. timeo);
  353. }
  354. case IUCV_CLOSING: /* fall through */
  355. sk->sk_state = IUCV_CLOSED;
  356. sk->sk_state_change(sk);
  357. if (iucv->path) {
  358. low_nmcpy(user_data, iucv->src_name);
  359. high_nmcpy(user_data, iucv->dst_name);
  360. ASCEBC(user_data, sizeof(user_data));
  361. iucv_path_sever(iucv->path, user_data);
  362. iucv_path_free(iucv->path);
  363. iucv->path = NULL;
  364. }
  365. sk->sk_err = ECONNRESET;
  366. sk->sk_state_change(sk);
  367. skb_queue_purge(&iucv->send_skb_q);
  368. skb_queue_purge(&iucv->backlog_skb_q);
  369. break;
  370. default:
  371. /* nothing to do here */
  372. break;
  373. }
  374. /* mark socket for deletion by iucv_sock_kill() */
  375. sock_set_flag(sk, SOCK_ZAPPED);
  376. release_sock(sk);
  377. }
  378. static void iucv_sock_init(struct sock *sk, struct sock *parent)
  379. {
  380. if (parent)
  381. sk->sk_type = parent->sk_type;
  382. }
  383. static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
  384. {
  385. struct sock *sk;
  386. sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
  387. if (!sk)
  388. return NULL;
  389. sock_init_data(sock, sk);
  390. INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
  391. spin_lock_init(&iucv_sk(sk)->accept_q_lock);
  392. skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
  393. INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
  394. spin_lock_init(&iucv_sk(sk)->message_q.lock);
  395. skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
  396. iucv_sk(sk)->send_tag = 0;
  397. iucv_sk(sk)->flags = 0;
  398. iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
  399. iucv_sk(sk)->path = NULL;
  400. memset(&iucv_sk(sk)->src_user_id , 0, 32);
  401. sk->sk_destruct = iucv_sock_destruct;
  402. sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
  403. sk->sk_allocation = GFP_DMA;
  404. sock_reset_flag(sk, SOCK_ZAPPED);
  405. sk->sk_protocol = proto;
  406. sk->sk_state = IUCV_OPEN;
  407. setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
  408. iucv_sock_link(&iucv_sk_list, sk);
  409. return sk;
  410. }
  411. /* Create an IUCV socket */
  412. static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
  413. int kern)
  414. {
  415. struct sock *sk;
  416. if (protocol && protocol != PF_IUCV)
  417. return -EPROTONOSUPPORT;
  418. sock->state = SS_UNCONNECTED;
  419. switch (sock->type) {
  420. case SOCK_STREAM:
  421. sock->ops = &iucv_sock_ops;
  422. break;
  423. case SOCK_SEQPACKET:
  424. /* currently, proto ops can handle both sk types */
  425. sock->ops = &iucv_sock_ops;
  426. break;
  427. default:
  428. return -ESOCKTNOSUPPORT;
  429. }
  430. sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
  431. if (!sk)
  432. return -ENOMEM;
  433. iucv_sock_init(sk, NULL);
  434. return 0;
  435. }
  436. void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
  437. {
  438. write_lock_bh(&l->lock);
  439. sk_add_node(sk, &l->head);
  440. write_unlock_bh(&l->lock);
  441. }
  442. void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
  443. {
  444. write_lock_bh(&l->lock);
  445. sk_del_node_init(sk);
  446. write_unlock_bh(&l->lock);
  447. }
  448. void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
  449. {
  450. unsigned long flags;
  451. struct iucv_sock *par = iucv_sk(parent);
  452. sock_hold(sk);
  453. spin_lock_irqsave(&par->accept_q_lock, flags);
  454. list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
  455. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  456. iucv_sk(sk)->parent = parent;
  457. sk_acceptq_added(parent);
  458. }
  459. void iucv_accept_unlink(struct sock *sk)
  460. {
  461. unsigned long flags;
  462. struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
  463. spin_lock_irqsave(&par->accept_q_lock, flags);
  464. list_del_init(&iucv_sk(sk)->accept_q);
  465. spin_unlock_irqrestore(&par->accept_q_lock, flags);
  466. sk_acceptq_removed(iucv_sk(sk)->parent);
  467. iucv_sk(sk)->parent = NULL;
  468. sock_put(sk);
  469. }
  470. struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
  471. {
  472. struct iucv_sock *isk, *n;
  473. struct sock *sk;
  474. list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
  475. sk = (struct sock *) isk;
  476. lock_sock(sk);
  477. if (sk->sk_state == IUCV_CLOSED) {
  478. iucv_accept_unlink(sk);
  479. release_sock(sk);
  480. continue;
  481. }
  482. if (sk->sk_state == IUCV_CONNECTED ||
  483. sk->sk_state == IUCV_SEVERED ||
  484. sk->sk_state == IUCV_DISCONN || /* due to PM restore */
  485. !newsock) {
  486. iucv_accept_unlink(sk);
  487. if (newsock)
  488. sock_graft(sk, newsock);
  489. if (sk->sk_state == IUCV_SEVERED)
  490. sk->sk_state = IUCV_DISCONN;
  491. release_sock(sk);
  492. return sk;
  493. }
  494. release_sock(sk);
  495. }
  496. return NULL;
  497. }
  498. /* Bind an unbound socket */
  499. static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
  500. int addr_len)
  501. {
  502. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  503. struct sock *sk = sock->sk;
  504. struct iucv_sock *iucv;
  505. int err;
  506. /* Verify the input sockaddr */
  507. if (!addr || addr->sa_family != AF_IUCV)
  508. return -EINVAL;
  509. lock_sock(sk);
  510. if (sk->sk_state != IUCV_OPEN) {
  511. err = -EBADFD;
  512. goto done;
  513. }
  514. write_lock_bh(&iucv_sk_list.lock);
  515. iucv = iucv_sk(sk);
  516. if (__iucv_get_sock_by_name(sa->siucv_name)) {
  517. err = -EADDRINUSE;
  518. goto done_unlock;
  519. }
  520. if (iucv->path) {
  521. err = 0;
  522. goto done_unlock;
  523. }
  524. /* Bind the socket */
  525. memcpy(iucv->src_name, sa->siucv_name, 8);
  526. /* Copy the user id */
  527. memcpy(iucv->src_user_id, iucv_userid, 8);
  528. sk->sk_state = IUCV_BOUND;
  529. err = 0;
  530. done_unlock:
  531. /* Release the socket list lock */
  532. write_unlock_bh(&iucv_sk_list.lock);
  533. done:
  534. release_sock(sk);
  535. return err;
  536. }
  537. /* Automatically bind an unbound socket */
  538. static int iucv_sock_autobind(struct sock *sk)
  539. {
  540. struct iucv_sock *iucv = iucv_sk(sk);
  541. char query_buffer[80];
  542. char name[12];
  543. int err = 0;
  544. /* Set the userid and name */
  545. cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
  546. if (unlikely(err))
  547. return -EPROTO;
  548. memcpy(iucv->src_user_id, query_buffer, 8);
  549. write_lock_bh(&iucv_sk_list.lock);
  550. sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
  551. while (__iucv_get_sock_by_name(name)) {
  552. sprintf(name, "%08x",
  553. atomic_inc_return(&iucv_sk_list.autobind_name));
  554. }
  555. write_unlock_bh(&iucv_sk_list.lock);
  556. memcpy(&iucv->src_name, name, 8);
  557. return err;
  558. }
  559. /* Connect an unconnected socket */
  560. static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
  561. int alen, int flags)
  562. {
  563. struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
  564. struct sock *sk = sock->sk;
  565. struct iucv_sock *iucv;
  566. unsigned char user_data[16];
  567. int err;
  568. if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
  569. return -EINVAL;
  570. if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
  571. return -EBADFD;
  572. if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
  573. return -EINVAL;
  574. if (sk->sk_state == IUCV_OPEN) {
  575. err = iucv_sock_autobind(sk);
  576. if (unlikely(err))
  577. return err;
  578. }
  579. lock_sock(sk);
  580. /* Set the destination information */
  581. memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
  582. memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
  583. high_nmcpy(user_data, sa->siucv_name);
  584. low_nmcpy(user_data, iucv_sk(sk)->src_name);
  585. ASCEBC(user_data, sizeof(user_data));
  586. iucv = iucv_sk(sk);
  587. /* Create path. */
  588. iucv->path = iucv_path_alloc(iucv->msglimit,
  589. IUCV_IPRMDATA, GFP_KERNEL);
  590. if (!iucv->path) {
  591. err = -ENOMEM;
  592. goto done;
  593. }
  594. err = iucv_path_connect(iucv->path, &af_iucv_handler,
  595. sa->siucv_user_id, NULL, user_data, sk);
  596. if (err) {
  597. iucv_path_free(iucv->path);
  598. iucv->path = NULL;
  599. switch (err) {
  600. case 0x0b: /* Target communicator is not logged on */
  601. err = -ENETUNREACH;
  602. break;
  603. case 0x0d: /* Max connections for this guest exceeded */
  604. case 0x0e: /* Max connections for target guest exceeded */
  605. err = -EAGAIN;
  606. break;
  607. case 0x0f: /* Missing IUCV authorization */
  608. err = -EACCES;
  609. break;
  610. default:
  611. err = -ECONNREFUSED;
  612. break;
  613. }
  614. goto done;
  615. }
  616. if (sk->sk_state != IUCV_CONNECTED) {
  617. err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
  618. IUCV_DISCONN),
  619. sock_sndtimeo(sk, flags & O_NONBLOCK));
  620. }
  621. if (sk->sk_state == IUCV_DISCONN) {
  622. err = -ECONNREFUSED;
  623. }
  624. if (err) {
  625. iucv_path_sever(iucv->path, NULL);
  626. iucv_path_free(iucv->path);
  627. iucv->path = NULL;
  628. }
  629. done:
  630. release_sock(sk);
  631. return err;
  632. }
  633. /* Move a socket into listening state. */
  634. static int iucv_sock_listen(struct socket *sock, int backlog)
  635. {
  636. struct sock *sk = sock->sk;
  637. int err;
  638. lock_sock(sk);
  639. err = -EINVAL;
  640. if (sk->sk_state != IUCV_BOUND)
  641. goto done;
  642. if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
  643. goto done;
  644. sk->sk_max_ack_backlog = backlog;
  645. sk->sk_ack_backlog = 0;
  646. sk->sk_state = IUCV_LISTEN;
  647. err = 0;
  648. done:
  649. release_sock(sk);
  650. return err;
  651. }
  652. /* Accept a pending connection */
  653. static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
  654. int flags)
  655. {
  656. DECLARE_WAITQUEUE(wait, current);
  657. struct sock *sk = sock->sk, *nsk;
  658. long timeo;
  659. int err = 0;
  660. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  661. if (sk->sk_state != IUCV_LISTEN) {
  662. err = -EBADFD;
  663. goto done;
  664. }
  665. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  666. /* Wait for an incoming connection */
  667. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  668. while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
  669. set_current_state(TASK_INTERRUPTIBLE);
  670. if (!timeo) {
  671. err = -EAGAIN;
  672. break;
  673. }
  674. release_sock(sk);
  675. timeo = schedule_timeout(timeo);
  676. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  677. if (sk->sk_state != IUCV_LISTEN) {
  678. err = -EBADFD;
  679. break;
  680. }
  681. if (signal_pending(current)) {
  682. err = sock_intr_errno(timeo);
  683. break;
  684. }
  685. }
  686. set_current_state(TASK_RUNNING);
  687. remove_wait_queue(sk_sleep(sk), &wait);
  688. if (err)
  689. goto done;
  690. newsock->state = SS_CONNECTED;
  691. done:
  692. release_sock(sk);
  693. return err;
  694. }
  695. static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
  696. int *len, int peer)
  697. {
  698. struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
  699. struct sock *sk = sock->sk;
  700. addr->sa_family = AF_IUCV;
  701. *len = sizeof(struct sockaddr_iucv);
  702. if (peer) {
  703. memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
  704. memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
  705. } else {
  706. memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
  707. memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
  708. }
  709. memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
  710. memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
  711. memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
  712. return 0;
  713. }
  714. /**
  715. * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
  716. * @path: IUCV path
  717. * @msg: Pointer to a struct iucv_message
  718. * @skb: The socket data to send, skb->len MUST BE <= 7
  719. *
  720. * Send the socket data in the parameter list in the iucv message
  721. * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
  722. * list and the socket data len at index 7 (last byte).
  723. * See also iucv_msg_length().
  724. *
  725. * Returns the error code from the iucv_message_send() call.
  726. */
  727. static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
  728. struct sk_buff *skb)
  729. {
  730. u8 prmdata[8];
  731. memcpy(prmdata, (void *) skb->data, skb->len);
  732. prmdata[7] = 0xff - (u8) skb->len;
  733. return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
  734. (void *) prmdata, 8);
  735. }
  736. static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  737. struct msghdr *msg, size_t len)
  738. {
  739. struct sock *sk = sock->sk;
  740. struct iucv_sock *iucv = iucv_sk(sk);
  741. struct sk_buff *skb;
  742. struct iucv_message txmsg;
  743. struct cmsghdr *cmsg;
  744. int cmsg_done;
  745. long timeo;
  746. char user_id[9];
  747. char appl_id[9];
  748. int err;
  749. int noblock = msg->msg_flags & MSG_DONTWAIT;
  750. err = sock_error(sk);
  751. if (err)
  752. return err;
  753. if (msg->msg_flags & MSG_OOB)
  754. return -EOPNOTSUPP;
  755. /* SOCK_SEQPACKET: we do not support segmented records */
  756. if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
  757. return -EOPNOTSUPP;
  758. lock_sock(sk);
  759. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  760. err = -EPIPE;
  761. goto out;
  762. }
  763. /* Return if the socket is not in connected state */
  764. if (sk->sk_state != IUCV_CONNECTED) {
  765. err = -ENOTCONN;
  766. goto out;
  767. }
  768. /* initialize defaults */
  769. cmsg_done = 0; /* check for duplicate headers */
  770. txmsg.class = 0;
  771. /* iterate over control messages */
  772. for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
  773. cmsg = CMSG_NXTHDR(msg, cmsg)) {
  774. if (!CMSG_OK(msg, cmsg)) {
  775. err = -EINVAL;
  776. goto out;
  777. }
  778. if (cmsg->cmsg_level != SOL_IUCV)
  779. continue;
  780. if (cmsg->cmsg_type & cmsg_done) {
  781. err = -EINVAL;
  782. goto out;
  783. }
  784. cmsg_done |= cmsg->cmsg_type;
  785. switch (cmsg->cmsg_type) {
  786. case SCM_IUCV_TRGCLS:
  787. if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
  788. err = -EINVAL;
  789. goto out;
  790. }
  791. /* set iucv message target class */
  792. memcpy(&txmsg.class,
  793. (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
  794. break;
  795. default:
  796. err = -EINVAL;
  797. goto out;
  798. break;
  799. }
  800. }
  801. /* allocate one skb for each iucv message:
  802. * this is fine for SOCK_SEQPACKET (unless we want to support
  803. * segmented records using the MSG_EOR flag), but
  804. * for SOCK_STREAM we might want to improve it in future */
  805. skb = sock_alloc_send_skb(sk, len, noblock, &err);
  806. if (!skb)
  807. goto out;
  808. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  809. err = -EFAULT;
  810. goto fail;
  811. }
  812. /* wait if outstanding messages for iucv path has reached */
  813. timeo = sock_sndtimeo(sk, noblock);
  814. err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
  815. if (err)
  816. goto fail;
  817. /* return -ECONNRESET if the socket is no longer connected */
  818. if (sk->sk_state != IUCV_CONNECTED) {
  819. err = -ECONNRESET;
  820. goto fail;
  821. }
  822. /* increment and save iucv message tag for msg_completion cbk */
  823. txmsg.tag = iucv->send_tag++;
  824. memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
  825. skb_queue_tail(&iucv->send_skb_q, skb);
  826. if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
  827. && skb->len <= 7) {
  828. err = iucv_send_iprm(iucv->path, &txmsg, skb);
  829. /* on success: there is no message_complete callback
  830. * for an IPRMDATA msg; remove skb from send queue */
  831. if (err == 0) {
  832. skb_unlink(skb, &iucv->send_skb_q);
  833. kfree_skb(skb);
  834. }
  835. /* this error should never happen since the
  836. * IUCV_IPRMDATA path flag is set... sever path */
  837. if (err == 0x15) {
  838. iucv_path_sever(iucv->path, NULL);
  839. skb_unlink(skb, &iucv->send_skb_q);
  840. err = -EPIPE;
  841. goto fail;
  842. }
  843. } else
  844. err = iucv_message_send(iucv->path, &txmsg, 0, 0,
  845. (void *) skb->data, skb->len);
  846. if (err) {
  847. if (err == 3) {
  848. user_id[8] = 0;
  849. memcpy(user_id, iucv->dst_user_id, 8);
  850. appl_id[8] = 0;
  851. memcpy(appl_id, iucv->dst_name, 8);
  852. pr_err("Application %s on z/VM guest %s"
  853. " exceeds message limit\n",
  854. appl_id, user_id);
  855. err = -EAGAIN;
  856. } else
  857. err = -EPIPE;
  858. skb_unlink(skb, &iucv->send_skb_q);
  859. goto fail;
  860. }
  861. release_sock(sk);
  862. return len;
  863. fail:
  864. kfree_skb(skb);
  865. out:
  866. release_sock(sk);
  867. return err;
  868. }
  869. /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
  870. *
  871. * Locking: must be called with message_q.lock held
  872. */
  873. static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
  874. {
  875. int dataleft, size, copied = 0;
  876. struct sk_buff *nskb;
  877. dataleft = len;
  878. while (dataleft) {
  879. if (dataleft >= sk->sk_rcvbuf / 4)
  880. size = sk->sk_rcvbuf / 4;
  881. else
  882. size = dataleft;
  883. nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
  884. if (!nskb)
  885. return -ENOMEM;
  886. /* copy target class to control buffer of new skb */
  887. memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
  888. /* copy data fragment */
  889. memcpy(nskb->data, skb->data + copied, size);
  890. copied += size;
  891. dataleft -= size;
  892. skb_reset_transport_header(nskb);
  893. skb_reset_network_header(nskb);
  894. nskb->len = size;
  895. skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
  896. }
  897. return 0;
  898. }
  899. /* iucv_process_message() - Receive a single outstanding IUCV message
  900. *
  901. * Locking: must be called with message_q.lock held
  902. */
  903. static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
  904. struct iucv_path *path,
  905. struct iucv_message *msg)
  906. {
  907. int rc;
  908. unsigned int len;
  909. len = iucv_msg_length(msg);
  910. /* store msg target class in the second 4 bytes of skb ctrl buffer */
  911. /* Note: the first 4 bytes are reserved for msg tag */
  912. memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
  913. /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
  914. if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
  915. if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
  916. skb->data = NULL;
  917. skb->len = 0;
  918. }
  919. } else {
  920. rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
  921. skb->data, len, NULL);
  922. if (rc) {
  923. kfree_skb(skb);
  924. return;
  925. }
  926. /* we need to fragment iucv messages for SOCK_STREAM only;
  927. * for SOCK_SEQPACKET, it is only relevant if we support
  928. * record segmentation using MSG_EOR (see also recvmsg()) */
  929. if (sk->sk_type == SOCK_STREAM &&
  930. skb->truesize >= sk->sk_rcvbuf / 4) {
  931. rc = iucv_fragment_skb(sk, skb, len);
  932. kfree_skb(skb);
  933. skb = NULL;
  934. if (rc) {
  935. iucv_path_sever(path, NULL);
  936. return;
  937. }
  938. skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
  939. } else {
  940. skb_reset_transport_header(skb);
  941. skb_reset_network_header(skb);
  942. skb->len = len;
  943. }
  944. }
  945. if (sock_queue_rcv_skb(sk, skb))
  946. skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
  947. }
  948. /* iucv_process_message_q() - Process outstanding IUCV messages
  949. *
  950. * Locking: must be called with message_q.lock held
  951. */
  952. static void iucv_process_message_q(struct sock *sk)
  953. {
  954. struct iucv_sock *iucv = iucv_sk(sk);
  955. struct sk_buff *skb;
  956. struct sock_msg_q *p, *n;
  957. list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
  958. skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
  959. if (!skb)
  960. break;
  961. iucv_process_message(sk, skb, p->path, &p->msg);
  962. list_del(&p->list);
  963. kfree(p);
  964. if (!skb_queue_empty(&iucv->backlog_skb_q))
  965. break;
  966. }
  967. }
  968. static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  969. struct msghdr *msg, size_t len, int flags)
  970. {
  971. int noblock = flags & MSG_DONTWAIT;
  972. struct sock *sk = sock->sk;
  973. struct iucv_sock *iucv = iucv_sk(sk);
  974. unsigned int copied, rlen;
  975. struct sk_buff *skb, *rskb, *cskb;
  976. int err = 0;
  977. if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
  978. skb_queue_empty(&iucv->backlog_skb_q) &&
  979. skb_queue_empty(&sk->sk_receive_queue) &&
  980. list_empty(&iucv->message_q.list))
  981. return 0;
  982. if (flags & (MSG_OOB))
  983. return -EOPNOTSUPP;
  984. /* receive/dequeue next skb:
  985. * the function understands MSG_PEEK and, thus, does not dequeue skb */
  986. skb = skb_recv_datagram(sk, flags, noblock, &err);
  987. if (!skb) {
  988. if (sk->sk_shutdown & RCV_SHUTDOWN)
  989. return 0;
  990. return err;
  991. }
  992. rlen = skb->len; /* real length of skb */
  993. copied = min_t(unsigned int, rlen, len);
  994. cskb = skb;
  995. if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
  996. if (!(flags & MSG_PEEK))
  997. skb_queue_head(&sk->sk_receive_queue, skb);
  998. return -EFAULT;
  999. }
  1000. /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
  1001. if (sk->sk_type == SOCK_SEQPACKET) {
  1002. if (copied < rlen)
  1003. msg->msg_flags |= MSG_TRUNC;
  1004. /* each iucv message contains a complete record */
  1005. msg->msg_flags |= MSG_EOR;
  1006. }
  1007. /* create control message to store iucv msg target class:
  1008. * get the trgcls from the control buffer of the skb due to
  1009. * fragmentation of original iucv message. */
  1010. err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
  1011. CB_TRGCLS_LEN, CB_TRGCLS(skb));
  1012. if (err) {
  1013. if (!(flags & MSG_PEEK))
  1014. skb_queue_head(&sk->sk_receive_queue, skb);
  1015. return err;
  1016. }
  1017. /* Mark read part of skb as used */
  1018. if (!(flags & MSG_PEEK)) {
  1019. /* SOCK_STREAM: re-queue skb if it contains unreceived data */
  1020. if (sk->sk_type == SOCK_STREAM) {
  1021. skb_pull(skb, copied);
  1022. if (skb->len) {
  1023. skb_queue_head(&sk->sk_receive_queue, skb);
  1024. goto done;
  1025. }
  1026. }
  1027. kfree_skb(skb);
  1028. /* Queue backlog skbs */
  1029. spin_lock_bh(&iucv->message_q.lock);
  1030. rskb = skb_dequeue(&iucv->backlog_skb_q);
  1031. while (rskb) {
  1032. if (sock_queue_rcv_skb(sk, rskb)) {
  1033. skb_queue_head(&iucv->backlog_skb_q,
  1034. rskb);
  1035. break;
  1036. } else {
  1037. rskb = skb_dequeue(&iucv->backlog_skb_q);
  1038. }
  1039. }
  1040. if (skb_queue_empty(&iucv->backlog_skb_q)) {
  1041. if (!list_empty(&iucv->message_q.list))
  1042. iucv_process_message_q(sk);
  1043. }
  1044. spin_unlock_bh(&iucv->message_q.lock);
  1045. }
  1046. done:
  1047. /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
  1048. if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
  1049. copied = rlen;
  1050. return copied;
  1051. }
  1052. static inline unsigned int iucv_accept_poll(struct sock *parent)
  1053. {
  1054. struct iucv_sock *isk, *n;
  1055. struct sock *sk;
  1056. list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
  1057. sk = (struct sock *) isk;
  1058. if (sk->sk_state == IUCV_CONNECTED)
  1059. return POLLIN | POLLRDNORM;
  1060. }
  1061. return 0;
  1062. }
  1063. unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
  1064. poll_table *wait)
  1065. {
  1066. struct sock *sk = sock->sk;
  1067. unsigned int mask = 0;
  1068. sock_poll_wait(file, sk_sleep(sk), wait);
  1069. if (sk->sk_state == IUCV_LISTEN)
  1070. return iucv_accept_poll(sk);
  1071. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  1072. mask |= POLLERR;
  1073. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1074. mask |= POLLRDHUP;
  1075. if (sk->sk_shutdown == SHUTDOWN_MASK)
  1076. mask |= POLLHUP;
  1077. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  1078. (sk->sk_shutdown & RCV_SHUTDOWN))
  1079. mask |= POLLIN | POLLRDNORM;
  1080. if (sk->sk_state == IUCV_CLOSED)
  1081. mask |= POLLHUP;
  1082. if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
  1083. mask |= POLLIN;
  1084. if (sock_writeable(sk))
  1085. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1086. else
  1087. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1088. return mask;
  1089. }
  1090. static int iucv_sock_shutdown(struct socket *sock, int how)
  1091. {
  1092. struct sock *sk = sock->sk;
  1093. struct iucv_sock *iucv = iucv_sk(sk);
  1094. struct iucv_message txmsg;
  1095. int err = 0;
  1096. how++;
  1097. if ((how & ~SHUTDOWN_MASK) || !how)
  1098. return -EINVAL;
  1099. lock_sock(sk);
  1100. switch (sk->sk_state) {
  1101. case IUCV_DISCONN:
  1102. case IUCV_CLOSING:
  1103. case IUCV_SEVERED:
  1104. case IUCV_CLOSED:
  1105. err = -ENOTCONN;
  1106. goto fail;
  1107. default:
  1108. sk->sk_shutdown |= how;
  1109. break;
  1110. }
  1111. if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
  1112. txmsg.class = 0;
  1113. txmsg.tag = 0;
  1114. err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
  1115. (void *) iprm_shutdown, 8);
  1116. if (err) {
  1117. switch (err) {
  1118. case 1:
  1119. err = -ENOTCONN;
  1120. break;
  1121. case 2:
  1122. err = -ECONNRESET;
  1123. break;
  1124. default:
  1125. err = -ENOTCONN;
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
  1131. err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
  1132. if (err)
  1133. err = -ENOTCONN;
  1134. skb_queue_purge(&sk->sk_receive_queue);
  1135. }
  1136. /* Wake up anyone sleeping in poll */
  1137. sk->sk_state_change(sk);
  1138. fail:
  1139. release_sock(sk);
  1140. return err;
  1141. }
  1142. static int iucv_sock_release(struct socket *sock)
  1143. {
  1144. struct sock *sk = sock->sk;
  1145. int err = 0;
  1146. if (!sk)
  1147. return 0;
  1148. iucv_sock_close(sk);
  1149. /* Unregister with IUCV base support */
  1150. if (iucv_sk(sk)->path) {
  1151. iucv_path_sever(iucv_sk(sk)->path, NULL);
  1152. iucv_path_free(iucv_sk(sk)->path);
  1153. iucv_sk(sk)->path = NULL;
  1154. }
  1155. sock_orphan(sk);
  1156. iucv_sock_kill(sk);
  1157. return err;
  1158. }
  1159. /* getsockopt and setsockopt */
  1160. static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
  1161. char __user *optval, unsigned int optlen)
  1162. {
  1163. struct sock *sk = sock->sk;
  1164. struct iucv_sock *iucv = iucv_sk(sk);
  1165. int val;
  1166. int rc;
  1167. if (level != SOL_IUCV)
  1168. return -ENOPROTOOPT;
  1169. if (optlen < sizeof(int))
  1170. return -EINVAL;
  1171. if (get_user(val, (int __user *) optval))
  1172. return -EFAULT;
  1173. rc = 0;
  1174. lock_sock(sk);
  1175. switch (optname) {
  1176. case SO_IPRMDATA_MSG:
  1177. if (val)
  1178. iucv->flags |= IUCV_IPRMDATA;
  1179. else
  1180. iucv->flags &= ~IUCV_IPRMDATA;
  1181. break;
  1182. case SO_MSGLIMIT:
  1183. switch (sk->sk_state) {
  1184. case IUCV_OPEN:
  1185. case IUCV_BOUND:
  1186. if (val < 1 || val > (u16)(~0))
  1187. rc = -EINVAL;
  1188. else
  1189. iucv->msglimit = val;
  1190. break;
  1191. default:
  1192. rc = -EINVAL;
  1193. break;
  1194. }
  1195. break;
  1196. default:
  1197. rc = -ENOPROTOOPT;
  1198. break;
  1199. }
  1200. release_sock(sk);
  1201. return rc;
  1202. }
  1203. static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
  1204. char __user *optval, int __user *optlen)
  1205. {
  1206. struct sock *sk = sock->sk;
  1207. struct iucv_sock *iucv = iucv_sk(sk);
  1208. int val, len;
  1209. if (level != SOL_IUCV)
  1210. return -ENOPROTOOPT;
  1211. if (get_user(len, optlen))
  1212. return -EFAULT;
  1213. if (len < 0)
  1214. return -EINVAL;
  1215. len = min_t(unsigned int, len, sizeof(int));
  1216. switch (optname) {
  1217. case SO_IPRMDATA_MSG:
  1218. val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
  1219. break;
  1220. case SO_MSGLIMIT:
  1221. lock_sock(sk);
  1222. val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
  1223. : iucv->msglimit; /* default */
  1224. release_sock(sk);
  1225. break;
  1226. default:
  1227. return -ENOPROTOOPT;
  1228. }
  1229. if (put_user(len, optlen))
  1230. return -EFAULT;
  1231. if (copy_to_user(optval, &val, len))
  1232. return -EFAULT;
  1233. return 0;
  1234. }
  1235. /* Callback wrappers - called from iucv base support */
  1236. static int iucv_callback_connreq(struct iucv_path *path,
  1237. u8 ipvmid[8], u8 ipuser[16])
  1238. {
  1239. unsigned char user_data[16];
  1240. unsigned char nuser_data[16];
  1241. unsigned char src_name[8];
  1242. struct hlist_node *node;
  1243. struct sock *sk, *nsk;
  1244. struct iucv_sock *iucv, *niucv;
  1245. int err;
  1246. memcpy(src_name, ipuser, 8);
  1247. EBCASC(src_name, 8);
  1248. /* Find out if this path belongs to af_iucv. */
  1249. read_lock(&iucv_sk_list.lock);
  1250. iucv = NULL;
  1251. sk = NULL;
  1252. sk_for_each(sk, node, &iucv_sk_list.head)
  1253. if (sk->sk_state == IUCV_LISTEN &&
  1254. !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
  1255. /*
  1256. * Found a listening socket with
  1257. * src_name == ipuser[0-7].
  1258. */
  1259. iucv = iucv_sk(sk);
  1260. break;
  1261. }
  1262. read_unlock(&iucv_sk_list.lock);
  1263. if (!iucv)
  1264. /* No socket found, not one of our paths. */
  1265. return -EINVAL;
  1266. bh_lock_sock(sk);
  1267. /* Check if parent socket is listening */
  1268. low_nmcpy(user_data, iucv->src_name);
  1269. high_nmcpy(user_data, iucv->dst_name);
  1270. ASCEBC(user_data, sizeof(user_data));
  1271. if (sk->sk_state != IUCV_LISTEN) {
  1272. err = iucv_path_sever(path, user_data);
  1273. iucv_path_free(path);
  1274. goto fail;
  1275. }
  1276. /* Check for backlog size */
  1277. if (sk_acceptq_is_full(sk)) {
  1278. err = iucv_path_sever(path, user_data);
  1279. iucv_path_free(path);
  1280. goto fail;
  1281. }
  1282. /* Create the new socket */
  1283. nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
  1284. if (!nsk) {
  1285. err = iucv_path_sever(path, user_data);
  1286. iucv_path_free(path);
  1287. goto fail;
  1288. }
  1289. niucv = iucv_sk(nsk);
  1290. iucv_sock_init(nsk, sk);
  1291. /* Set the new iucv_sock */
  1292. memcpy(niucv->dst_name, ipuser + 8, 8);
  1293. EBCASC(niucv->dst_name, 8);
  1294. memcpy(niucv->dst_user_id, ipvmid, 8);
  1295. memcpy(niucv->src_name, iucv->src_name, 8);
  1296. memcpy(niucv->src_user_id, iucv->src_user_id, 8);
  1297. niucv->path = path;
  1298. /* Call iucv_accept */
  1299. high_nmcpy(nuser_data, ipuser + 8);
  1300. memcpy(nuser_data + 8, niucv->src_name, 8);
  1301. ASCEBC(nuser_data + 8, 8);
  1302. /* set message limit for path based on msglimit of accepting socket */
  1303. niucv->msglimit = iucv->msglimit;
  1304. path->msglim = iucv->msglimit;
  1305. err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
  1306. if (err) {
  1307. err = iucv_path_sever(path, user_data);
  1308. iucv_path_free(path);
  1309. iucv_sock_kill(nsk);
  1310. goto fail;
  1311. }
  1312. iucv_accept_enqueue(sk, nsk);
  1313. /* Wake up accept */
  1314. nsk->sk_state = IUCV_CONNECTED;
  1315. sk->sk_data_ready(sk, 1);
  1316. err = 0;
  1317. fail:
  1318. bh_unlock_sock(sk);
  1319. return 0;
  1320. }
  1321. static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
  1322. {
  1323. struct sock *sk = path->private;
  1324. sk->sk_state = IUCV_CONNECTED;
  1325. sk->sk_state_change(sk);
  1326. }
  1327. static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
  1328. {
  1329. struct sock *sk = path->private;
  1330. struct iucv_sock *iucv = iucv_sk(sk);
  1331. struct sk_buff *skb;
  1332. struct sock_msg_q *save_msg;
  1333. int len;
  1334. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1335. iucv_message_reject(path, msg);
  1336. return;
  1337. }
  1338. spin_lock(&iucv->message_q.lock);
  1339. if (!list_empty(&iucv->message_q.list) ||
  1340. !skb_queue_empty(&iucv->backlog_skb_q))
  1341. goto save_message;
  1342. len = atomic_read(&sk->sk_rmem_alloc);
  1343. len += iucv_msg_length(msg) + sizeof(struct sk_buff);
  1344. if (len > sk->sk_rcvbuf)
  1345. goto save_message;
  1346. skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
  1347. if (!skb)
  1348. goto save_message;
  1349. iucv_process_message(sk, skb, path, msg);
  1350. goto out_unlock;
  1351. save_message:
  1352. save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
  1353. if (!save_msg)
  1354. goto out_unlock;
  1355. save_msg->path = path;
  1356. save_msg->msg = *msg;
  1357. list_add_tail(&save_msg->list, &iucv->message_q.list);
  1358. out_unlock:
  1359. spin_unlock(&iucv->message_q.lock);
  1360. }
  1361. static void iucv_callback_txdone(struct iucv_path *path,
  1362. struct iucv_message *msg)
  1363. {
  1364. struct sock *sk = path->private;
  1365. struct sk_buff *this = NULL;
  1366. struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
  1367. struct sk_buff *list_skb = list->next;
  1368. unsigned long flags;
  1369. if (!skb_queue_empty(list)) {
  1370. spin_lock_irqsave(&list->lock, flags);
  1371. while (list_skb != (struct sk_buff *)list) {
  1372. if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
  1373. this = list_skb;
  1374. break;
  1375. }
  1376. list_skb = list_skb->next;
  1377. }
  1378. if (this)
  1379. __skb_unlink(this, list);
  1380. spin_unlock_irqrestore(&list->lock, flags);
  1381. if (this) {
  1382. kfree_skb(this);
  1383. /* wake up any process waiting for sending */
  1384. iucv_sock_wake_msglim(sk);
  1385. }
  1386. }
  1387. BUG_ON(!this);
  1388. if (sk->sk_state == IUCV_CLOSING) {
  1389. if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
  1390. sk->sk_state = IUCV_CLOSED;
  1391. sk->sk_state_change(sk);
  1392. }
  1393. }
  1394. }
  1395. static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
  1396. {
  1397. struct sock *sk = path->private;
  1398. if (!list_empty(&iucv_sk(sk)->accept_q))
  1399. sk->sk_state = IUCV_SEVERED;
  1400. else
  1401. sk->sk_state = IUCV_DISCONN;
  1402. sk->sk_state_change(sk);
  1403. }
  1404. /* called if the other communication side shuts down its RECV direction;
  1405. * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
  1406. */
  1407. static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
  1408. {
  1409. struct sock *sk = path->private;
  1410. bh_lock_sock(sk);
  1411. if (sk->sk_state != IUCV_CLOSED) {
  1412. sk->sk_shutdown |= SEND_SHUTDOWN;
  1413. sk->sk_state_change(sk);
  1414. }
  1415. bh_unlock_sock(sk);
  1416. }
  1417. static const struct proto_ops iucv_sock_ops = {
  1418. .family = PF_IUCV,
  1419. .owner = THIS_MODULE,
  1420. .release = iucv_sock_release,
  1421. .bind = iucv_sock_bind,
  1422. .connect = iucv_sock_connect,
  1423. .listen = iucv_sock_listen,
  1424. .accept = iucv_sock_accept,
  1425. .getname = iucv_sock_getname,
  1426. .sendmsg = iucv_sock_sendmsg,
  1427. .recvmsg = iucv_sock_recvmsg,
  1428. .poll = iucv_sock_poll,
  1429. .ioctl = sock_no_ioctl,
  1430. .mmap = sock_no_mmap,
  1431. .socketpair = sock_no_socketpair,
  1432. .shutdown = iucv_sock_shutdown,
  1433. .setsockopt = iucv_sock_setsockopt,
  1434. .getsockopt = iucv_sock_getsockopt,
  1435. };
  1436. static const struct net_proto_family iucv_sock_family_ops = {
  1437. .family = AF_IUCV,
  1438. .owner = THIS_MODULE,
  1439. .create = iucv_sock_create,
  1440. };
  1441. static int __init afiucv_init(void)
  1442. {
  1443. int err;
  1444. if (!MACHINE_IS_VM) {
  1445. pr_err("The af_iucv module cannot be loaded"
  1446. " without z/VM\n");
  1447. err = -EPROTONOSUPPORT;
  1448. goto out;
  1449. }
  1450. cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
  1451. if (unlikely(err)) {
  1452. WARN_ON(err);
  1453. err = -EPROTONOSUPPORT;
  1454. goto out;
  1455. }
  1456. err = iucv_register(&af_iucv_handler, 0);
  1457. if (err)
  1458. goto out;
  1459. err = proto_register(&iucv_proto, 0);
  1460. if (err)
  1461. goto out_iucv;
  1462. err = sock_register(&iucv_sock_family_ops);
  1463. if (err)
  1464. goto out_proto;
  1465. /* establish dummy device */
  1466. err = driver_register(&af_iucv_driver);
  1467. if (err)
  1468. goto out_sock;
  1469. af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1470. if (!af_iucv_dev) {
  1471. err = -ENOMEM;
  1472. goto out_driver;
  1473. }
  1474. dev_set_name(af_iucv_dev, "af_iucv");
  1475. af_iucv_dev->bus = &iucv_bus;
  1476. af_iucv_dev->parent = iucv_root;
  1477. af_iucv_dev->release = (void (*)(struct device *))kfree;
  1478. af_iucv_dev->driver = &af_iucv_driver;
  1479. err = device_register(af_iucv_dev);
  1480. if (err)
  1481. goto out_driver;
  1482. return 0;
  1483. out_driver:
  1484. driver_unregister(&af_iucv_driver);
  1485. out_sock:
  1486. sock_unregister(PF_IUCV);
  1487. out_proto:
  1488. proto_unregister(&iucv_proto);
  1489. out_iucv:
  1490. iucv_unregister(&af_iucv_handler, 0);
  1491. out:
  1492. return err;
  1493. }
  1494. static void __exit afiucv_exit(void)
  1495. {
  1496. device_unregister(af_iucv_dev);
  1497. driver_unregister(&af_iucv_driver);
  1498. sock_unregister(PF_IUCV);
  1499. proto_unregister(&iucv_proto);
  1500. iucv_unregister(&af_iucv_handler, 0);
  1501. }
  1502. module_init(afiucv_init);
  1503. module_exit(afiucv_exit);
  1504. MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
  1505. MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
  1506. MODULE_VERSION(VERSION);
  1507. MODULE_LICENSE("GPL");
  1508. MODULE_ALIAS_NETPROTO(PF_IUCV);