af_irda.c 68 KB

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