af_irda.c 68 KB

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