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