af_irda.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:66k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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