af_irda.c 67 KB

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