af_irda.c 67 KB

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