af_irda.c 68 KB

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