af_irda.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748
  1. /*********************************************************************
  2. *
  3. * Filename: af_irda.c
  4. * Version: 0.9
  5. * Description: IrDA sockets implementation
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun May 31 10:12:43 1998
  9. * Modified at: Sat Dec 25 21:10:23 1999
  10. * Modified by: Dag Brattli <dag@brattli.net>
  11. * Sources: af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
  12. *
  13. * Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
  14. * Copyright (c) 1999-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. * All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. *
  32. * Linux-IrDA now supports four different types of IrDA sockets:
  33. *
  34. * o SOCK_STREAM: TinyTP connections with SAR disabled. The
  35. * max SDU size is 0 for conn. of this type
  36. * o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may
  37. * fragment the messages, but will preserve
  38. * the message boundaries
  39. * o SOCK_DGRAM: IRDAPROTO_UNITDATA: TinyTP connections with Unitdata
  40. * (unreliable) transfers
  41. * IRDAPROTO_ULTRA: Connectionless and unreliable data
  42. *
  43. ********************************************************************/
  44. #include <linux/capability.h>
  45. #include <linux/module.h>
  46. #include <linux/types.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/socket.h>
  49. #include <linux/sockios.h>
  50. #include <linux/slab.h>
  51. #include <linux/init.h>
  52. #include <linux/net.h>
  53. #include <linux/irda.h>
  54. #include <linux/poll.h>
  55. #include <asm/ioctls.h> /* TIOCOUTQ, TIOCINQ */
  56. #include <asm/uaccess.h>
  57. #include <net/sock.h>
  58. #include <net/tcp_states.h>
  59. #include <net/irda/af_irda.h>
  60. static int irda_create(struct net *net, struct socket *sock, int protocol, int kern);
  61. static const struct proto_ops irda_stream_ops;
  62. static const struct proto_ops irda_seqpacket_ops;
  63. static const struct proto_ops irda_dgram_ops;
  64. #ifdef CONFIG_IRDA_ULTRA
  65. static const struct proto_ops irda_ultra_ops;
  66. #define ULTRA_MAX_DATA 382
  67. #endif /* CONFIG_IRDA_ULTRA */
  68. #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
  69. /*
  70. * Function irda_data_indication (instance, sap, skb)
  71. *
  72. * Received some data from TinyTP. Just queue it on the receive queue
  73. *
  74. */
  75. static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
  76. {
  77. struct irda_sock *self;
  78. struct sock *sk;
  79. int err;
  80. IRDA_DEBUG(3, "%s()\n", __func__);
  81. self = instance;
  82. sk = instance;
  83. err = sock_queue_rcv_skb(sk, skb);
  84. if (err) {
  85. IRDA_DEBUG(1, "%s(), error: no more mem!\n", __func__);
  86. self->rx_flow = FLOW_STOP;
  87. /* When we return error, TTP will need to requeue the skb */
  88. return err;
  89. }
  90. return 0;
  91. }
  92. /*
  93. * Function irda_disconnect_indication (instance, sap, reason, skb)
  94. *
  95. * Connection has been closed. Check reason to find out why
  96. *
  97. */
  98. static void irda_disconnect_indication(void *instance, void *sap,
  99. LM_REASON reason, struct sk_buff *skb)
  100. {
  101. struct irda_sock *self;
  102. struct sock *sk;
  103. self = instance;
  104. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  105. /* Don't care about it, but let's not leak it */
  106. if(skb)
  107. dev_kfree_skb(skb);
  108. sk = instance;
  109. if (sk == NULL) {
  110. IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n",
  111. __func__, self);
  112. return;
  113. }
  114. /* Prevent race conditions with irda_release() and irda_shutdown() */
  115. bh_lock_sock(sk);
  116. if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) {
  117. sk->sk_state = TCP_CLOSE;
  118. sk->sk_shutdown |= SEND_SHUTDOWN;
  119. sk->sk_state_change(sk);
  120. /* Close our TSAP.
  121. * If we leave it open, IrLMP put it back into the list of
  122. * unconnected LSAPs. The problem is that any incoming request
  123. * can then be matched to this socket (and it will be, because
  124. * it is at the head of the list). This would prevent any
  125. * listening socket waiting on the same TSAP to get those
  126. * requests. Some apps forget to close sockets, or hang to it
  127. * a bit too long, so we may stay in this dead state long
  128. * enough to be noticed...
  129. * Note : all socket function do check sk->sk_state, so we are
  130. * safe...
  131. * Jean II
  132. */
  133. if (self->tsap) {
  134. irttp_close_tsap(self->tsap);
  135. self->tsap = NULL;
  136. }
  137. }
  138. bh_unlock_sock(sk);
  139. /* Note : once we are there, there is not much you want to do
  140. * with the socket anymore, apart from closing it.
  141. * For example, bind() and connect() won't reset sk->sk_err,
  142. * sk->sk_shutdown and sk->sk_flags to valid values...
  143. * Jean II
  144. */
  145. }
  146. /*
  147. * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
  148. *
  149. * Connections has been confirmed by the remote device
  150. *
  151. */
  152. static void irda_connect_confirm(void *instance, void *sap,
  153. struct qos_info *qos,
  154. __u32 max_sdu_size, __u8 max_header_size,
  155. struct sk_buff *skb)
  156. {
  157. struct irda_sock *self;
  158. struct sock *sk;
  159. self = instance;
  160. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  161. sk = instance;
  162. if (sk == NULL) {
  163. dev_kfree_skb(skb);
  164. return;
  165. }
  166. dev_kfree_skb(skb);
  167. // Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb);
  168. /* How much header space do we need to reserve */
  169. self->max_header_size = max_header_size;
  170. /* IrTTP max SDU size in transmit direction */
  171. self->max_sdu_size_tx = max_sdu_size;
  172. /* Find out what the largest chunk of data that we can transmit is */
  173. switch (sk->sk_type) {
  174. case SOCK_STREAM:
  175. if (max_sdu_size != 0) {
  176. IRDA_ERROR("%s: max_sdu_size must be 0\n",
  177. __func__);
  178. return;
  179. }
  180. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  181. break;
  182. case SOCK_SEQPACKET:
  183. if (max_sdu_size == 0) {
  184. IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
  185. __func__);
  186. return;
  187. }
  188. self->max_data_size = max_sdu_size;
  189. break;
  190. default:
  191. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  192. }
  193. IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
  194. self->max_data_size);
  195. memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
  196. /* We are now connected! */
  197. sk->sk_state = TCP_ESTABLISHED;
  198. sk->sk_state_change(sk);
  199. }
  200. /*
  201. * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
  202. *
  203. * Incoming connection
  204. *
  205. */
  206. static void irda_connect_indication(void *instance, void *sap,
  207. struct qos_info *qos, __u32 max_sdu_size,
  208. __u8 max_header_size, struct sk_buff *skb)
  209. {
  210. struct irda_sock *self;
  211. struct sock *sk;
  212. self = instance;
  213. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  214. sk = instance;
  215. if (sk == NULL) {
  216. dev_kfree_skb(skb);
  217. return;
  218. }
  219. /* How much header space do we need to reserve */
  220. self->max_header_size = max_header_size;
  221. /* IrTTP max SDU size in transmit direction */
  222. self->max_sdu_size_tx = max_sdu_size;
  223. /* Find out what the largest chunk of data that we can transmit is */
  224. switch (sk->sk_type) {
  225. case SOCK_STREAM:
  226. if (max_sdu_size != 0) {
  227. IRDA_ERROR("%s: max_sdu_size must be 0\n",
  228. __func__);
  229. kfree_skb(skb);
  230. return;
  231. }
  232. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  233. break;
  234. case SOCK_SEQPACKET:
  235. if (max_sdu_size == 0) {
  236. IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
  237. __func__);
  238. kfree_skb(skb);
  239. return;
  240. }
  241. self->max_data_size = max_sdu_size;
  242. break;
  243. default:
  244. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  245. }
  246. IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
  247. self->max_data_size);
  248. memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
  249. skb_queue_tail(&sk->sk_receive_queue, skb);
  250. sk->sk_state_change(sk);
  251. }
  252. /*
  253. * Function irda_connect_response (handle)
  254. *
  255. * Accept incoming connection
  256. *
  257. */
  258. static void irda_connect_response(struct irda_sock *self)
  259. {
  260. struct sk_buff *skb;
  261. IRDA_DEBUG(2, "%s()\n", __func__);
  262. skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
  263. GFP_ATOMIC);
  264. if (skb == NULL) {
  265. IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n",
  266. __func__);
  267. return;
  268. }
  269. /* Reserve space for MUX_CONTROL and LAP header */
  270. skb_reserve(skb, IRDA_MAX_HEADER);
  271. irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
  272. }
  273. /*
  274. * Function irda_flow_indication (instance, sap, flow)
  275. *
  276. * Used by TinyTP to tell us if it can accept more data or not
  277. *
  278. */
  279. static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
  280. {
  281. struct irda_sock *self;
  282. struct sock *sk;
  283. IRDA_DEBUG(2, "%s()\n", __func__);
  284. self = instance;
  285. sk = instance;
  286. BUG_ON(sk == NULL);
  287. switch (flow) {
  288. case FLOW_STOP:
  289. IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n",
  290. __func__);
  291. self->tx_flow = flow;
  292. break;
  293. case FLOW_START:
  294. self->tx_flow = flow;
  295. IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n",
  296. __func__);
  297. wake_up_interruptible(sk_sleep(sk));
  298. break;
  299. default:
  300. IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __func__);
  301. /* Unknown flow command, better stop */
  302. self->tx_flow = flow;
  303. break;
  304. }
  305. }
  306. /*
  307. * Function irda_getvalue_confirm (obj_id, value, priv)
  308. *
  309. * Got answer from remote LM-IAS, just pass object to requester...
  310. *
  311. * Note : duplicate from above, but we need our own version that
  312. * doesn't touch the dtsap_sel and save the full value structure...
  313. */
  314. static void irda_getvalue_confirm(int result, __u16 obj_id,
  315. struct ias_value *value, void *priv)
  316. {
  317. struct irda_sock *self;
  318. self = (struct irda_sock *) priv;
  319. if (!self) {
  320. IRDA_WARNING("%s: lost myself!\n", __func__);
  321. return;
  322. }
  323. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  324. /* We probably don't need to make any more queries */
  325. iriap_close(self->iriap);
  326. self->iriap = NULL;
  327. /* Check if request succeeded */
  328. if (result != IAS_SUCCESS) {
  329. IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __func__,
  330. result);
  331. self->errno = result; /* We really need it later */
  332. /* Wake up any processes waiting for result */
  333. wake_up_interruptible(&self->query_wait);
  334. return;
  335. }
  336. /* Pass the object to the caller (so the caller must delete it) */
  337. self->ias_result = value;
  338. self->errno = 0;
  339. /* Wake up any processes waiting for result */
  340. wake_up_interruptible(&self->query_wait);
  341. }
  342. /*
  343. * Function irda_selective_discovery_indication (discovery)
  344. *
  345. * Got a selective discovery indication from IrLMP.
  346. *
  347. * IrLMP is telling us that this node is new and matching our hint bit
  348. * filter. Wake up any process waiting for answer...
  349. */
  350. static void irda_selective_discovery_indication(discinfo_t *discovery,
  351. DISCOVERY_MODE mode,
  352. void *priv)
  353. {
  354. struct irda_sock *self;
  355. IRDA_DEBUG(2, "%s()\n", __func__);
  356. self = (struct irda_sock *) priv;
  357. if (!self) {
  358. IRDA_WARNING("%s: lost myself!\n", __func__);
  359. return;
  360. }
  361. /* Pass parameter to the caller */
  362. self->cachedaddr = discovery->daddr;
  363. /* Wake up process if its waiting for device to be discovered */
  364. wake_up_interruptible(&self->query_wait);
  365. }
  366. /*
  367. * Function irda_discovery_timeout (priv)
  368. *
  369. * Timeout in the selective discovery process
  370. *
  371. * We were waiting for a node to be discovered, but nothing has come up
  372. * so far. Wake up the user and tell him that we failed...
  373. */
  374. static void irda_discovery_timeout(u_long priv)
  375. {
  376. struct irda_sock *self;
  377. IRDA_DEBUG(2, "%s()\n", __func__);
  378. self = (struct irda_sock *) priv;
  379. BUG_ON(self == NULL);
  380. /* Nothing for the caller */
  381. self->cachelog = NULL;
  382. self->cachedaddr = 0;
  383. self->errno = -ETIME;
  384. /* Wake up process if its still waiting... */
  385. wake_up_interruptible(&self->query_wait);
  386. }
  387. /*
  388. * Function irda_open_tsap (self)
  389. *
  390. * Open local Transport Service Access Point (TSAP)
  391. *
  392. */
  393. static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
  394. {
  395. notify_t notify;
  396. if (self->tsap) {
  397. IRDA_WARNING("%s: busy!\n", __func__);
  398. return -EBUSY;
  399. }
  400. /* Initialize callbacks to be used by the IrDA stack */
  401. irda_notify_init(&notify);
  402. notify.connect_confirm = irda_connect_confirm;
  403. notify.connect_indication = irda_connect_indication;
  404. notify.disconnect_indication = irda_disconnect_indication;
  405. notify.data_indication = irda_data_indication;
  406. notify.udata_indication = irda_data_indication;
  407. notify.flow_indication = irda_flow_indication;
  408. notify.instance = self;
  409. strncpy(notify.name, name, NOTIFY_MAX_NAME);
  410. self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
  411. &notify);
  412. if (self->tsap == NULL) {
  413. IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n",
  414. __func__);
  415. return -ENOMEM;
  416. }
  417. /* Remember which TSAP selector we actually got */
  418. self->stsap_sel = self->tsap->stsap_sel;
  419. return 0;
  420. }
  421. /*
  422. * Function irda_open_lsap (self)
  423. *
  424. * Open local Link Service Access Point (LSAP). Used for opening Ultra
  425. * sockets
  426. */
  427. #ifdef CONFIG_IRDA_ULTRA
  428. static int irda_open_lsap(struct irda_sock *self, int pid)
  429. {
  430. notify_t notify;
  431. if (self->lsap) {
  432. IRDA_WARNING("%s(), busy!\n", __func__);
  433. return -EBUSY;
  434. }
  435. /* Initialize callbacks to be used by the IrDA stack */
  436. irda_notify_init(&notify);
  437. notify.udata_indication = irda_data_indication;
  438. notify.instance = self;
  439. strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
  440. self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);
  441. if (self->lsap == NULL) {
  442. IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __func__);
  443. return -ENOMEM;
  444. }
  445. return 0;
  446. }
  447. #endif /* CONFIG_IRDA_ULTRA */
  448. /*
  449. * Function irda_find_lsap_sel (self, name)
  450. *
  451. * Try to lookup LSAP selector in remote LM-IAS
  452. *
  453. * Basically, we start a IAP query, and then go to sleep. When the query
  454. * return, irda_getvalue_confirm will wake us up, and we can examine the
  455. * result of the query...
  456. * Note that in some case, the query fail even before we go to sleep,
  457. * creating some races...
  458. */
  459. static int irda_find_lsap_sel(struct irda_sock *self, char *name)
  460. {
  461. IRDA_DEBUG(2, "%s(%p, %s)\n", __func__, self, name);
  462. if (self->iriap) {
  463. IRDA_WARNING("%s(): busy with a previous query\n",
  464. __func__);
  465. return -EBUSY;
  466. }
  467. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  468. irda_getvalue_confirm);
  469. if(self->iriap == NULL)
  470. return -ENOMEM;
  471. /* Treat unexpected wakeup as disconnect */
  472. self->errno = -EHOSTUNREACH;
  473. /* Query remote LM-IAS */
  474. iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
  475. name, "IrDA:TinyTP:LsapSel");
  476. /* Wait for answer, if not yet finished (or failed) */
  477. if (wait_event_interruptible(self->query_wait, (self->iriap==NULL)))
  478. /* Treat signals as disconnect */
  479. return -EHOSTUNREACH;
  480. /* Check what happened */
  481. if (self->errno)
  482. {
  483. /* Requested object/attribute doesn't exist */
  484. if((self->errno == IAS_CLASS_UNKNOWN) ||
  485. (self->errno == IAS_ATTRIB_UNKNOWN))
  486. return -EADDRNOTAVAIL;
  487. else
  488. return -EHOSTUNREACH;
  489. }
  490. /* Get the remote TSAP selector */
  491. switch (self->ias_result->type) {
  492. case IAS_INTEGER:
  493. IRDA_DEBUG(4, "%s() int=%d\n",
  494. __func__, self->ias_result->t.integer);
  495. if (self->ias_result->t.integer != -1)
  496. self->dtsap_sel = self->ias_result->t.integer;
  497. else
  498. self->dtsap_sel = 0;
  499. break;
  500. default:
  501. self->dtsap_sel = 0;
  502. IRDA_DEBUG(0, "%s(), bad type!\n", __func__);
  503. break;
  504. }
  505. if (self->ias_result)
  506. irias_delete_value(self->ias_result);
  507. if (self->dtsap_sel)
  508. return 0;
  509. return -EADDRNOTAVAIL;
  510. }
  511. /*
  512. * Function irda_discover_daddr_and_lsap_sel (self, name)
  513. *
  514. * This try to find a device with the requested service.
  515. *
  516. * It basically look into the discovery log. For each address in the list,
  517. * it queries the LM-IAS of the device to find if this device offer
  518. * the requested service.
  519. * If there is more than one node supporting the service, we complain
  520. * to the user (it should move devices around).
  521. * The, we set both the destination address and the lsap selector to point
  522. * on the service on the unique device we have found.
  523. *
  524. * Note : this function fails if there is more than one device in range,
  525. * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
  526. * Moreover, we would need to wait the LAP disconnection...
  527. */
  528. static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
  529. {
  530. discinfo_t *discoveries; /* Copy of the discovery log */
  531. int number; /* Number of nodes in the log */
  532. int i;
  533. int err = -ENETUNREACH;
  534. __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
  535. __u8 dtsap_sel = 0x0; /* TSAP associated with it */
  536. IRDA_DEBUG(2, "%s(), name=%s\n", __func__, name);
  537. /* Ask lmp for the current discovery log
  538. * Note : we have to use irlmp_get_discoveries(), as opposed
  539. * to play with the cachelog directly, because while we are
  540. * making our ias query, le log might change... */
  541. discoveries = irlmp_get_discoveries(&number, self->mask.word,
  542. self->nslots);
  543. /* Check if the we got some results */
  544. if (discoveries == NULL)
  545. return -ENETUNREACH; /* No nodes discovered */
  546. /*
  547. * Now, check all discovered devices (if any), and connect
  548. * client only about the services that the client is
  549. * interested in...
  550. */
  551. for(i = 0; i < number; i++) {
  552. /* Try the address in the log */
  553. self->daddr = discoveries[i].daddr;
  554. self->saddr = 0x0;
  555. IRDA_DEBUG(1, "%s(), trying daddr = %08x\n",
  556. __func__, self->daddr);
  557. /* Query remote LM-IAS for this service */
  558. err = irda_find_lsap_sel(self, name);
  559. switch (err) {
  560. case 0:
  561. /* We found the requested service */
  562. if(daddr != DEV_ADDR_ANY) {
  563. IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
  564. __func__, name);
  565. self->daddr = DEV_ADDR_ANY;
  566. kfree(discoveries);
  567. return -ENOTUNIQ;
  568. }
  569. /* First time we found that one, save it ! */
  570. daddr = self->daddr;
  571. dtsap_sel = self->dtsap_sel;
  572. break;
  573. case -EADDRNOTAVAIL:
  574. /* Requested service simply doesn't exist on this node */
  575. break;
  576. default:
  577. /* Something bad did happen :-( */
  578. IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __func__);
  579. self->daddr = DEV_ADDR_ANY;
  580. kfree(discoveries);
  581. return -EHOSTUNREACH;
  582. break;
  583. }
  584. }
  585. /* Cleanup our copy of the discovery log */
  586. kfree(discoveries);
  587. /* Check out what we found */
  588. if(daddr == DEV_ADDR_ANY) {
  589. IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
  590. __func__, name);
  591. self->daddr = DEV_ADDR_ANY;
  592. return -EADDRNOTAVAIL;
  593. }
  594. /* Revert back to discovered device & service */
  595. self->daddr = daddr;
  596. self->saddr = 0x0;
  597. self->dtsap_sel = dtsap_sel;
  598. IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
  599. __func__, name, self->daddr);
  600. return 0;
  601. }
  602. /*
  603. * Function irda_getname (sock, uaddr, uaddr_len, peer)
  604. *
  605. * Return the our own, or peers socket address (sockaddr_irda)
  606. *
  607. */
  608. static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
  609. int *uaddr_len, int peer)
  610. {
  611. struct sockaddr_irda saddr;
  612. struct sock *sk = sock->sk;
  613. struct irda_sock *self = irda_sk(sk);
  614. memset(&saddr, 0, sizeof(saddr));
  615. if (peer) {
  616. if (sk->sk_state != TCP_ESTABLISHED)
  617. return -ENOTCONN;
  618. saddr.sir_family = AF_IRDA;
  619. saddr.sir_lsap_sel = self->dtsap_sel;
  620. saddr.sir_addr = self->daddr;
  621. } else {
  622. saddr.sir_family = AF_IRDA;
  623. saddr.sir_lsap_sel = self->stsap_sel;
  624. saddr.sir_addr = self->saddr;
  625. }
  626. IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __func__, saddr.sir_lsap_sel);
  627. IRDA_DEBUG(1, "%s(), addr = %08x\n", __func__, saddr.sir_addr);
  628. /* uaddr_len come to us uninitialised */
  629. *uaddr_len = sizeof (struct sockaddr_irda);
  630. memcpy(uaddr, &saddr, *uaddr_len);
  631. return 0;
  632. }
  633. /*
  634. * Function irda_listen (sock, backlog)
  635. *
  636. * Just move to the listen state
  637. *
  638. */
  639. static int irda_listen(struct socket *sock, int backlog)
  640. {
  641. struct sock *sk = sock->sk;
  642. int err = -EOPNOTSUPP;
  643. IRDA_DEBUG(2, "%s()\n", __func__);
  644. lock_sock(sk);
  645. if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
  646. (sk->sk_type != SOCK_DGRAM))
  647. goto out;
  648. if (sk->sk_state != TCP_LISTEN) {
  649. sk->sk_max_ack_backlog = backlog;
  650. sk->sk_state = TCP_LISTEN;
  651. err = 0;
  652. }
  653. out:
  654. release_sock(sk);
  655. return err;
  656. }
  657. /*
  658. * Function irda_bind (sock, uaddr, addr_len)
  659. *
  660. * Used by servers to register their well known TSAP
  661. *
  662. */
  663. static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  664. {
  665. struct sock *sk = sock->sk;
  666. struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
  667. struct irda_sock *self = irda_sk(sk);
  668. int err;
  669. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  670. if (addr_len != sizeof(struct sockaddr_irda))
  671. return -EINVAL;
  672. lock_sock(sk);
  673. #ifdef CONFIG_IRDA_ULTRA
  674. /* Special care for Ultra sockets */
  675. if ((sk->sk_type == SOCK_DGRAM) &&
  676. (sk->sk_protocol == IRDAPROTO_ULTRA)) {
  677. self->pid = addr->sir_lsap_sel;
  678. err = -EOPNOTSUPP;
  679. if (self->pid & 0x80) {
  680. IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
  681. goto out;
  682. }
  683. err = irda_open_lsap(self, self->pid);
  684. if (err < 0)
  685. goto out;
  686. /* Pretend we are connected */
  687. sock->state = SS_CONNECTED;
  688. sk->sk_state = TCP_ESTABLISHED;
  689. err = 0;
  690. goto out;
  691. }
  692. #endif /* CONFIG_IRDA_ULTRA */
  693. self->ias_obj = irias_new_object(addr->sir_name, jiffies);
  694. err = -ENOMEM;
  695. if (self->ias_obj == NULL)
  696. goto out;
  697. err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
  698. if (err < 0) {
  699. irias_delete_object(self->ias_obj);
  700. self->ias_obj = NULL;
  701. goto out;
  702. }
  703. /* Register with LM-IAS */
  704. irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
  705. self->stsap_sel, IAS_KERNEL_ATTR);
  706. irias_insert_object(self->ias_obj);
  707. err = 0;
  708. out:
  709. release_sock(sk);
  710. return err;
  711. }
  712. /*
  713. * Function irda_accept (sock, newsock, flags)
  714. *
  715. * Wait for incoming connection
  716. *
  717. */
  718. static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
  719. {
  720. struct sock *sk = sock->sk;
  721. struct irda_sock *new, *self = irda_sk(sk);
  722. struct sock *newsk;
  723. struct sk_buff *skb;
  724. int err;
  725. IRDA_DEBUG(2, "%s()\n", __func__);
  726. err = irda_create(sock_net(sk), newsock, sk->sk_protocol, 0);
  727. if (err)
  728. return err;
  729. err = -EINVAL;
  730. lock_sock(sk);
  731. if (sock->state != SS_UNCONNECTED)
  732. goto out;
  733. if ((sk = sock->sk) == NULL)
  734. goto out;
  735. err = -EOPNOTSUPP;
  736. if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
  737. (sk->sk_type != SOCK_DGRAM))
  738. goto out;
  739. err = -EINVAL;
  740. if (sk->sk_state != TCP_LISTEN)
  741. goto out;
  742. /*
  743. * The read queue this time is holding sockets ready to use
  744. * hooked into the SABM we saved
  745. */
  746. /*
  747. * We can perform the accept only if there is incoming data
  748. * on the listening socket.
  749. * So, we will block the caller until we receive any data.
  750. * If the caller was waiting on select() or poll() before
  751. * calling us, the data is waiting for us ;-)
  752. * Jean II
  753. */
  754. while (1) {
  755. skb = skb_dequeue(&sk->sk_receive_queue);
  756. if (skb)
  757. break;
  758. /* Non blocking operation */
  759. err = -EWOULDBLOCK;
  760. if (flags & O_NONBLOCK)
  761. goto out;
  762. err = wait_event_interruptible(*(sk_sleep(sk)),
  763. skb_peek(&sk->sk_receive_queue));
  764. if (err)
  765. goto out;
  766. }
  767. newsk = newsock->sk;
  768. err = -EIO;
  769. if (newsk == NULL)
  770. goto out;
  771. newsk->sk_state = TCP_ESTABLISHED;
  772. new = irda_sk(newsk);
  773. /* Now attach up the new socket */
  774. new->tsap = irttp_dup(self->tsap, new);
  775. err = -EPERM; /* value does not seem to make sense. -arnd */
  776. if (!new->tsap) {
  777. IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
  778. kfree_skb(skb);
  779. goto out;
  780. }
  781. new->stsap_sel = new->tsap->stsap_sel;
  782. new->dtsap_sel = new->tsap->dtsap_sel;
  783. new->saddr = irttp_get_saddr(new->tsap);
  784. new->daddr = irttp_get_daddr(new->tsap);
  785. new->max_sdu_size_tx = self->max_sdu_size_tx;
  786. new->max_sdu_size_rx = self->max_sdu_size_rx;
  787. new->max_data_size = self->max_data_size;
  788. new->max_header_size = self->max_header_size;
  789. memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
  790. /* Clean up the original one to keep it in listen state */
  791. irttp_listen(self->tsap);
  792. kfree_skb(skb);
  793. sk->sk_ack_backlog--;
  794. newsock->state = SS_CONNECTED;
  795. irda_connect_response(new);
  796. err = 0;
  797. out:
  798. release_sock(sk);
  799. return err;
  800. }
  801. /*
  802. * Function irda_connect (sock, uaddr, addr_len, flags)
  803. *
  804. * Connect to a IrDA device
  805. *
  806. * The main difference with a "standard" connect is that with IrDA we need
  807. * to resolve the service name into a TSAP selector (in TCP, port number
  808. * doesn't have to be resolved).
  809. * Because of this service name resoltion, we can offer "auto-connect",
  810. * where we connect to a service without specifying a destination address.
  811. *
  812. * Note : by consulting "errno", the user space caller may learn the cause
  813. * of the failure. Most of them are visible in the function, others may come
  814. * from subroutines called and are listed here :
  815. * o EBUSY : already processing a connect
  816. * o EHOSTUNREACH : bad addr->sir_addr argument
  817. * o EADDRNOTAVAIL : bad addr->sir_name argument
  818. * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
  819. * o ENETUNREACH : no node found on the network (auto-connect)
  820. */
  821. static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
  822. int addr_len, int flags)
  823. {
  824. struct sock *sk = sock->sk;
  825. struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
  826. struct irda_sock *self = irda_sk(sk);
  827. int err;
  828. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  829. lock_sock(sk);
  830. /* Don't allow connect for Ultra sockets */
  831. err = -ESOCKTNOSUPPORT;
  832. if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA))
  833. goto out;
  834. if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
  835. sock->state = SS_CONNECTED;
  836. err = 0;
  837. goto out; /* Connect completed during a ERESTARTSYS event */
  838. }
  839. if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
  840. sock->state = SS_UNCONNECTED;
  841. err = -ECONNREFUSED;
  842. goto out;
  843. }
  844. err = -EISCONN; /* No reconnect on a seqpacket socket */
  845. if (sk->sk_state == TCP_ESTABLISHED)
  846. goto out;
  847. sk->sk_state = TCP_CLOSE;
  848. sock->state = SS_UNCONNECTED;
  849. err = -EINVAL;
  850. if (addr_len != sizeof(struct sockaddr_irda))
  851. goto out;
  852. /* Check if user supplied any destination device address */
  853. if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
  854. /* Try to find one suitable */
  855. err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
  856. if (err) {
  857. IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __func__);
  858. goto out;
  859. }
  860. } else {
  861. /* Use the one provided by the user */
  862. self->daddr = addr->sir_addr;
  863. IRDA_DEBUG(1, "%s(), daddr = %08x\n", __func__, self->daddr);
  864. /* If we don't have a valid service name, we assume the
  865. * user want to connect on a specific LSAP. Prevent
  866. * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */
  867. if((addr->sir_name[0] != '\0') ||
  868. (addr->sir_lsap_sel >= 0x70)) {
  869. /* Query remote LM-IAS using service name */
  870. err = irda_find_lsap_sel(self, addr->sir_name);
  871. if (err) {
  872. IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
  873. goto out;
  874. }
  875. } else {
  876. /* Directly connect to the remote LSAP
  877. * specified by the sir_lsap field.
  878. * Please use with caution, in IrDA LSAPs are
  879. * dynamic and there is no "well-known" LSAP. */
  880. self->dtsap_sel = addr->sir_lsap_sel;
  881. }
  882. }
  883. /* Check if we have opened a local TSAP */
  884. if (!self->tsap)
  885. irda_open_tsap(self, LSAP_ANY, addr->sir_name);
  886. /* Move to connecting socket, start sending Connect Requests */
  887. sock->state = SS_CONNECTING;
  888. sk->sk_state = TCP_SYN_SENT;
  889. /* Connect to remote device */
  890. err = irttp_connect_request(self->tsap, self->dtsap_sel,
  891. self->saddr, self->daddr, NULL,
  892. self->max_sdu_size_rx, NULL);
  893. if (err) {
  894. IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
  895. goto out;
  896. }
  897. /* Now the loop */
  898. err = -EINPROGRESS;
  899. if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
  900. goto out;
  901. err = -ERESTARTSYS;
  902. if (wait_event_interruptible(*(sk_sleep(sk)),
  903. (sk->sk_state != TCP_SYN_SENT)))
  904. goto out;
  905. if (sk->sk_state != TCP_ESTABLISHED) {
  906. sock->state = SS_UNCONNECTED;
  907. if (sk->sk_prot->disconnect(sk, flags))
  908. sock->state = SS_DISCONNECTING;
  909. err = sock_error(sk);
  910. if (!err)
  911. err = -ECONNRESET;
  912. goto out;
  913. }
  914. sock->state = SS_CONNECTED;
  915. /* At this point, IrLMP has assigned our source address */
  916. self->saddr = irttp_get_saddr(self->tsap);
  917. err = 0;
  918. out:
  919. release_sock(sk);
  920. return err;
  921. }
  922. static struct proto irda_proto = {
  923. .name = "IRDA",
  924. .owner = THIS_MODULE,
  925. .obj_size = sizeof(struct irda_sock),
  926. };
  927. /*
  928. * Function irda_create (sock, protocol)
  929. *
  930. * Create IrDA socket
  931. *
  932. */
  933. static int irda_create(struct net *net, struct socket *sock, int protocol,
  934. int kern)
  935. {
  936. struct sock *sk;
  937. struct irda_sock *self;
  938. IRDA_DEBUG(2, "%s()\n", __func__);
  939. if (net != &init_net)
  940. return -EAFNOSUPPORT;
  941. /* Check for valid socket type */
  942. switch (sock->type) {
  943. case SOCK_STREAM: /* For TTP connections with SAR disabled */
  944. case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
  945. case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
  946. break;
  947. default:
  948. return -ESOCKTNOSUPPORT;
  949. }
  950. /* Allocate networking socket */
  951. sk = sk_alloc(net, PF_IRDA, GFP_ATOMIC, &irda_proto);
  952. if (sk == NULL)
  953. return -ENOMEM;
  954. self = irda_sk(sk);
  955. IRDA_DEBUG(2, "%s() : self is %p\n", __func__, self);
  956. init_waitqueue_head(&self->query_wait);
  957. switch (sock->type) {
  958. case SOCK_STREAM:
  959. sock->ops = &irda_stream_ops;
  960. self->max_sdu_size_rx = TTP_SAR_DISABLE;
  961. break;
  962. case SOCK_SEQPACKET:
  963. sock->ops = &irda_seqpacket_ops;
  964. self->max_sdu_size_rx = TTP_SAR_UNBOUND;
  965. break;
  966. case SOCK_DGRAM:
  967. switch (protocol) {
  968. #ifdef CONFIG_IRDA_ULTRA
  969. case IRDAPROTO_ULTRA:
  970. sock->ops = &irda_ultra_ops;
  971. /* Initialise now, because we may send on unbound
  972. * sockets. Jean II */
  973. self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
  974. self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
  975. break;
  976. #endif /* CONFIG_IRDA_ULTRA */
  977. case IRDAPROTO_UNITDATA:
  978. sock->ops = &irda_dgram_ops;
  979. /* We let Unitdata conn. be like seqpack conn. */
  980. self->max_sdu_size_rx = TTP_SAR_UNBOUND;
  981. break;
  982. default:
  983. sk_free(sk);
  984. return -ESOCKTNOSUPPORT;
  985. }
  986. break;
  987. default:
  988. sk_free(sk);
  989. return -ESOCKTNOSUPPORT;
  990. }
  991. /* Initialise networking socket struct */
  992. sock_init_data(sock, sk); /* Note : set sk->sk_refcnt to 1 */
  993. sk->sk_family = PF_IRDA;
  994. sk->sk_protocol = protocol;
  995. /* Register as a client with IrLMP */
  996. self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
  997. self->mask.word = 0xffff;
  998. self->rx_flow = self->tx_flow = FLOW_START;
  999. self->nslots = DISCOVERY_DEFAULT_SLOTS;
  1000. self->daddr = DEV_ADDR_ANY; /* Until we get connected */
  1001. self->saddr = 0x0; /* so IrLMP assign us any link */
  1002. return 0;
  1003. }
  1004. /*
  1005. * Function irda_destroy_socket (self)
  1006. *
  1007. * Destroy socket
  1008. *
  1009. */
  1010. static void irda_destroy_socket(struct irda_sock *self)
  1011. {
  1012. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  1013. /* Unregister with IrLMP */
  1014. irlmp_unregister_client(self->ckey);
  1015. irlmp_unregister_service(self->skey);
  1016. /* Unregister with LM-IAS */
  1017. if (self->ias_obj) {
  1018. irias_delete_object(self->ias_obj);
  1019. self->ias_obj = NULL;
  1020. }
  1021. if (self->iriap) {
  1022. iriap_close(self->iriap);
  1023. self->iriap = NULL;
  1024. }
  1025. if (self->tsap) {
  1026. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  1027. irttp_close_tsap(self->tsap);
  1028. self->tsap = NULL;
  1029. }
  1030. #ifdef CONFIG_IRDA_ULTRA
  1031. if (self->lsap) {
  1032. irlmp_close_lsap(self->lsap);
  1033. self->lsap = NULL;
  1034. }
  1035. #endif /* CONFIG_IRDA_ULTRA */
  1036. }
  1037. /*
  1038. * Function irda_release (sock)
  1039. */
  1040. static int irda_release(struct socket *sock)
  1041. {
  1042. struct sock *sk = sock->sk;
  1043. IRDA_DEBUG(2, "%s()\n", __func__);
  1044. if (sk == NULL)
  1045. return 0;
  1046. lock_sock(sk);
  1047. sk->sk_state = TCP_CLOSE;
  1048. sk->sk_shutdown |= SEND_SHUTDOWN;
  1049. sk->sk_state_change(sk);
  1050. /* Destroy IrDA socket */
  1051. irda_destroy_socket(irda_sk(sk));
  1052. sock_orphan(sk);
  1053. sock->sk = NULL;
  1054. release_sock(sk);
  1055. /* Purge queues (see sock_init_data()) */
  1056. skb_queue_purge(&sk->sk_receive_queue);
  1057. /* Destroy networking socket if we are the last reference on it,
  1058. * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */
  1059. sock_put(sk);
  1060. /* Notes on socket locking and deallocation... - Jean II
  1061. * In theory we should put pairs of sock_hold() / sock_put() to
  1062. * prevent the socket to be destroyed whenever there is an
  1063. * outstanding request or outstanding incoming packet or event.
  1064. *
  1065. * 1) This may include IAS request, both in connect and getsockopt.
  1066. * Unfortunately, the situation is a bit more messy than it looks,
  1067. * because we close iriap and kfree(self) above.
  1068. *
  1069. * 2) This may include selective discovery in getsockopt.
  1070. * Same stuff as above, irlmp registration and self are gone.
  1071. *
  1072. * Probably 1 and 2 may not matter, because it's all triggered
  1073. * by a process and the socket layer already prevent the
  1074. * socket to go away while a process is holding it, through
  1075. * sockfd_put() and fput()...
  1076. *
  1077. * 3) This may include deferred TSAP closure. In particular,
  1078. * we may receive a late irda_disconnect_indication()
  1079. * Fortunately, (tsap_cb *)->close_pend should protect us
  1080. * from that.
  1081. *
  1082. * I did some testing on SMP, and it looks solid. And the socket
  1083. * memory leak is now gone... - Jean II
  1084. */
  1085. return 0;
  1086. }
  1087. /*
  1088. * Function irda_sendmsg (iocb, sock, msg, len)
  1089. *
  1090. * Send message down to TinyTP. This function is used for both STREAM and
  1091. * SEQPACK services. This is possible since it forces the client to
  1092. * fragment the message if necessary
  1093. */
  1094. static int irda_sendmsg(struct kiocb *iocb, struct socket *sock,
  1095. struct msghdr *msg, size_t len)
  1096. {
  1097. struct sock *sk = sock->sk;
  1098. struct irda_sock *self;
  1099. struct sk_buff *skb;
  1100. int err = -EPIPE;
  1101. IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
  1102. /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
  1103. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR | MSG_CMSG_COMPAT |
  1104. MSG_NOSIGNAL)) {
  1105. err = -EINVAL;
  1106. goto out;
  1107. }
  1108. lock_sock(sk);
  1109. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1110. goto out_err;
  1111. if (sk->sk_state != TCP_ESTABLISHED) {
  1112. err = -ENOTCONN;
  1113. goto out;
  1114. }
  1115. self = irda_sk(sk);
  1116. /* Check if IrTTP is wants us to slow down */
  1117. if (wait_event_interruptible(*(sk_sleep(sk)),
  1118. (self->tx_flow != FLOW_STOP || sk->sk_state != TCP_ESTABLISHED))) {
  1119. err = -ERESTARTSYS;
  1120. goto out;
  1121. }
  1122. /* Check if we are still connected */
  1123. if (sk->sk_state != TCP_ESTABLISHED) {
  1124. err = -ENOTCONN;
  1125. goto out;
  1126. }
  1127. /* Check that we don't send out too big frames */
  1128. if (len > self->max_data_size) {
  1129. IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n",
  1130. __func__, len, self->max_data_size);
  1131. len = self->max_data_size;
  1132. }
  1133. skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16,
  1134. msg->msg_flags & MSG_DONTWAIT, &err);
  1135. if (!skb)
  1136. goto out_err;
  1137. skb_reserve(skb, self->max_header_size + 16);
  1138. skb_reset_transport_header(skb);
  1139. skb_put(skb, len);
  1140. err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
  1141. if (err) {
  1142. kfree_skb(skb);
  1143. goto out_err;
  1144. }
  1145. /*
  1146. * Just send the message to TinyTP, and let it deal with possible
  1147. * errors. No need to duplicate all that here
  1148. */
  1149. err = irttp_data_request(self->tsap, skb);
  1150. if (err) {
  1151. IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
  1152. goto out_err;
  1153. }
  1154. release_sock(sk);
  1155. /* Tell client how much data we actually sent */
  1156. return len;
  1157. out_err:
  1158. err = sk_stream_error(sk, msg->msg_flags, err);
  1159. out:
  1160. release_sock(sk);
  1161. return err;
  1162. }
  1163. /*
  1164. * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags)
  1165. *
  1166. * Try to receive message and copy it to user. The frame is discarded
  1167. * after being read, regardless of how much the user actually read
  1168. */
  1169. static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
  1170. struct msghdr *msg, size_t size, int flags)
  1171. {
  1172. struct sock *sk = sock->sk;
  1173. struct irda_sock *self = irda_sk(sk);
  1174. struct sk_buff *skb;
  1175. size_t copied;
  1176. int err;
  1177. IRDA_DEBUG(4, "%s()\n", __func__);
  1178. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  1179. flags & MSG_DONTWAIT, &err);
  1180. if (!skb)
  1181. return err;
  1182. skb_reset_transport_header(skb);
  1183. copied = skb->len;
  1184. if (copied > size) {
  1185. IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n",
  1186. __func__, copied, size);
  1187. copied = size;
  1188. msg->msg_flags |= MSG_TRUNC;
  1189. }
  1190. skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1191. skb_free_datagram(sk, skb);
  1192. /*
  1193. * Check if we have previously stopped IrTTP and we know
  1194. * have more free space in our rx_queue. If so tell IrTTP
  1195. * to start delivering frames again before our rx_queue gets
  1196. * empty
  1197. */
  1198. if (self->rx_flow == FLOW_STOP) {
  1199. if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
  1200. IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
  1201. self->rx_flow = FLOW_START;
  1202. irttp_flow_request(self->tsap, FLOW_START);
  1203. }
  1204. }
  1205. return copied;
  1206. }
  1207. /*
  1208. * Function irda_recvmsg_stream (iocb, sock, msg, size, flags)
  1209. */
  1210. static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
  1211. struct msghdr *msg, size_t size, int flags)
  1212. {
  1213. struct sock *sk = sock->sk;
  1214. struct irda_sock *self = irda_sk(sk);
  1215. int noblock = flags & MSG_DONTWAIT;
  1216. size_t copied = 0;
  1217. int target, err;
  1218. long timeo;
  1219. IRDA_DEBUG(3, "%s()\n", __func__);
  1220. if ((err = sock_error(sk)) < 0)
  1221. return err;
  1222. if (sock->flags & __SO_ACCEPTCON)
  1223. return -EINVAL;
  1224. err =-EOPNOTSUPP;
  1225. if (flags & MSG_OOB)
  1226. return -EOPNOTSUPP;
  1227. err = 0;
  1228. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  1229. timeo = sock_rcvtimeo(sk, noblock);
  1230. msg->msg_namelen = 0;
  1231. do {
  1232. int chunk;
  1233. struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
  1234. if (skb == NULL) {
  1235. DEFINE_WAIT(wait);
  1236. err = 0;
  1237. if (copied >= target)
  1238. break;
  1239. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1240. /*
  1241. * POSIX 1003.1g mandates this order.
  1242. */
  1243. err = sock_error(sk);
  1244. if (err)
  1245. ;
  1246. else if (sk->sk_shutdown & RCV_SHUTDOWN)
  1247. ;
  1248. else if (noblock)
  1249. err = -EAGAIN;
  1250. else if (signal_pending(current))
  1251. err = sock_intr_errno(timeo);
  1252. else if (sk->sk_state != TCP_ESTABLISHED)
  1253. err = -ENOTCONN;
  1254. else if (skb_peek(&sk->sk_receive_queue) == NULL)
  1255. /* Wait process until data arrives */
  1256. schedule();
  1257. finish_wait(sk_sleep(sk), &wait);
  1258. if (err)
  1259. return err;
  1260. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1261. break;
  1262. continue;
  1263. }
  1264. chunk = min_t(unsigned int, skb->len, size);
  1265. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  1266. skb_queue_head(&sk->sk_receive_queue, skb);
  1267. if (copied == 0)
  1268. copied = -EFAULT;
  1269. break;
  1270. }
  1271. copied += chunk;
  1272. size -= chunk;
  1273. /* Mark read part of skb as used */
  1274. if (!(flags & MSG_PEEK)) {
  1275. skb_pull(skb, chunk);
  1276. /* put the skb back if we didn't use it up.. */
  1277. if (skb->len) {
  1278. IRDA_DEBUG(1, "%s(), back on q!\n",
  1279. __func__);
  1280. skb_queue_head(&sk->sk_receive_queue, skb);
  1281. break;
  1282. }
  1283. kfree_skb(skb);
  1284. } else {
  1285. IRDA_DEBUG(0, "%s() questionable!?\n", __func__);
  1286. /* put message back and return */
  1287. skb_queue_head(&sk->sk_receive_queue, skb);
  1288. break;
  1289. }
  1290. } while (size);
  1291. /*
  1292. * Check if we have previously stopped IrTTP and we know
  1293. * have more free space in our rx_queue. If so tell IrTTP
  1294. * to start delivering frames again before our rx_queue gets
  1295. * empty
  1296. */
  1297. if (self->rx_flow == FLOW_STOP) {
  1298. if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
  1299. IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
  1300. self->rx_flow = FLOW_START;
  1301. irttp_flow_request(self->tsap, FLOW_START);
  1302. }
  1303. }
  1304. return copied;
  1305. }
  1306. /*
  1307. * Function irda_sendmsg_dgram (iocb, sock, msg, len)
  1308. *
  1309. * Send message down to TinyTP for the unreliable sequenced
  1310. * packet service...
  1311. *
  1312. */
  1313. static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock,
  1314. struct msghdr *msg, size_t len)
  1315. {
  1316. struct sock *sk = sock->sk;
  1317. struct irda_sock *self;
  1318. struct sk_buff *skb;
  1319. int err;
  1320. IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
  1321. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  1322. return -EINVAL;
  1323. lock_sock(sk);
  1324. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1325. send_sig(SIGPIPE, current, 0);
  1326. err = -EPIPE;
  1327. goto out;
  1328. }
  1329. err = -ENOTCONN;
  1330. if (sk->sk_state != TCP_ESTABLISHED)
  1331. goto out;
  1332. self = irda_sk(sk);
  1333. /*
  1334. * Check that we don't send out too big frames. This is an unreliable
  1335. * service, so we have no fragmentation and no coalescence
  1336. */
  1337. if (len > self->max_data_size) {
  1338. IRDA_DEBUG(0, "%s(), Warning to much data! "
  1339. "Chopping frame from %zd to %d bytes!\n",
  1340. __func__, len, self->max_data_size);
  1341. len = self->max_data_size;
  1342. }
  1343. skb = sock_alloc_send_skb(sk, len + self->max_header_size,
  1344. msg->msg_flags & MSG_DONTWAIT, &err);
  1345. err = -ENOBUFS;
  1346. if (!skb)
  1347. goto out;
  1348. skb_reserve(skb, self->max_header_size);
  1349. skb_reset_transport_header(skb);
  1350. IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
  1351. skb_put(skb, len);
  1352. err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
  1353. if (err) {
  1354. kfree_skb(skb);
  1355. goto out;
  1356. }
  1357. /*
  1358. * Just send the message to TinyTP, and let it deal with possible
  1359. * errors. No need to duplicate all that here
  1360. */
  1361. err = irttp_udata_request(self->tsap, skb);
  1362. if (err) {
  1363. IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
  1364. goto out;
  1365. }
  1366. release_sock(sk);
  1367. return len;
  1368. out:
  1369. release_sock(sk);
  1370. return err;
  1371. }
  1372. /*
  1373. * Function irda_sendmsg_ultra (iocb, sock, msg, len)
  1374. *
  1375. * Send message down to IrLMP for the unreliable Ultra
  1376. * packet service...
  1377. */
  1378. #ifdef CONFIG_IRDA_ULTRA
  1379. static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock,
  1380. struct msghdr *msg, size_t len)
  1381. {
  1382. struct sock *sk = sock->sk;
  1383. struct irda_sock *self;
  1384. __u8 pid = 0;
  1385. int bound = 0;
  1386. struct sk_buff *skb;
  1387. int err;
  1388. IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
  1389. err = -EINVAL;
  1390. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  1391. return -EINVAL;
  1392. lock_sock(sk);
  1393. err = -EPIPE;
  1394. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1395. send_sig(SIGPIPE, current, 0);
  1396. goto out;
  1397. }
  1398. self = irda_sk(sk);
  1399. /* Check if an address was specified with sendto. Jean II */
  1400. if (msg->msg_name) {
  1401. struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name;
  1402. err = -EINVAL;
  1403. /* Check address, extract pid. Jean II */
  1404. if (msg->msg_namelen < sizeof(*addr))
  1405. goto out;
  1406. if (addr->sir_family != AF_IRDA)
  1407. goto out;
  1408. pid = addr->sir_lsap_sel;
  1409. if (pid & 0x80) {
  1410. IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
  1411. err = -EOPNOTSUPP;
  1412. goto out;
  1413. }
  1414. } else {
  1415. /* Check that the socket is properly bound to an Ultra
  1416. * port. Jean II */
  1417. if ((self->lsap == NULL) ||
  1418. (sk->sk_state != TCP_ESTABLISHED)) {
  1419. IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n",
  1420. __func__);
  1421. err = -ENOTCONN;
  1422. goto out;
  1423. }
  1424. /* Use PID from socket */
  1425. bound = 1;
  1426. }
  1427. /*
  1428. * Check that we don't send out too big frames. This is an unreliable
  1429. * service, so we have no fragmentation and no coalescence
  1430. */
  1431. if (len > self->max_data_size) {
  1432. IRDA_DEBUG(0, "%s(), Warning to much data! "
  1433. "Chopping frame from %zd to %d bytes!\n",
  1434. __func__, len, self->max_data_size);
  1435. len = self->max_data_size;
  1436. }
  1437. skb = sock_alloc_send_skb(sk, len + self->max_header_size,
  1438. msg->msg_flags & MSG_DONTWAIT, &err);
  1439. err = -ENOBUFS;
  1440. if (!skb)
  1441. goto out;
  1442. skb_reserve(skb, self->max_header_size);
  1443. skb_reset_transport_header(skb);
  1444. IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
  1445. skb_put(skb, len);
  1446. err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
  1447. if (err) {
  1448. kfree_skb(skb);
  1449. goto out;
  1450. }
  1451. err = irlmp_connless_data_request((bound ? self->lsap : NULL),
  1452. skb, pid);
  1453. if (err)
  1454. IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
  1455. out:
  1456. release_sock(sk);
  1457. return err ? : len;
  1458. }
  1459. #endif /* CONFIG_IRDA_ULTRA */
  1460. /*
  1461. * Function irda_shutdown (sk, how)
  1462. */
  1463. static int irda_shutdown(struct socket *sock, int how)
  1464. {
  1465. struct sock *sk = sock->sk;
  1466. struct irda_sock *self = irda_sk(sk);
  1467. IRDA_DEBUG(1, "%s(%p)\n", __func__, self);
  1468. lock_sock(sk);
  1469. sk->sk_state = TCP_CLOSE;
  1470. sk->sk_shutdown |= SEND_SHUTDOWN;
  1471. sk->sk_state_change(sk);
  1472. if (self->iriap) {
  1473. iriap_close(self->iriap);
  1474. self->iriap = NULL;
  1475. }
  1476. if (self->tsap) {
  1477. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  1478. irttp_close_tsap(self->tsap);
  1479. self->tsap = NULL;
  1480. }
  1481. /* A few cleanup so the socket look as good as new... */
  1482. self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
  1483. self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
  1484. self->saddr = 0x0; /* so IrLMP assign us any link */
  1485. release_sock(sk);
  1486. return 0;
  1487. }
  1488. /*
  1489. * Function irda_poll (file, sock, wait)
  1490. */
  1491. static unsigned int irda_poll(struct file * file, struct socket *sock,
  1492. poll_table *wait)
  1493. {
  1494. struct sock *sk = sock->sk;
  1495. struct irda_sock *self = irda_sk(sk);
  1496. unsigned int mask;
  1497. IRDA_DEBUG(4, "%s()\n", __func__);
  1498. poll_wait(file, sk_sleep(sk), wait);
  1499. mask = 0;
  1500. /* Exceptional events? */
  1501. if (sk->sk_err)
  1502. mask |= POLLERR;
  1503. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1504. IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
  1505. mask |= POLLHUP;
  1506. }
  1507. /* Readable? */
  1508. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  1509. IRDA_DEBUG(4, "Socket is readable\n");
  1510. mask |= POLLIN | POLLRDNORM;
  1511. }
  1512. /* Connection-based need to check for termination and startup */
  1513. switch (sk->sk_type) {
  1514. case SOCK_STREAM:
  1515. if (sk->sk_state == TCP_CLOSE) {
  1516. IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
  1517. mask |= POLLHUP;
  1518. }
  1519. if (sk->sk_state == TCP_ESTABLISHED) {
  1520. if ((self->tx_flow == FLOW_START) &&
  1521. sock_writeable(sk))
  1522. {
  1523. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1524. }
  1525. }
  1526. break;
  1527. case SOCK_SEQPACKET:
  1528. if ((self->tx_flow == FLOW_START) &&
  1529. sock_writeable(sk))
  1530. {
  1531. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1532. }
  1533. break;
  1534. case SOCK_DGRAM:
  1535. if (sock_writeable(sk))
  1536. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1537. break;
  1538. default:
  1539. break;
  1540. }
  1541. return mask;
  1542. }
  1543. /*
  1544. * Function irda_ioctl (sock, cmd, arg)
  1545. */
  1546. static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1547. {
  1548. struct sock *sk = sock->sk;
  1549. int err;
  1550. IRDA_DEBUG(4, "%s(), cmd=%#x\n", __func__, cmd);
  1551. err = -EINVAL;
  1552. switch (cmd) {
  1553. case TIOCOUTQ: {
  1554. long amount;
  1555. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1556. if (amount < 0)
  1557. amount = 0;
  1558. err = put_user(amount, (unsigned int __user *)arg);
  1559. break;
  1560. }
  1561. case TIOCINQ: {
  1562. struct sk_buff *skb;
  1563. long amount = 0L;
  1564. /* These two are safe on a single CPU system as only user tasks fiddle here */
  1565. if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  1566. amount = skb->len;
  1567. err = put_user(amount, (unsigned int __user *)arg);
  1568. break;
  1569. }
  1570. case SIOCGSTAMP:
  1571. if (sk != NULL)
  1572. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  1573. break;
  1574. case SIOCGIFADDR:
  1575. case SIOCSIFADDR:
  1576. case SIOCGIFDSTADDR:
  1577. case SIOCSIFDSTADDR:
  1578. case SIOCGIFBRDADDR:
  1579. case SIOCSIFBRDADDR:
  1580. case SIOCGIFNETMASK:
  1581. case SIOCSIFNETMASK:
  1582. case SIOCGIFMETRIC:
  1583. case SIOCSIFMETRIC:
  1584. break;
  1585. default:
  1586. IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __func__);
  1587. err = -ENOIOCTLCMD;
  1588. }
  1589. return err;
  1590. }
  1591. #ifdef CONFIG_COMPAT
  1592. /*
  1593. * Function irda_ioctl (sock, cmd, arg)
  1594. */
  1595. static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1596. {
  1597. /*
  1598. * All IRDA's ioctl are standard ones.
  1599. */
  1600. return -ENOIOCTLCMD;
  1601. }
  1602. #endif
  1603. /*
  1604. * Function irda_setsockopt (sock, level, optname, optval, optlen)
  1605. *
  1606. * Set some options for the socket
  1607. *
  1608. */
  1609. static int irda_setsockopt(struct socket *sock, int level, int optname,
  1610. char __user *optval, unsigned int optlen)
  1611. {
  1612. struct sock *sk = sock->sk;
  1613. struct irda_sock *self = irda_sk(sk);
  1614. struct irda_ias_set *ias_opt;
  1615. struct ias_object *ias_obj;
  1616. struct ias_attrib * ias_attr; /* Attribute in IAS object */
  1617. int opt, free_ias = 0, err = 0;
  1618. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  1619. if (level != SOL_IRLMP)
  1620. return -ENOPROTOOPT;
  1621. lock_sock(sk);
  1622. switch (optname) {
  1623. case IRLMP_IAS_SET:
  1624. /* The user want to add an attribute to an existing IAS object
  1625. * (in the IAS database) or to create a new object with this
  1626. * attribute.
  1627. * We first query IAS to know if the object exist, and then
  1628. * create the right attribute...
  1629. */
  1630. if (optlen != sizeof(struct irda_ias_set)) {
  1631. err = -EINVAL;
  1632. goto out;
  1633. }
  1634. ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
  1635. if (ias_opt == NULL) {
  1636. err = -ENOMEM;
  1637. goto out;
  1638. }
  1639. /* Copy query to the driver. */
  1640. if (copy_from_user(ias_opt, optval, optlen)) {
  1641. kfree(ias_opt);
  1642. err = -EFAULT;
  1643. goto out;
  1644. }
  1645. /* Find the object we target.
  1646. * If the user gives us an empty string, we use the object
  1647. * associated with this socket. This will workaround
  1648. * duplicated class name - Jean II */
  1649. if(ias_opt->irda_class_name[0] == '\0') {
  1650. if(self->ias_obj == NULL) {
  1651. kfree(ias_opt);
  1652. err = -EINVAL;
  1653. goto out;
  1654. }
  1655. ias_obj = self->ias_obj;
  1656. } else
  1657. ias_obj = irias_find_object(ias_opt->irda_class_name);
  1658. /* Only ROOT can mess with the global IAS database.
  1659. * Users can only add attributes to the object associated
  1660. * with the socket they own - Jean II */
  1661. if((!capable(CAP_NET_ADMIN)) &&
  1662. ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
  1663. kfree(ias_opt);
  1664. err = -EPERM;
  1665. goto out;
  1666. }
  1667. /* If the object doesn't exist, create it */
  1668. if(ias_obj == (struct ias_object *) NULL) {
  1669. /* Create a new object */
  1670. ias_obj = irias_new_object(ias_opt->irda_class_name,
  1671. jiffies);
  1672. if (ias_obj == NULL) {
  1673. kfree(ias_opt);
  1674. err = -ENOMEM;
  1675. goto out;
  1676. }
  1677. free_ias = 1;
  1678. }
  1679. /* Do we have the attribute already ? */
  1680. if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
  1681. kfree(ias_opt);
  1682. if (free_ias) {
  1683. kfree(ias_obj->name);
  1684. kfree(ias_obj);
  1685. }
  1686. err = -EINVAL;
  1687. goto out;
  1688. }
  1689. /* Look at the type */
  1690. switch(ias_opt->irda_attrib_type) {
  1691. case IAS_INTEGER:
  1692. /* Add an integer attribute */
  1693. irias_add_integer_attrib(
  1694. ias_obj,
  1695. ias_opt->irda_attrib_name,
  1696. ias_opt->attribute.irda_attrib_int,
  1697. IAS_USER_ATTR);
  1698. break;
  1699. case IAS_OCT_SEQ:
  1700. /* Check length */
  1701. if(ias_opt->attribute.irda_attrib_octet_seq.len >
  1702. IAS_MAX_OCTET_STRING) {
  1703. kfree(ias_opt);
  1704. if (free_ias) {
  1705. kfree(ias_obj->name);
  1706. kfree(ias_obj);
  1707. }
  1708. err = -EINVAL;
  1709. goto out;
  1710. }
  1711. /* Add an octet sequence attribute */
  1712. irias_add_octseq_attrib(
  1713. ias_obj,
  1714. ias_opt->irda_attrib_name,
  1715. ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
  1716. ias_opt->attribute.irda_attrib_octet_seq.len,
  1717. IAS_USER_ATTR);
  1718. break;
  1719. case IAS_STRING:
  1720. /* Should check charset & co */
  1721. /* Check length */
  1722. /* The length is encoded in a __u8, and
  1723. * IAS_MAX_STRING == 256, so there is no way
  1724. * userspace can pass us a string too large.
  1725. * Jean II */
  1726. /* NULL terminate the string (avoid troubles) */
  1727. ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
  1728. /* Add a string attribute */
  1729. irias_add_string_attrib(
  1730. ias_obj,
  1731. ias_opt->irda_attrib_name,
  1732. ias_opt->attribute.irda_attrib_string.string,
  1733. IAS_USER_ATTR);
  1734. break;
  1735. default :
  1736. kfree(ias_opt);
  1737. if (free_ias) {
  1738. kfree(ias_obj->name);
  1739. kfree(ias_obj);
  1740. }
  1741. err = -EINVAL;
  1742. goto out;
  1743. }
  1744. irias_insert_object(ias_obj);
  1745. kfree(ias_opt);
  1746. break;
  1747. case IRLMP_IAS_DEL:
  1748. /* The user want to delete an object from our local IAS
  1749. * database. We just need to query the IAS, check is the
  1750. * object is not owned by the kernel and delete it.
  1751. */
  1752. if (optlen != sizeof(struct irda_ias_set)) {
  1753. err = -EINVAL;
  1754. goto out;
  1755. }
  1756. ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
  1757. if (ias_opt == NULL) {
  1758. err = -ENOMEM;
  1759. goto out;
  1760. }
  1761. /* Copy query to the driver. */
  1762. if (copy_from_user(ias_opt, optval, optlen)) {
  1763. kfree(ias_opt);
  1764. err = -EFAULT;
  1765. goto out;
  1766. }
  1767. /* Find the object we target.
  1768. * If the user gives us an empty string, we use the object
  1769. * associated with this socket. This will workaround
  1770. * duplicated class name - Jean II */
  1771. if(ias_opt->irda_class_name[0] == '\0')
  1772. ias_obj = self->ias_obj;
  1773. else
  1774. ias_obj = irias_find_object(ias_opt->irda_class_name);
  1775. if(ias_obj == (struct ias_object *) NULL) {
  1776. kfree(ias_opt);
  1777. err = -EINVAL;
  1778. goto out;
  1779. }
  1780. /* Only ROOT can mess with the global IAS database.
  1781. * Users can only del attributes from the object associated
  1782. * with the socket they own - Jean II */
  1783. if((!capable(CAP_NET_ADMIN)) &&
  1784. ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
  1785. kfree(ias_opt);
  1786. err = -EPERM;
  1787. goto out;
  1788. }
  1789. /* Find the attribute (in the object) we target */
  1790. ias_attr = irias_find_attrib(ias_obj,
  1791. ias_opt->irda_attrib_name);
  1792. if(ias_attr == (struct ias_attrib *) NULL) {
  1793. kfree(ias_opt);
  1794. err = -EINVAL;
  1795. goto out;
  1796. }
  1797. /* Check is the user space own the object */
  1798. if(ias_attr->value->owner != IAS_USER_ATTR) {
  1799. IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __func__);
  1800. kfree(ias_opt);
  1801. err = -EPERM;
  1802. goto out;
  1803. }
  1804. /* Remove the attribute (and maybe the object) */
  1805. irias_delete_attrib(ias_obj, ias_attr, 1);
  1806. kfree(ias_opt);
  1807. break;
  1808. case IRLMP_MAX_SDU_SIZE:
  1809. if (optlen < sizeof(int)) {
  1810. err = -EINVAL;
  1811. goto out;
  1812. }
  1813. if (get_user(opt, (int __user *)optval)) {
  1814. err = -EFAULT;
  1815. goto out;
  1816. }
  1817. /* Only possible for a seqpacket service (TTP with SAR) */
  1818. if (sk->sk_type != SOCK_SEQPACKET) {
  1819. IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n",
  1820. __func__, opt);
  1821. self->max_sdu_size_rx = opt;
  1822. } else {
  1823. IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n",
  1824. __func__);
  1825. err = -ENOPROTOOPT;
  1826. goto out;
  1827. }
  1828. break;
  1829. case IRLMP_HINTS_SET:
  1830. if (optlen < sizeof(int)) {
  1831. err = -EINVAL;
  1832. goto out;
  1833. }
  1834. /* The input is really a (__u8 hints[2]), easier as an int */
  1835. if (get_user(opt, (int __user *)optval)) {
  1836. err = -EFAULT;
  1837. goto out;
  1838. }
  1839. /* Unregister any old registration */
  1840. if (self->skey)
  1841. irlmp_unregister_service(self->skey);
  1842. self->skey = irlmp_register_service((__u16) opt);
  1843. break;
  1844. case IRLMP_HINT_MASK_SET:
  1845. /* As opposed to the previous case which set the hint bits
  1846. * that we advertise, this one set the filter we use when
  1847. * making a discovery (nodes which don't match any hint
  1848. * bit in the mask are not reported).
  1849. */
  1850. if (optlen < sizeof(int)) {
  1851. err = -EINVAL;
  1852. goto out;
  1853. }
  1854. /* The input is really a (__u8 hints[2]), easier as an int */
  1855. if (get_user(opt, (int __user *)optval)) {
  1856. err = -EFAULT;
  1857. goto out;
  1858. }
  1859. /* Set the new hint mask */
  1860. self->mask.word = (__u16) opt;
  1861. /* Mask out extension bits */
  1862. self->mask.word &= 0x7f7f;
  1863. /* Check if no bits */
  1864. if(!self->mask.word)
  1865. self->mask.word = 0xFFFF;
  1866. break;
  1867. default:
  1868. err = -ENOPROTOOPT;
  1869. break;
  1870. }
  1871. out:
  1872. release_sock(sk);
  1873. return err;
  1874. }
  1875. /*
  1876. * Function irda_extract_ias_value(ias_opt, ias_value)
  1877. *
  1878. * Translate internal IAS value structure to the user space representation
  1879. *
  1880. * The external representation of IAS values, as we exchange them with
  1881. * user space program is quite different from the internal representation,
  1882. * as stored in the IAS database (because we need a flat structure for
  1883. * crossing kernel boundary).
  1884. * This function transform the former in the latter. We also check
  1885. * that the value type is valid.
  1886. */
  1887. static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
  1888. struct ias_value *ias_value)
  1889. {
  1890. /* Look at the type */
  1891. switch (ias_value->type) {
  1892. case IAS_INTEGER:
  1893. /* Copy the integer */
  1894. ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
  1895. break;
  1896. case IAS_OCT_SEQ:
  1897. /* Set length */
  1898. ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
  1899. /* Copy over */
  1900. memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
  1901. ias_value->t.oct_seq, ias_value->len);
  1902. break;
  1903. case IAS_STRING:
  1904. /* Set length */
  1905. ias_opt->attribute.irda_attrib_string.len = ias_value->len;
  1906. ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
  1907. /* Copy over */
  1908. memcpy(ias_opt->attribute.irda_attrib_string.string,
  1909. ias_value->t.string, ias_value->len);
  1910. /* NULL terminate the string (avoid troubles) */
  1911. ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
  1912. break;
  1913. case IAS_MISSING:
  1914. default :
  1915. return -EINVAL;
  1916. }
  1917. /* Copy type over */
  1918. ias_opt->irda_attrib_type = ias_value->type;
  1919. return 0;
  1920. }
  1921. /*
  1922. * Function irda_getsockopt (sock, level, optname, optval, optlen)
  1923. */
  1924. static int irda_getsockopt(struct socket *sock, int level, int optname,
  1925. char __user *optval, int __user *optlen)
  1926. {
  1927. struct sock *sk = sock->sk;
  1928. struct irda_sock *self = irda_sk(sk);
  1929. struct irda_device_list list;
  1930. struct irda_device_info *discoveries;
  1931. struct irda_ias_set * ias_opt; /* IAS get/query params */
  1932. struct ias_object * ias_obj; /* Object in IAS */
  1933. struct ias_attrib * ias_attr; /* Attribute in IAS object */
  1934. int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
  1935. int val = 0;
  1936. int len = 0;
  1937. int err = 0;
  1938. int offset, total;
  1939. IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
  1940. if (level != SOL_IRLMP)
  1941. return -ENOPROTOOPT;
  1942. if (get_user(len, optlen))
  1943. return -EFAULT;
  1944. if(len < 0)
  1945. return -EINVAL;
  1946. lock_sock(sk);
  1947. switch (optname) {
  1948. case IRLMP_ENUMDEVICES:
  1949. /* Offset to first device entry */
  1950. offset = sizeof(struct irda_device_list) -
  1951. sizeof(struct irda_device_info);
  1952. if (len < offset) {
  1953. err = -EINVAL;
  1954. goto out;
  1955. }
  1956. /* Ask lmp for the current discovery log */
  1957. discoveries = irlmp_get_discoveries(&list.len, self->mask.word,
  1958. self->nslots);
  1959. /* Check if the we got some results */
  1960. if (discoveries == NULL) {
  1961. err = -EAGAIN;
  1962. goto out; /* Didn't find any devices */
  1963. }
  1964. /* Write total list length back to client */
  1965. if (copy_to_user(optval, &list, offset))
  1966. err = -EFAULT;
  1967. /* Copy the list itself - watch for overflow */
  1968. if (list.len > 2048) {
  1969. err = -EINVAL;
  1970. goto bed;
  1971. }
  1972. total = offset + (list.len * sizeof(struct irda_device_info));
  1973. if (total > len)
  1974. total = len;
  1975. if (copy_to_user(optval+offset, discoveries, total - offset))
  1976. err = -EFAULT;
  1977. /* Write total number of bytes used back to client */
  1978. if (put_user(total, optlen))
  1979. err = -EFAULT;
  1980. bed:
  1981. /* Free up our buffer */
  1982. kfree(discoveries);
  1983. break;
  1984. case IRLMP_MAX_SDU_SIZE:
  1985. val = self->max_data_size;
  1986. len = sizeof(int);
  1987. if (put_user(len, optlen)) {
  1988. err = -EFAULT;
  1989. goto out;
  1990. }
  1991. if (copy_to_user(optval, &val, len)) {
  1992. err = -EFAULT;
  1993. goto out;
  1994. }
  1995. break;
  1996. case IRLMP_IAS_GET:
  1997. /* The user want an object from our local IAS database.
  1998. * We just need to query the IAS and return the value
  1999. * that we found */
  2000. /* Check that the user has allocated the right space for us */
  2001. if (len != sizeof(struct irda_ias_set)) {
  2002. err = -EINVAL;
  2003. goto out;
  2004. }
  2005. ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
  2006. if (ias_opt == NULL) {
  2007. err = -ENOMEM;
  2008. goto out;
  2009. }
  2010. /* Copy query to the driver. */
  2011. if (copy_from_user(ias_opt, optval, len)) {
  2012. kfree(ias_opt);
  2013. err = -EFAULT;
  2014. goto out;
  2015. }
  2016. /* Find the object we target.
  2017. * If the user gives us an empty string, we use the object
  2018. * associated with this socket. This will workaround
  2019. * duplicated class name - Jean II */
  2020. if(ias_opt->irda_class_name[0] == '\0')
  2021. ias_obj = self->ias_obj;
  2022. else
  2023. ias_obj = irias_find_object(ias_opt->irda_class_name);
  2024. if(ias_obj == (struct ias_object *) NULL) {
  2025. kfree(ias_opt);
  2026. err = -EINVAL;
  2027. goto out;
  2028. }
  2029. /* Find the attribute (in the object) we target */
  2030. ias_attr = irias_find_attrib(ias_obj,
  2031. ias_opt->irda_attrib_name);
  2032. if(ias_attr == (struct ias_attrib *) NULL) {
  2033. kfree(ias_opt);
  2034. err = -EINVAL;
  2035. goto out;
  2036. }
  2037. /* Translate from internal to user structure */
  2038. err = irda_extract_ias_value(ias_opt, ias_attr->value);
  2039. if(err) {
  2040. kfree(ias_opt);
  2041. goto out;
  2042. }
  2043. /* Copy reply to the user */
  2044. if (copy_to_user(optval, ias_opt,
  2045. sizeof(struct irda_ias_set))) {
  2046. kfree(ias_opt);
  2047. err = -EFAULT;
  2048. goto out;
  2049. }
  2050. /* Note : don't need to put optlen, we checked it */
  2051. kfree(ias_opt);
  2052. break;
  2053. case IRLMP_IAS_QUERY:
  2054. /* The user want an object from a remote IAS database.
  2055. * We need to use IAP to query the remote database and
  2056. * then wait for the answer to come back. */
  2057. /* Check that the user has allocated the right space for us */
  2058. if (len != sizeof(struct irda_ias_set)) {
  2059. err = -EINVAL;
  2060. goto out;
  2061. }
  2062. ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
  2063. if (ias_opt == NULL) {
  2064. err = -ENOMEM;
  2065. goto out;
  2066. }
  2067. /* Copy query to the driver. */
  2068. if (copy_from_user(ias_opt, optval, len)) {
  2069. kfree(ias_opt);
  2070. err = -EFAULT;
  2071. goto out;
  2072. }
  2073. /* At this point, there are two cases...
  2074. * 1) the socket is connected - that's the easy case, we
  2075. * just query the device we are connected to...
  2076. * 2) the socket is not connected - the user doesn't want
  2077. * to connect and/or may not have a valid service name
  2078. * (so can't create a fake connection). In this case,
  2079. * we assume that the user pass us a valid destination
  2080. * address in the requesting structure...
  2081. */
  2082. if(self->daddr != DEV_ADDR_ANY) {
  2083. /* We are connected - reuse known daddr */
  2084. daddr = self->daddr;
  2085. } else {
  2086. /* We are not connected, we must specify a valid
  2087. * destination address */
  2088. daddr = ias_opt->daddr;
  2089. if((!daddr) || (daddr == DEV_ADDR_ANY)) {
  2090. kfree(ias_opt);
  2091. err = -EINVAL;
  2092. goto out;
  2093. }
  2094. }
  2095. /* Check that we can proceed with IAP */
  2096. if (self->iriap) {
  2097. IRDA_WARNING("%s: busy with a previous query\n",
  2098. __func__);
  2099. kfree(ias_opt);
  2100. err = -EBUSY;
  2101. goto out;
  2102. }
  2103. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  2104. irda_getvalue_confirm);
  2105. if (self->iriap == NULL) {
  2106. kfree(ias_opt);
  2107. err = -ENOMEM;
  2108. goto out;
  2109. }
  2110. /* Treat unexpected wakeup as disconnect */
  2111. self->errno = -EHOSTUNREACH;
  2112. /* Query remote LM-IAS */
  2113. iriap_getvaluebyclass_request(self->iriap,
  2114. self->saddr, daddr,
  2115. ias_opt->irda_class_name,
  2116. ias_opt->irda_attrib_name);
  2117. /* Wait for answer, if not yet finished (or failed) */
  2118. if (wait_event_interruptible(self->query_wait,
  2119. (self->iriap == NULL))) {
  2120. /* pending request uses copy of ias_opt-content
  2121. * we can free it regardless! */
  2122. kfree(ias_opt);
  2123. /* Treat signals as disconnect */
  2124. err = -EHOSTUNREACH;
  2125. goto out;
  2126. }
  2127. /* Check what happened */
  2128. if (self->errno)
  2129. {
  2130. kfree(ias_opt);
  2131. /* Requested object/attribute doesn't exist */
  2132. if((self->errno == IAS_CLASS_UNKNOWN) ||
  2133. (self->errno == IAS_ATTRIB_UNKNOWN))
  2134. err = -EADDRNOTAVAIL;
  2135. else
  2136. err = -EHOSTUNREACH;
  2137. goto out;
  2138. }
  2139. /* Translate from internal to user structure */
  2140. err = irda_extract_ias_value(ias_opt, self->ias_result);
  2141. if (self->ias_result)
  2142. irias_delete_value(self->ias_result);
  2143. if (err) {
  2144. kfree(ias_opt);
  2145. goto out;
  2146. }
  2147. /* Copy reply to the user */
  2148. if (copy_to_user(optval, ias_opt,
  2149. sizeof(struct irda_ias_set))) {
  2150. kfree(ias_opt);
  2151. err = -EFAULT;
  2152. goto out;
  2153. }
  2154. /* Note : don't need to put optlen, we checked it */
  2155. kfree(ias_opt);
  2156. break;
  2157. case IRLMP_WAITDEVICE:
  2158. /* This function is just another way of seeing life ;-)
  2159. * IRLMP_ENUMDEVICES assumes that you have a static network,
  2160. * and that you just want to pick one of the devices present.
  2161. * On the other hand, in here we assume that no device is
  2162. * present and that at some point in the future a device will
  2163. * come into range. When this device arrive, we just wake
  2164. * up the caller, so that he has time to connect to it before
  2165. * the device goes away...
  2166. * Note : once the node has been discovered for more than a
  2167. * few second, it won't trigger this function, unless it
  2168. * goes away and come back changes its hint bits (so we
  2169. * might call it IRLMP_WAITNEWDEVICE).
  2170. */
  2171. /* Check that the user is passing us an int */
  2172. if (len != sizeof(int)) {
  2173. err = -EINVAL;
  2174. goto out;
  2175. }
  2176. /* Get timeout in ms (max time we block the caller) */
  2177. if (get_user(val, (int __user *)optval)) {
  2178. err = -EFAULT;
  2179. goto out;
  2180. }
  2181. /* Tell IrLMP we want to be notified */
  2182. irlmp_update_client(self->ckey, self->mask.word,
  2183. irda_selective_discovery_indication,
  2184. NULL, (void *) self);
  2185. /* Do some discovery (and also return cached results) */
  2186. irlmp_discovery_request(self->nslots);
  2187. /* Wait until a node is discovered */
  2188. if (!self->cachedaddr) {
  2189. IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __func__);
  2190. /* Set watchdog timer to expire in <val> ms. */
  2191. self->errno = 0;
  2192. setup_timer(&self->watchdog, irda_discovery_timeout,
  2193. (unsigned long)self);
  2194. self->watchdog.expires = jiffies + (val * HZ/1000);
  2195. add_timer(&(self->watchdog));
  2196. /* Wait for IR-LMP to call us back */
  2197. __wait_event_interruptible(self->query_wait,
  2198. (self->cachedaddr != 0 || self->errno == -ETIME),
  2199. err);
  2200. /* If watchdog is still activated, kill it! */
  2201. if(timer_pending(&(self->watchdog)))
  2202. del_timer(&(self->watchdog));
  2203. IRDA_DEBUG(1, "%s(), ...waking up !\n", __func__);
  2204. if (err != 0)
  2205. goto out;
  2206. }
  2207. else
  2208. IRDA_DEBUG(1, "%s(), found immediately !\n",
  2209. __func__);
  2210. /* Tell IrLMP that we have been notified */
  2211. irlmp_update_client(self->ckey, self->mask.word,
  2212. NULL, NULL, NULL);
  2213. /* Check if the we got some results */
  2214. if (!self->cachedaddr)
  2215. return -EAGAIN; /* Didn't find any devices */
  2216. daddr = self->cachedaddr;
  2217. /* Cleanup */
  2218. self->cachedaddr = 0;
  2219. /* We return the daddr of the device that trigger the
  2220. * wakeup. As irlmp pass us only the new devices, we
  2221. * are sure that it's not an old device.
  2222. * If the user want more details, he should query
  2223. * the whole discovery log and pick one device...
  2224. */
  2225. if (put_user(daddr, (int __user *)optval)) {
  2226. err = -EFAULT;
  2227. goto out;
  2228. }
  2229. break;
  2230. default:
  2231. err = -ENOPROTOOPT;
  2232. }
  2233. out:
  2234. release_sock(sk);
  2235. return err;
  2236. }
  2237. static const struct net_proto_family irda_family_ops = {
  2238. .family = PF_IRDA,
  2239. .create = irda_create,
  2240. .owner = THIS_MODULE,
  2241. };
  2242. static const struct proto_ops irda_stream_ops = {
  2243. .family = PF_IRDA,
  2244. .owner = THIS_MODULE,
  2245. .release = irda_release,
  2246. .bind = irda_bind,
  2247. .connect = irda_connect,
  2248. .socketpair = sock_no_socketpair,
  2249. .accept = irda_accept,
  2250. .getname = irda_getname,
  2251. .poll = irda_poll,
  2252. .ioctl = irda_ioctl,
  2253. #ifdef CONFIG_COMPAT
  2254. .compat_ioctl = irda_compat_ioctl,
  2255. #endif
  2256. .listen = irda_listen,
  2257. .shutdown = irda_shutdown,
  2258. .setsockopt = irda_setsockopt,
  2259. .getsockopt = irda_getsockopt,
  2260. .sendmsg = irda_sendmsg,
  2261. .recvmsg = irda_recvmsg_stream,
  2262. .mmap = sock_no_mmap,
  2263. .sendpage = sock_no_sendpage,
  2264. };
  2265. static const struct proto_ops irda_seqpacket_ops = {
  2266. .family = PF_IRDA,
  2267. .owner = THIS_MODULE,
  2268. .release = irda_release,
  2269. .bind = irda_bind,
  2270. .connect = irda_connect,
  2271. .socketpair = sock_no_socketpair,
  2272. .accept = irda_accept,
  2273. .getname = irda_getname,
  2274. .poll = datagram_poll,
  2275. .ioctl = irda_ioctl,
  2276. #ifdef CONFIG_COMPAT
  2277. .compat_ioctl = irda_compat_ioctl,
  2278. #endif
  2279. .listen = irda_listen,
  2280. .shutdown = irda_shutdown,
  2281. .setsockopt = irda_setsockopt,
  2282. .getsockopt = irda_getsockopt,
  2283. .sendmsg = irda_sendmsg,
  2284. .recvmsg = irda_recvmsg_dgram,
  2285. .mmap = sock_no_mmap,
  2286. .sendpage = sock_no_sendpage,
  2287. };
  2288. static const struct proto_ops irda_dgram_ops = {
  2289. .family = PF_IRDA,
  2290. .owner = THIS_MODULE,
  2291. .release = irda_release,
  2292. .bind = irda_bind,
  2293. .connect = irda_connect,
  2294. .socketpair = sock_no_socketpair,
  2295. .accept = irda_accept,
  2296. .getname = irda_getname,
  2297. .poll = datagram_poll,
  2298. .ioctl = irda_ioctl,
  2299. #ifdef CONFIG_COMPAT
  2300. .compat_ioctl = irda_compat_ioctl,
  2301. #endif
  2302. .listen = irda_listen,
  2303. .shutdown = irda_shutdown,
  2304. .setsockopt = irda_setsockopt,
  2305. .getsockopt = irda_getsockopt,
  2306. .sendmsg = irda_sendmsg_dgram,
  2307. .recvmsg = irda_recvmsg_dgram,
  2308. .mmap = sock_no_mmap,
  2309. .sendpage = sock_no_sendpage,
  2310. };
  2311. #ifdef CONFIG_IRDA_ULTRA
  2312. static const struct proto_ops irda_ultra_ops = {
  2313. .family = PF_IRDA,
  2314. .owner = THIS_MODULE,
  2315. .release = irda_release,
  2316. .bind = irda_bind,
  2317. .connect = sock_no_connect,
  2318. .socketpair = sock_no_socketpair,
  2319. .accept = sock_no_accept,
  2320. .getname = irda_getname,
  2321. .poll = datagram_poll,
  2322. .ioctl = irda_ioctl,
  2323. #ifdef CONFIG_COMPAT
  2324. .compat_ioctl = irda_compat_ioctl,
  2325. #endif
  2326. .listen = sock_no_listen,
  2327. .shutdown = irda_shutdown,
  2328. .setsockopt = irda_setsockopt,
  2329. .getsockopt = irda_getsockopt,
  2330. .sendmsg = irda_sendmsg_ultra,
  2331. .recvmsg = irda_recvmsg_dgram,
  2332. .mmap = sock_no_mmap,
  2333. .sendpage = sock_no_sendpage,
  2334. };
  2335. #endif /* CONFIG_IRDA_ULTRA */
  2336. /*
  2337. * Function irsock_init (pro)
  2338. *
  2339. * Initialize IrDA protocol
  2340. *
  2341. */
  2342. int __init irsock_init(void)
  2343. {
  2344. int rc = proto_register(&irda_proto, 0);
  2345. if (rc == 0)
  2346. rc = sock_register(&irda_family_ops);
  2347. return rc;
  2348. }
  2349. /*
  2350. * Function irsock_cleanup (void)
  2351. *
  2352. * Remove IrDA protocol
  2353. *
  2354. */
  2355. void irsock_cleanup(void)
  2356. {
  2357. sock_unregister(PF_IRDA);
  2358. proto_unregister(&irda_proto);
  2359. }