af_irda.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:66k
源码类别:

Linux/Unix编程

开发平台:

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