af_irda.c 67 KB

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