af_irda.c 66 KB

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