af_irda.c 67 KB

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