af_irda.c 67 KB

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