af_irda.c 67 KB

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