af_irda.c 66 KB

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