af_irda.c 68 KB

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