af_irda.c 68 KB

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