af_irda.c 66 KB

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