af_irda.c 66 KB

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