af_irda.c 67 KB

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