af_irda.c 67 KB

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