1 /*********************************************************************
5 * Description: IrDA sockets implementation
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.
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.
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.
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.
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,
32 * Linux-IrDA now supports four different types of IrDA sockets:
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
43 ********************************************************************/
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/socket.h>
49 #include <linux/sockios.h>
50 #include <linux/init.h>
51 #include <linux/if_arp.h>
52 #include <linux/net.h>
53 #include <linux/irda.h>
54 #include <linux/poll.h>
56 #include <asm/uaccess.h>
60 #include <net/irda/irda.h>
61 #include <net/irda/iriap.h>
62 #include <net/irda/irias_object.h>
63 #include <net/irda/irlmp.h>
64 #include <net/irda/irttp.h>
65 #include <net/irda/discovery.h>
67 extern int irda_init(void);
68 extern void irda_cleanup(void);
69 extern int irlap_driver_rcv(struct sk_buff *, struct net_device *,
70 struct packet_type *);
72 static int irda_create(struct socket *sock, int protocol);
74 static struct proto_ops irda_stream_ops;
75 static struct proto_ops irda_seqpacket_ops;
76 static struct proto_ops irda_dgram_ops;
78 #ifdef CONFIG_IRDA_ULTRA
79 static struct proto_ops irda_ultra_ops;
80 #define ULTRA_MAX_DATA 382
81 #endif /* CONFIG_IRDA_ULTRA */
83 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
85 #ifdef CONFIG_IRDA_DEBUG
86 __u32 irda_debug = IRDA_DEBUG_LEVEL;
90 * Function irda_data_indication (instance, sap, skb)
92 * Received some data from TinyTP. Just queue it on the receive queue
95 static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
97 struct irda_sock *self;
101 IRDA_DEBUG(3, __FUNCTION__ "()\n");
103 self = (struct irda_sock *) instance;
104 ASSERT(self != NULL, return -1;);
107 ASSERT(sk != NULL, return -1;);
109 err = sock_queue_rcv_skb(sk, skb);
111 IRDA_DEBUG(1, __FUNCTION__ "(), error: no more mem!\n");
112 self->rx_flow = FLOW_STOP;
114 /* When we return error, TTP will need to requeue the skb */
122 * Function irda_disconnect_indication (instance, sap, reason, skb)
124 * Connection has been closed. Check reason to find out why
127 static void irda_disconnect_indication(void *instance, void *sap,
128 LM_REASON reason, struct sk_buff *skb)
130 struct irda_sock *self;
133 self = (struct irda_sock *) instance;
135 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
137 /* Don't care about it, but let's not leak it */
145 /* Prevent race conditions with irda_release() and irda_shutdown() */
146 if ((!sk->dead) && (sk->state != TCP_CLOSE)) {
147 sk->state = TCP_CLOSE;
148 sk->err = ECONNRESET;
149 sk->shutdown |= SEND_SHUTDOWN;
151 sk->state_change(sk);
152 sk->dead = 1; /* Uh-oh... Should use sock_orphan ? */
155 * If we leave it open, IrLMP put it back into the list of
156 * unconnected LSAPs. The problem is that any incoming request
157 * can then be matched to this socket (and it will be, because
158 * it is at the head of the list). This would prevent any
159 * listening socket waiting on the same TSAP to get those
160 * requests. Some apps forget to close sockets, or hang to it
161 * a bit too long, so we may stay in this dead state long
162 * enough to be noticed...
163 * Note : all socket function do check sk->state, so we are
168 irttp_close_tsap(self->tsap);
173 /* Note : once we are there, there is not much you want to do
174 * with the socket anymore, apart from closing it.
175 * For example, bind() and connect() won't reset sk->err,
176 * sk->shutdown and sk->dead to valid values...
182 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
184 * Connections has been confirmed by the remote device
187 static void irda_connect_confirm(void *instance, void *sap,
188 struct qos_info *qos,
189 __u32 max_sdu_size, __u8 max_header_size,
192 struct irda_sock *self;
195 self = (struct irda_sock *) instance;
197 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
203 /* How much header space do we need to reserve */
204 self->max_header_size = max_header_size;
206 /* IrTTP max SDU size in transmit direction */
207 self->max_sdu_size_tx = max_sdu_size;
209 /* Find out what the largest chunk of data that we can transmit is */
212 if (max_sdu_size != 0) {
213 ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
216 self->max_data_size = irttp_get_max_seg_size(self->tsap);
219 if (max_sdu_size == 0) {
220 ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
223 self->max_data_size = max_sdu_size;
226 self->max_data_size = irttp_get_max_seg_size(self->tsap);
229 IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n",
230 self->max_data_size);
232 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
234 // Should be ??? skb_queue_tail(&sk->receive_queue, skb);
236 /* We are now connected! */
237 sk->state = TCP_ESTABLISHED;
238 sk->state_change(sk);
242 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
244 * Incoming connection
247 static void irda_connect_indication(void *instance, void *sap,
248 struct qos_info *qos, __u32 max_sdu_size,
249 __u8 max_header_size, struct sk_buff *skb)
251 struct irda_sock *self;
254 self = (struct irda_sock *) instance;
256 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
262 /* How much header space do we need to reserve */
263 self->max_header_size = max_header_size;
265 /* IrTTP max SDU size in transmit direction */
266 self->max_sdu_size_tx = max_sdu_size;
268 /* Find out what the largest chunk of data that we can transmit is */
271 if (max_sdu_size != 0) {
272 ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
275 self->max_data_size = irttp_get_max_seg_size(self->tsap);
278 if (max_sdu_size == 0) {
279 ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
282 self->max_data_size = max_sdu_size;
285 self->max_data_size = irttp_get_max_seg_size(self->tsap);
288 IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n",
289 self->max_data_size);
291 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
293 skb_queue_tail(&sk->receive_queue, skb);
294 sk->state_change(sk);
298 * Function irda_connect_response (handle)
300 * Accept incoming connection
303 void irda_connect_response(struct irda_sock *self)
307 IRDA_DEBUG(2, __FUNCTION__ "()\n");
309 ASSERT(self != NULL, return;);
311 skb = dev_alloc_skb(64);
313 IRDA_DEBUG(0, __FUNCTION__ "() Unable to allocate sk_buff!\n");
317 /* Reserve space for MUX_CONTROL and LAP header */
318 skb_reserve(skb, IRDA_MAX_HEADER);
320 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
324 * Function irda_flow_indication (instance, sap, flow)
326 * Used by TinyTP to tell us if it can accept more data or not
329 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
331 struct irda_sock *self;
334 IRDA_DEBUG(2, __FUNCTION__ "()\n");
336 self = (struct irda_sock *) instance;
337 ASSERT(self != NULL, return;);
340 ASSERT(sk != NULL, return;);
344 IRDA_DEBUG(1, __FUNCTION__ "(), IrTTP wants us to slow down\n");
345 self->tx_flow = flow;
348 self->tx_flow = flow;
349 IRDA_DEBUG(1, __FUNCTION__
350 "(), IrTTP wants us to start again\n");
351 wake_up_interruptible(sk->sleep);
354 IRDA_DEBUG( 0, __FUNCTION__ "(), Unknown flow command!\n");
355 /* Unknown flow command, better stop */
356 self->tx_flow = flow;
362 * Function irda_getvalue_confirm (obj_id, value, priv)
364 * Got answer from remote LM-IAS, just pass object to requester...
366 * Note : duplicate from above, but we need our own version that
367 * doesn't touch the dtsap_sel and save the full value structure...
369 static void irda_getvalue_confirm(int result, __u16 obj_id,
370 struct ias_value *value, void *priv)
372 struct irda_sock *self;
374 self = (struct irda_sock *) priv;
376 WARNING(__FUNCTION__ "(), lost myself!\n");
380 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
382 /* We probably don't need to make any more queries */
383 iriap_close(self->iriap);
386 /* Check if request succeeded */
387 if (result != IAS_SUCCESS) {
388 IRDA_DEBUG(1, __FUNCTION__ "(), IAS query failed! (%d)\n",
391 self->errno = result; /* We really need it later */
393 /* Wake up any processes waiting for result */
394 wake_up_interruptible(&self->query_wait);
399 /* Pass the object to the caller (so the caller must delete it) */
400 self->ias_result = value;
403 /* Wake up any processes waiting for result */
404 wake_up_interruptible(&self->query_wait);
408 * Function irda_selective_discovery_indication (discovery)
410 * Got a selective discovery indication from IrLMP.
412 * IrLMP is telling us that this node is matching our hint bit
413 * filter. Check if it's a newly discovered node (or if node changed its
414 * hint bits), and then wake up any process waiting for answer...
416 static void irda_selective_discovery_indication(discovery_t *discovery,
420 struct irda_sock *self;
422 IRDA_DEBUG(2, __FUNCTION__ "()\n");
424 self = (struct irda_sock *) priv;
426 WARNING(__FUNCTION__ "(), lost myself!\n");
430 /* Check if node is discovered is a new one or an old one.
431 * We check when how long ago this node was discovered, with a
432 * coarse timeout (we may miss some discovery events or be delayed).
433 * Note : by doing this test here, we avoid waking up a process ;-)
435 if((jiffies - discovery->first_timestamp) >
436 (sysctl_discovery_timeout * HZ)) {
437 return; /* Too old, not interesting -> goodbye */
440 /* Pass parameter to the caller */
441 self->cachediscovery = discovery;
443 /* Wake up process if its waiting for device to be discovered */
444 wake_up_interruptible(&self->query_wait);
448 * Function irda_discovery_timeout (priv)
450 * Timeout in the selective discovery process
452 * We were waiting for a node to be discovered, but nothing has come up
453 * so far. Wake up the user and tell him that we failed...
455 static void irda_discovery_timeout(u_long priv)
457 struct irda_sock *self;
459 IRDA_DEBUG(2, __FUNCTION__ "()\n");
461 self = (struct irda_sock *) priv;
462 ASSERT(self != NULL, return;);
464 /* Nothing for the caller */
465 self->cachelog = NULL;
466 self->cachediscovery = NULL;
467 self->errno = -ETIME;
469 /* Wake up process if its still waiting... */
470 wake_up_interruptible(&self->query_wait);
474 * Function irda_open_tsap (self)
476 * Open local Transport Service Access Point (TSAP)
479 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
484 WARNING(__FUNCTION__ "(), busy!\n");
488 /* Initialize callbacks to be used by the IrDA stack */
489 irda_notify_init(¬ify);
490 notify.connect_confirm = irda_connect_confirm;
491 notify.connect_indication = irda_connect_indication;
492 notify.disconnect_indication = irda_disconnect_indication;
493 notify.data_indication = irda_data_indication;
494 notify.udata_indication = irda_data_indication;
495 notify.flow_indication = irda_flow_indication;
496 notify.instance = self;
497 strncpy(notify.name, name, NOTIFY_MAX_NAME);
499 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
501 if (self->tsap == NULL) {
502 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate TSAP!\n");
505 /* Remember which TSAP selector we actually got */
506 self->stsap_sel = self->tsap->stsap_sel;
512 * Function irda_open_lsap (self)
514 * Open local Link Service Access Point (LSAP). Used for opening Ultra
517 #ifdef CONFIG_IRDA_ULTRA
518 static int irda_open_lsap(struct irda_sock *self, int pid)
523 WARNING(__FUNCTION__ "(), busy!\n");
527 /* Initialize callbacks to be used by the IrDA stack */
528 irda_notify_init(¬ify);
529 notify.udata_indication = irda_data_indication;
530 notify.instance = self;
531 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
533 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid);
534 if (self->lsap == NULL) {
535 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate LSAP!\n");
541 #endif /* CONFIG_IRDA_ULTRA */
544 * Function irda_find_lsap_sel (self, name)
546 * Try to lookup LSAP selector in remote LM-IAS
548 * Basically, we start a IAP query, and then go to sleep. When the query
549 * return, irda_getvalue_confirm will wake us up, and we can examine the
550 * result of the query...
551 * Note that in some case, the query fail even before we go to sleep,
552 * creating some races...
554 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
556 IRDA_DEBUG(2, __FUNCTION__ "(%p, %s)\n", self, name);
558 ASSERT(self != NULL, return -1;);
561 WARNING(__FUNCTION__ "(), busy with a previous query\n");
565 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
566 irda_getvalue_confirm);
567 if(self->iriap == NULL)
570 /* Treat unexpected signals as disconnect */
571 self->errno = -EHOSTUNREACH;
573 /* Query remote LM-IAS */
574 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
575 name, "IrDA:TinyTP:LsapSel");
576 /* Wait for answer (if not already failed) */
577 if(self->iriap != NULL)
578 interruptible_sleep_on(&self->query_wait);
580 /* Check what happened */
583 /* Requested object/attribute doesn't exist */
584 if((self->errno == IAS_CLASS_UNKNOWN) ||
585 (self->errno == IAS_ATTRIB_UNKNOWN))
586 return (-EADDRNOTAVAIL);
588 return (-EHOSTUNREACH);
591 /* Get the remote TSAP selector */
592 switch (self->ias_result->type) {
594 IRDA_DEBUG(4, __FUNCTION__ "() int=%d\n",
595 self->ias_result->t.integer);
597 if (self->ias_result->t.integer != -1)
598 self->dtsap_sel = self->ias_result->t.integer;
604 IRDA_DEBUG(0, __FUNCTION__ "(), bad type!\n");
607 if (self->ias_result)
608 irias_delete_value(self->ias_result);
613 return -EADDRNOTAVAIL;
617 * Function irda_discover_daddr_and_lsap_sel (self, name)
619 * This try to find a device with the requested service.
621 * It basically look into the discovery log. For each address in the list,
622 * it queries the LM-IAS of the device to find if this device offer
623 * the requested service.
624 * If there is more than one node supporting the service, we complain
625 * to the user (it should move devices around).
626 * The, we set both the destination address and the lsap selector to point
627 * on the service on the unique device we have found.
629 * Note : this function fails if there is more than one device in range,
630 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
631 * Moreover, we would need to wait the LAP disconnection...
633 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
635 struct irda_device_info *discoveries; /* Copy of the discovery log */
636 int number; /* Number of nodes in the log */
638 int err = -ENETUNREACH;
639 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
640 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
642 IRDA_DEBUG(2, __FUNCTION__ "(), name=%s\n", name);
644 ASSERT(self != NULL, return -1;);
646 /* Ask lmp for the current discovery log
647 * Note : we have to use irlmp_get_discoveries(), as opposed
648 * to play with the cachelog directly, because while we are
649 * making our ias query, le log might change... */
650 discoveries = irlmp_get_discoveries(&number, self->mask, self->nslots);
651 /* Check if the we got some results */
652 if (discoveries == NULL)
653 return -ENETUNREACH; /* No nodes discovered */
656 * Now, check all discovered devices (if any), and connect
657 * client only about the services that the client is
660 for(i = 0; i < number; i++) {
661 /* Try the address in the log */
662 self->daddr = discoveries[i].daddr;
664 IRDA_DEBUG(1, __FUNCTION__ "(), trying daddr = %08x\n",
667 /* Query remote LM-IAS for this service */
668 err = irda_find_lsap_sel(self, name);
671 /* We found the requested service */
672 if(daddr != DEV_ADDR_ANY) {
673 IRDA_DEBUG(1, __FUNCTION__
674 "(), discovered service ''%s'' in two different devices !!!\n",
676 self->daddr = DEV_ADDR_ANY;
680 /* First time we found that one, save it ! */
682 dtsap_sel = self->dtsap_sel;
685 /* Requested service simply doesn't exist on this node */
688 /* Something bad did happen :-( */
689 IRDA_DEBUG(0, __FUNCTION__
690 "(), unexpected IAS query failure\n");
691 self->daddr = DEV_ADDR_ANY;
693 return(-EHOSTUNREACH);
697 /* Cleanup our copy of the discovery log */
700 /* Check out what we found */
701 if(daddr == DEV_ADDR_ANY) {
702 IRDA_DEBUG(1, __FUNCTION__
703 "(), cannot discover service ''%s'' in any device !!!\n",
705 self->daddr = DEV_ADDR_ANY;
706 return(-EADDRNOTAVAIL);
709 /* Revert back to discovered device & service */
712 self->dtsap_sel = dtsap_sel;
714 IRDA_DEBUG(1, __FUNCTION__
715 "(), discovered requested service ''%s'' at address %08x\n",
722 * Function irda_getname (sock, uaddr, uaddr_len, peer)
724 * Return the our own, or peers socket address (sockaddr_irda)
727 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
728 int *uaddr_len, int peer)
730 struct sockaddr_irda saddr;
731 struct sock *sk = sock->sk;
732 struct irda_sock *self = sk->protinfo.irda;
735 if (sk->state != TCP_ESTABLISHED)
738 saddr.sir_family = AF_IRDA;
739 saddr.sir_lsap_sel = self->dtsap_sel;
740 saddr.sir_addr = self->daddr;
742 saddr.sir_family = AF_IRDA;
743 saddr.sir_lsap_sel = self->stsap_sel;
744 saddr.sir_addr = self->saddr;
747 IRDA_DEBUG(1, __FUNCTION__ "(), tsap_sel = %#x\n", saddr.sir_lsap_sel);
748 IRDA_DEBUG(1, __FUNCTION__ "(), addr = %08x\n", saddr.sir_addr);
750 /* uaddr_len come to us uninitialised */
751 *uaddr_len = sizeof (struct sockaddr_irda);
752 memcpy(uaddr, &saddr, *uaddr_len);
758 * Function irda_listen (sock, backlog)
760 * Just move to the listen state
763 static int irda_listen(struct socket *sock, int backlog)
765 struct sock *sk = sock->sk;
767 IRDA_DEBUG(2, __FUNCTION__ "()\n");
769 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
770 (sk->type != SOCK_DGRAM))
773 if (sk->state != TCP_LISTEN) {
774 sk->max_ack_backlog = backlog;
775 sk->state = TCP_LISTEN;
784 * Function irda_bind (sock, uaddr, addr_len)
786 * Used by servers to register their well known TSAP
789 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
791 struct sock *sk = sock->sk;
792 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
793 struct irda_sock *self;
796 self = sk->protinfo.irda;
797 ASSERT(self != NULL, return -1;);
799 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
801 if (addr_len != sizeof(struct sockaddr_irda))
804 #ifdef CONFIG_IRDA_ULTRA
805 /* Special care for Ultra sockets */
806 if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
807 self->pid = addr->sir_lsap_sel;
808 if (self->pid & 0x80) {
809 IRDA_DEBUG(0, __FUNCTION__
810 "(), extension in PID not supp!\n");
813 err = irda_open_lsap(self, self->pid);
817 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
818 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
820 /* Pretend we are connected */
821 sock->state = SS_CONNECTED;
822 sk->state = TCP_ESTABLISHED;
826 #endif /* CONFIG_IRDA_ULTRA */
828 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
832 /* Register with LM-IAS */
833 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
834 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
835 self->stsap_sel, IAS_KERNEL_ATTR);
836 irias_insert_object(self->ias_obj);
842 * Function irda_accept (sock, newsock, flags)
844 * Wait for incoming connection
847 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
849 struct irda_sock *self, *new;
850 struct sock *sk = sock->sk;
855 IRDA_DEBUG(2, __FUNCTION__ "()\n");
857 self = sk->protinfo.irda;
858 ASSERT(self != NULL, return -1;);
860 err = irda_create(newsock, sk->protocol);
864 if (sock->state != SS_UNCONNECTED)
867 if ((sk = sock->sk) == NULL)
870 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
871 (sk->type != SOCK_DGRAM))
874 if (sk->state != TCP_LISTEN)
878 * The read queue this time is holding sockets ready to use
879 * hooked into the SABM we saved
882 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
883 if (flags & O_NONBLOCK)
886 interruptible_sleep_on(sk->sleep);
887 if (signal_pending(current))
890 } while (skb == NULL);
893 newsk->state = TCP_ESTABLISHED;
895 new = newsk->protinfo.irda;
896 ASSERT(new != NULL, return -1;);
898 /* Now attach up the new socket */
899 new->tsap = irttp_dup(self->tsap, new);
901 IRDA_DEBUG(0, __FUNCTION__ "(), dup failed!\n");
905 new->stsap_sel = new->tsap->stsap_sel;
906 new->dtsap_sel = new->tsap->dtsap_sel;
907 new->saddr = irttp_get_saddr(new->tsap);
908 new->daddr = irttp_get_daddr(new->tsap);
910 new->max_sdu_size_tx = self->max_sdu_size_tx;
911 new->max_sdu_size_rx = self->max_sdu_size_rx;
912 new->max_data_size = self->max_data_size;
913 new->max_header_size = self->max_header_size;
915 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
917 /* Clean up the original one to keep it in listen state */
918 irttp_listen(self->tsap);
921 skb->destructor = NULL;
925 newsock->state = SS_CONNECTED;
927 irda_connect_response(new);
933 * Function irda_connect (sock, uaddr, addr_len, flags)
935 * Connect to a IrDA device
937 * The main difference with a "standard" connect is that with IrDA we need
938 * to resolve the service name into a TSAP selector (in TCP, port number
939 * doesn't have to be resolved).
940 * Because of this service name resoltion, we can offer "auto-connect",
941 * where we connect to a service without specifying a destination address.
943 * Note : by consulting "errno", the user space caller may learn the cause
944 * of the failure. Most of them are visible in the function, others may come
945 * from subroutines called and are listed here :
946 * o EBUSY : already processing a connect
947 * o EHOSTUNREACH : bad addr->sir_addr argument
948 * o EADDRNOTAVAIL : bad addr->sir_name argument
949 * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
950 * o ENETUNREACH : no node found on the network (auto-connect)
952 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
953 int addr_len, int flags)
955 struct sock *sk = sock->sk;
956 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
957 struct irda_sock *self;
960 self = sk->protinfo.irda;
962 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
964 /* Don't allow connect for Ultra sockets */
965 if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA))
966 return -ESOCKTNOSUPPORT;
968 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
969 sock->state = SS_CONNECTED;
970 return 0; /* Connect completed during a ERESTARTSYS event */
973 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
974 sock->state = SS_UNCONNECTED;
975 return -ECONNREFUSED;
978 if (sk->state == TCP_ESTABLISHED)
979 return -EISCONN; /* No reconnect on a seqpacket socket */
981 sk->state = TCP_CLOSE;
982 sock->state = SS_UNCONNECTED;
984 if (addr_len != sizeof(struct sockaddr_irda))
987 /* Check if user supplied any destination device address */
988 if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
989 /* Try to find one suitable */
990 err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
992 IRDA_DEBUG(0, __FUNCTION__
993 "(), auto-connect failed!\n");
997 /* Use the one provided by the user */
998 self->daddr = addr->sir_addr;
999 IRDA_DEBUG(1, __FUNCTION__ "(), daddr = %08x\n", self->daddr);
1001 /* Query remote LM-IAS */
1002 err = irda_find_lsap_sel(self, addr->sir_name);
1004 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
1009 /* Check if we have opened a local TSAP */
1011 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1013 /* Move to connecting socket, start sending Connect Requests */
1014 sock->state = SS_CONNECTING;
1015 sk->state = TCP_SYN_SENT;
1017 /* Connect to remote device */
1018 err = irttp_connect_request(self->tsap, self->dtsap_sel,
1019 self->saddr, self->daddr, NULL,
1020 self->max_sdu_size_rx, NULL);
1022 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
1027 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1028 return -EINPROGRESS;
1030 /* Here, there is a race condition : the state may change between
1031 * our test and the sleep, via irda_connect_confirm().
1032 * The way to workaround that is to sleep with a timeout, so that
1033 * we don't sleep forever and check the state when waking up.
1034 * 50ms is plenty good enough, because the LAP is already connected.
1036 while (sk->state == TCP_SYN_SENT) {
1037 interruptible_sleep_on_timeout(sk->sleep, HZ/20);
1038 if (signal_pending(current)) {
1039 return -ERESTARTSYS;
1043 if (sk->state != TCP_ESTABLISHED) {
1044 sock->state = SS_UNCONNECTED;
1045 return sock_error(sk); /* Always set at this point */
1048 sock->state = SS_CONNECTED;
1050 /* At this point, IrLMP has assigned our source address */
1051 self->saddr = irttp_get_saddr(self->tsap);
1057 * Function irda_create (sock, protocol)
1059 * Create IrDA socket
1062 static int irda_create(struct socket *sock, int protocol)
1065 struct irda_sock *self;
1067 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1069 /* Check for valid socket type */
1070 switch (sock->type) {
1071 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1072 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1073 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1076 return -ESOCKTNOSUPPORT;
1079 /* Allocate networking socket */
1080 if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
1083 /* Allocate IrDA socket */
1084 self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
1089 memset(self, 0, sizeof(struct irda_sock));
1091 IRDA_DEBUG(2, __FUNCTION__ "() : self is %p\n", self);
1093 init_waitqueue_head(&self->query_wait);
1095 /* Initialise networking socket struct */
1096 sock_init_data(sock, sk); /* Note : set sk->refcnt to 1 */
1097 sk->family = PF_IRDA;
1098 sk->protocol = protocol;
1099 /* Link networking socket and IrDA socket structs together */
1100 sk->protinfo.irda = self;
1103 switch (sock->type) {
1105 sock->ops = &irda_stream_ops;
1106 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1108 case SOCK_SEQPACKET:
1109 sock->ops = &irda_seqpacket_ops;
1110 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1114 #ifdef CONFIG_IRDA_ULTRA
1115 case IRDAPROTO_ULTRA:
1116 sock->ops = &irda_ultra_ops;
1118 #endif /* CONFIG_IRDA_ULTRA */
1119 case IRDAPROTO_UNITDATA:
1120 sock->ops = &irda_dgram_ops;
1121 /* We let Unitdata conn. be like seqpack conn. */
1122 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1125 ERROR(__FUNCTION__ "(), protocol not supported!\n");
1126 return -ESOCKTNOSUPPORT;
1130 return -ESOCKTNOSUPPORT;
1133 /* Register as a client with IrLMP */
1134 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1135 self->mask = 0xffff;
1136 self->rx_flow = self->tx_flow = FLOW_START;
1137 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1138 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1139 self->saddr = 0x0; /* so IrLMP assign us any link */
1147 * Function irda_destroy_socket (self)
1152 void irda_destroy_socket(struct irda_sock *self)
1154 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1156 ASSERT(self != NULL, return;);
1158 /* Unregister with IrLMP */
1159 irlmp_unregister_client(self->ckey);
1160 irlmp_unregister_service(self->skey);
1162 /* Unregister with LM-IAS */
1163 if (self->ias_obj) {
1164 irias_delete_object(self->ias_obj);
1165 self->ias_obj = NULL;
1169 iriap_close(self->iriap);
1174 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1175 irttp_close_tsap(self->tsap);
1178 #ifdef CONFIG_IRDA_ULTRA
1180 irlmp_close_lsap(self->lsap);
1183 #endif /* CONFIG_IRDA_ULTRA */
1191 * Function irda_release (sock)
1196 static int irda_release(struct socket *sock)
1198 struct sock *sk = sock->sk;
1200 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1205 sk->state = TCP_CLOSE;
1206 sk->shutdown |= SEND_SHUTDOWN;
1207 sk->state_change(sk);
1209 /* Destroy IrDA socket */
1210 irda_destroy_socket(sk->protinfo.irda);
1211 /* Prevent sock_def_destruct() to create havoc */
1212 sk->protinfo.irda = NULL;
1217 /* Purge queues (see sock_init_data()) */
1218 skb_queue_purge(&sk->receive_queue);
1220 /* Destroy networking socket if we are the last reference on it,
1221 * i.e. if(sk->refcnt == 0) -> sk_free(sk) */
1224 /* Notes on socket locking and deallocation... - Jean II
1225 * In theory we should put pairs of sock_hold() / sock_put() to
1226 * prevent the socket to be destroyed whenever there is an
1227 * outstanding request or outstanding incomming packet or event.
1229 * 1) This may include IAS request, both in connect and getsockopt.
1230 * Unfortunately, the situation is a bit more messy than it looks,
1231 * because we close iriap and kfree(self) above.
1233 * 2) This may include selective discovery in getsockopt.
1234 * Same stuff as above, irlmp registration and self are gone.
1236 * Probably 1 and 2 may not matter, because it's all triggered
1237 * by a process and the socket layer already prevent the
1238 * socket to go away while a process is holding it, through
1239 * sockfd_put() and fput()...
1241 * 3) This may include deferred TSAP closure. In particular,
1242 * we may receive a late irda_disconnect_indication()
1243 * Fortunately, (tsap_cb *)->close_pend should protect us
1246 * I did some testing on SMP, and it looks solid. And the socket
1247 * memory leak is now gone... - Jean II
1254 * Function irda_sendmsg (sock, msg, len, scm)
1256 * Send message down to TinyTP. This function is used for both STREAM and
1257 * SEQPACK services. This is possible since it forces the client to
1258 * fragment the message if necessary
1260 static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1261 struct scm_cookie *scm)
1263 struct sock *sk = sock->sk;
1264 struct irda_sock *self;
1265 struct sk_buff *skb;
1266 unsigned char *asmptr;
1269 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1271 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1272 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
1275 if (sk->shutdown & SEND_SHUTDOWN) {
1276 send_sig(SIGPIPE, current, 0);
1280 if (sk->state != TCP_ESTABLISHED)
1283 self = sk->protinfo.irda;
1284 ASSERT(self != NULL, return -1;);
1286 /* Check if IrTTP is wants us to slow down */
1287 while (self->tx_flow == FLOW_STOP) {
1288 IRDA_DEBUG(2, __FUNCTION__ "(), IrTTP is busy, going to sleep!\n");
1289 interruptible_sleep_on(sk->sleep);
1291 /* Check if we are still connected */
1292 if (sk->state != TCP_ESTABLISHED)
1294 /* Handle signals */
1295 if (signal_pending(current))
1296 return -ERESTARTSYS;
1299 /* Check that we don't send out to big frames */
1300 if (len > self->max_data_size) {
1301 IRDA_DEBUG(2, __FUNCTION__
1302 "(), Chopping frame from %d to %d bytes!\n", len,
1303 self->max_data_size);
1304 len = self->max_data_size;
1307 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1308 msg->msg_flags & MSG_DONTWAIT, &err);
1312 skb_reserve(skb, self->max_header_size);
1314 asmptr = skb->h.raw = skb_put(skb, len);
1315 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1318 * Just send the message to TinyTP, and let it deal with possible
1319 * errors. No need to duplicate all that here
1321 err = irttp_data_request(self->tsap, skb);
1323 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1326 /* Tell client how much data we actually sent */
1331 * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
1333 * Try to receive message and copy it to user. The frame is discarded
1334 * after being read, regardless of how much the user actually read
1336 static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
1337 int size, int flags, struct scm_cookie *scm)
1339 struct irda_sock *self;
1340 struct sock *sk = sock->sk;
1341 struct sk_buff *skb;
1344 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1346 self = sk->protinfo.irda;
1347 ASSERT(self != NULL, return -1;);
1349 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1350 flags & MSG_DONTWAIT, &err);
1354 skb->h.raw = skb->data;
1357 if (copied > size) {
1358 IRDA_DEBUG(2, __FUNCTION__
1359 "(), Received truncated frame (%d < %d)!\n",
1362 msg->msg_flags |= MSG_TRUNC;
1364 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1366 skb_free_datagram(sk, skb);
1369 * Check if we have previously stopped IrTTP and we know
1370 * have more free space in our rx_queue. If so tell IrTTP
1371 * to start delivering frames again before our rx_queue gets
1374 if (self->rx_flow == FLOW_STOP) {
1375 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1376 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1377 self->rx_flow = FLOW_START;
1378 irttp_flow_request(self->tsap, FLOW_START);
1386 * Function irda_data_wait (sk)
1388 * Sleep until data has arrive. But check for races..
1391 static void irda_data_wait(struct sock *sk)
1393 if (!skb_peek(&sk->receive_queue)) {
1394 set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1395 interruptible_sleep_on(sk->sleep);
1396 clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1401 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
1406 static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
1407 int size, int flags, struct scm_cookie *scm)
1409 struct irda_sock *self;
1410 struct sock *sk = sock->sk;
1411 int noblock = flags & MSG_DONTWAIT;
1415 IRDA_DEBUG(3, __FUNCTION__ "()\n");
1417 self = sk->protinfo.irda;
1418 ASSERT(self != NULL, return -1;);
1420 if (sock->flags & __SO_ACCEPTCON)
1423 if (flags & MSG_OOB)
1426 if (flags & MSG_WAITALL)
1429 msg->msg_namelen = 0;
1433 struct sk_buff *skb;
1435 skb=skb_dequeue(&sk->receive_queue);
1437 if (copied >= target)
1441 * POSIX 1003.1g mandates this order.
1445 return sock_error(sk);
1448 if (sk->shutdown & RCV_SHUTDOWN)
1454 if (signal_pending(current))
1455 return -ERESTARTSYS;
1459 chunk = min_t(unsigned int, skb->len, size);
1460 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1461 skb_queue_head(&sk->receive_queue, skb);
1469 /* Mark read part of skb as used */
1470 if (!(flags & MSG_PEEK)) {
1471 skb_pull(skb, chunk);
1473 /* put the skb back if we didn't use it up.. */
1475 IRDA_DEBUG(1, __FUNCTION__ "(), back on q!\n");
1476 skb_queue_head(&sk->receive_queue, skb);
1482 IRDA_DEBUG(0, __FUNCTION__ "() questionable!?\n");
1484 /* put message back and return */
1485 skb_queue_head(&sk->receive_queue, skb);
1491 * Check if we have previously stopped IrTTP and we know
1492 * have more free space in our rx_queue. If so tell IrTTP
1493 * to start delivering frames again before our rx_queue gets
1496 if (self->rx_flow == FLOW_STOP) {
1497 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1498 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1499 self->rx_flow = FLOW_START;
1500 irttp_flow_request(self->tsap, FLOW_START);
1508 * Function irda_sendmsg_dgram (sock, msg, len, scm)
1510 * Send message down to TinyTP for the unreliable sequenced
1514 static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
1515 int len, struct scm_cookie *scm)
1517 struct sock *sk = sock->sk;
1518 struct irda_sock *self;
1519 struct sk_buff *skb;
1520 unsigned char *asmptr;
1523 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1525 if (msg->msg_flags & ~MSG_DONTWAIT)
1528 if (sk->shutdown & SEND_SHUTDOWN) {
1529 send_sig(SIGPIPE, current, 0);
1533 if (sk->state != TCP_ESTABLISHED)
1536 self = sk->protinfo.irda;
1537 ASSERT(self != NULL, return -1;);
1540 * Check that we don't send out to big frames. This is an unreliable
1541 * service, so we have no fragmentation and no coalescence
1543 if (len > self->max_data_size) {
1544 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1545 "Chopping frame from %d to %d bytes!\n", len,
1546 self->max_data_size);
1547 len = self->max_data_size;
1550 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1551 msg->msg_flags & MSG_DONTWAIT, &err);
1555 skb_reserve(skb, self->max_header_size);
1557 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1558 asmptr = skb->h.raw = skb_put(skb, len);
1559 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1562 * Just send the message to TinyTP, and let it deal with possible
1563 * errors. No need to duplicate all that here
1565 err = irttp_udata_request(self->tsap, skb);
1567 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1574 * Function irda_sendmsg_ultra (sock, msg, len, scm)
1576 * Send message down to IrLMP for the unreliable Ultra
1579 #ifdef CONFIG_IRDA_ULTRA
1580 static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
1581 int len, struct scm_cookie *scm)
1583 struct sock *sk = sock->sk;
1584 struct irda_sock *self;
1585 struct sk_buff *skb;
1586 unsigned char *asmptr;
1589 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1591 if (msg->msg_flags & ~MSG_DONTWAIT)
1594 if (sk->shutdown & SEND_SHUTDOWN) {
1595 send_sig(SIGPIPE, current, 0);
1599 self = sk->protinfo.irda;
1600 ASSERT(self != NULL, return -1;);
1603 * Check that we don't send out to big frames. This is an unreliable
1604 * service, so we have no fragmentation and no coalescence
1606 if (len > self->max_data_size) {
1607 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1608 "Chopping frame from %d to %d bytes!\n", len,
1609 self->max_data_size);
1610 len = self->max_data_size;
1613 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1614 msg->msg_flags & MSG_DONTWAIT, &err);
1618 skb_reserve(skb, self->max_header_size);
1620 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1621 asmptr = skb->h.raw = skb_put(skb, len);
1622 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1624 err = irlmp_connless_data_request(self->lsap, skb);
1626 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1631 #endif /* CONFIG_IRDA_ULTRA */
1634 * Function irda_shutdown (sk, how)
1639 static int irda_shutdown(struct socket *sock, int how)
1641 struct irda_sock *self;
1642 struct sock *sk = sock->sk;
1644 self = sk->protinfo.irda;
1645 ASSERT(self != NULL, return -1;);
1647 IRDA_DEBUG(1, __FUNCTION__ "(%p)\n", self);
1649 sk->state = TCP_CLOSE;
1650 sk->shutdown |= SEND_SHUTDOWN;
1651 sk->state_change(sk);
1654 iriap_close(self->iriap);
1659 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1660 irttp_close_tsap(self->tsap);
1664 /* A few cleanup so the socket look as good as new... */
1665 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1666 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1667 self->saddr = 0x0; /* so IrLMP assign us any link */
1673 * Function irda_poll (file, sock, wait)
1678 static unsigned int irda_poll(struct file * file, struct socket *sock,
1681 struct sock *sk = sock->sk;
1683 struct irda_sock *self;
1685 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1687 self = sk->protinfo.irda;
1688 poll_wait(file, sk->sleep, wait);
1691 /* Exceptional events? */
1694 if (sk->shutdown & RCV_SHUTDOWN) {
1695 IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1700 if (!skb_queue_empty(&sk->receive_queue)) {
1701 IRDA_DEBUG(4, "Socket is readable\n");
1702 mask |= POLLIN | POLLRDNORM;
1705 /* Connection-based need to check for termination and startup */
1708 if (sk->state == TCP_CLOSE) {
1709 IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1713 if (sk->state == TCP_ESTABLISHED) {
1714 if ((self->tx_flow == FLOW_START) &&
1717 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1721 case SOCK_SEQPACKET:
1722 if ((self->tx_flow == FLOW_START) &&
1725 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1729 if (sock_writeable(sk))
1730 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1739 * Function irda_ioctl (sock, cmd, arg)
1744 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1746 struct sock *sk = sock->sk;
1748 IRDA_DEBUG(4, __FUNCTION__ "(), cmd=%#x\n", cmd);
1753 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1756 if (put_user(amount, (unsigned int *)arg))
1762 struct sk_buff *skb;
1764 /* These two are safe on a single CPU system as only user tasks fiddle here */
1765 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1767 if (put_user(amount, (unsigned int *)arg))
1774 if (sk->stamp.tv_sec == 0)
1776 if (copy_to_user((void *)arg, &sk->stamp,
1777 sizeof(struct timeval)))
1785 case SIOCGIFDSTADDR:
1786 case SIOCSIFDSTADDR:
1787 case SIOCGIFBRDADDR:
1788 case SIOCSIFBRDADDR:
1789 case SIOCGIFNETMASK:
1790 case SIOCSIFNETMASK:
1795 IRDA_DEBUG(1, __FUNCTION__ "(), doing device ioctl!\n");
1796 return dev_ioctl(cmd, (void *) arg);
1804 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1806 * Set some options for the socket
1809 static int irda_setsockopt(struct socket *sock, int level, int optname,
1810 char *optval, int optlen)
1812 struct sock *sk = sock->sk;
1813 struct irda_sock *self;
1814 struct irda_ias_set *ias_opt;
1815 struct ias_object *ias_obj;
1816 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1819 self = sk->protinfo.irda;
1820 ASSERT(self != NULL, return -1;);
1822 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1824 if (level != SOL_IRLMP)
1825 return -ENOPROTOOPT;
1829 /* The user want to add an attribute to an existing IAS object
1830 * (in the IAS database) or to create a new object with this
1832 * We first query IAS to know if the object exist, and then
1833 * create the right attribute...
1836 if (optlen != sizeof(struct irda_ias_set))
1839 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1840 if (ias_opt == NULL)
1843 /* Copy query to the driver. */
1844 if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1849 /* Find the object we target.
1850 * If the user gives us an empty string, we use the object
1851 * associated with this socket. This will workaround
1852 * duplicated class name - Jean II */
1853 if(ias_opt->irda_class_name[0] == '\0') {
1854 if(self->ias_obj == NULL) {
1858 ias_obj = self->ias_obj;
1860 ias_obj = irias_find_object(ias_opt->irda_class_name);
1862 /* Only ROOT can mess with the global IAS database.
1863 * Users can only add attributes to the object associated
1864 * with the socket they own - Jean II */
1865 if((!capable(CAP_NET_ADMIN)) &&
1866 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
1871 /* If the object doesn't exist, create it */
1872 if(ias_obj == (struct ias_object *) NULL) {
1873 /* Create a new object */
1874 ias_obj = irias_new_object(ias_opt->irda_class_name,
1878 /* Do we have the attribute already ? */
1879 if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1884 /* Look at the type */
1885 switch(ias_opt->irda_attrib_type) {
1887 /* Add an integer attribute */
1888 irias_add_integer_attrib(
1890 ias_opt->irda_attrib_name,
1891 ias_opt->attribute.irda_attrib_int,
1896 if(ias_opt->attribute.irda_attrib_octet_seq.len >
1897 IAS_MAX_OCTET_STRING) {
1901 /* Add an octet sequence attribute */
1902 irias_add_octseq_attrib(
1904 ias_opt->irda_attrib_name,
1905 ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1906 ias_opt->attribute.irda_attrib_octet_seq.len,
1910 /* Should check charset & co */
1912 if(ias_opt->attribute.irda_attrib_string.len >
1917 /* NULL terminate the string (avoid troubles) */
1918 ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1919 /* Add a string attribute */
1920 irias_add_string_attrib(
1922 ias_opt->irda_attrib_name,
1923 ias_opt->attribute.irda_attrib_string.string,
1930 irias_insert_object(ias_obj);
1934 /* The user want to delete an object from our local IAS
1935 * database. We just need to query the IAS, check is the
1936 * object is not owned by the kernel and delete it.
1939 if (optlen != sizeof(struct irda_ias_set))
1942 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1943 if (ias_opt == NULL)
1946 /* Copy query to the driver. */
1947 if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1952 /* Find the object we target.
1953 * If the user gives us an empty string, we use the object
1954 * associated with this socket. This will workaround
1955 * duplicated class name - Jean II */
1956 if(ias_opt->irda_class_name[0] == '\0')
1957 ias_obj = self->ias_obj;
1959 ias_obj = irias_find_object(ias_opt->irda_class_name);
1960 if(ias_obj == (struct ias_object *) NULL) {
1965 /* Only ROOT can mess with the global IAS database.
1966 * Users can only del attributes from the object associated
1967 * with the socket they own - Jean II */
1968 if((!capable(CAP_NET_ADMIN)) &&
1969 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
1974 /* Find the attribute (in the object) we target */
1975 ias_attr = irias_find_attrib(ias_obj,
1976 ias_opt->irda_attrib_name);
1977 if(ias_attr == (struct ias_attrib *) NULL) {
1982 /* Check is the user space own the object */
1983 if(ias_attr->value->owner != IAS_USER_ATTR) {
1984 IRDA_DEBUG(1, __FUNCTION__
1985 "(), attempting to delete a kernel attribute\n");
1990 /* Remove the attribute (and maybe the object) */
1991 irias_delete_attrib(ias_obj, ias_attr);
1994 case IRLMP_MAX_SDU_SIZE:
1995 if (optlen < sizeof(int))
1998 if (get_user(opt, (int *)optval))
2001 /* Only possible for a seqpacket service (TTP with SAR) */
2002 if (sk->type != SOCK_SEQPACKET) {
2003 IRDA_DEBUG(2, __FUNCTION__
2004 "(), setting max_sdu_size = %d\n", opt);
2005 self->max_sdu_size_rx = opt;
2007 WARNING(__FUNCTION__
2008 "(), not allowed to set MAXSDUSIZE for this "
2010 return -ENOPROTOOPT;
2013 case IRLMP_HINTS_SET:
2014 if (optlen < sizeof(int))
2017 if (get_user(opt, (int *)optval))
2020 /* Unregister any old registration */
2022 irlmp_unregister_service(self->skey);
2024 self->skey = irlmp_register_service((__u16) opt);
2026 case IRLMP_HINT_MASK_SET:
2027 /* As opposed to the previous case which set the hint bits
2028 * that we advertise, this one set the filter we use when
2029 * making a discovery (nodes which don't match any hint
2030 * bit in the mask are not reported).
2032 if (optlen < sizeof(int))
2035 if (get_user(opt, (int *)optval))
2038 /* Set the new hint mask */
2039 self->mask = (__u16) opt;
2040 /* Mask out extension bits */
2041 self->mask &= 0x7f7f;
2042 /* Check if no bits */
2044 self->mask = 0xFFFF;
2048 return -ENOPROTOOPT;
2054 * Function irda_extract_ias_value(ias_opt, ias_value)
2056 * Translate internal IAS value structure to the user space representation
2058 * The external representation of IAS values, as we exchange them with
2059 * user space program is quite different from the internal representation,
2060 * as stored in the IAS database (because we need a flat structure for
2061 * crossing kernel boundary).
2062 * This function transform the former in the latter. We also check
2063 * that the value type is valid.
2065 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2066 struct ias_value *ias_value)
2068 /* Look at the type */
2069 switch (ias_value->type) {
2071 /* Copy the integer */
2072 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2076 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2078 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2079 ias_value->t.oct_seq, ias_value->len);
2083 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2084 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2086 memcpy(ias_opt->attribute.irda_attrib_string.string,
2087 ias_value->t.string, ias_value->len);
2088 /* NULL terminate the string (avoid troubles) */
2089 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2096 /* Copy type over */
2097 ias_opt->irda_attrib_type = ias_value->type;
2103 * Function irda_getsockopt (sock, level, optname, optval, optlen)
2108 static int irda_getsockopt(struct socket *sock, int level, int optname,
2109 char *optval, int *optlen)
2111 struct sock *sk = sock->sk;
2112 struct irda_sock *self;
2113 struct irda_device_list list;
2114 struct irda_device_info *discoveries;
2115 struct irda_ias_set * ias_opt; /* IAS get/query params */
2116 struct ias_object * ias_obj; /* Object in IAS */
2117 struct ias_attrib * ias_attr; /* Attribute in IAS object */
2118 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
2124 self = sk->protinfo.irda;
2126 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
2128 if (level != SOL_IRLMP)
2129 return -ENOPROTOOPT;
2131 if (get_user(len, optlen))
2138 case IRLMP_ENUMDEVICES:
2139 /* Ask lmp for the current discovery log */
2140 discoveries = irlmp_get_discoveries(&list.len, self->mask,
2142 /* Check if the we got some results */
2143 if (discoveries == NULL)
2144 return -EAGAIN; /* Didn't find any devices */
2147 /* Write total list length back to client */
2148 if (copy_to_user(optval, &list,
2149 sizeof(struct irda_device_list) -
2150 sizeof(struct irda_device_info)))
2153 /* Offset to first device entry */
2154 offset = sizeof(struct irda_device_list) -
2155 sizeof(struct irda_device_info);
2157 /* Copy the list itself - watch for overflow */
2163 total = offset + (list.len * sizeof(struct irda_device_info));
2166 if (copy_to_user(optval+offset, discoveries, total - offset))
2169 /* Write total number of bytes used back to client */
2170 if (put_user(total, optlen))
2173 /* Free up our buffer */
2178 case IRLMP_MAX_SDU_SIZE:
2179 val = self->max_data_size;
2181 if (put_user(len, optlen))
2184 if (copy_to_user(optval, &val, len))
2188 /* The user want an object from our local IAS database.
2189 * We just need to query the IAS and return the value
2192 /* Check that the user has allocated the right space for us */
2193 if (len != sizeof(struct irda_ias_set))
2196 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2197 if (ias_opt == NULL)
2200 /* Copy query to the driver. */
2201 if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2206 /* Find the object we target.
2207 * If the user gives us an empty string, we use the object
2208 * associated with this socket. This will workaround
2209 * duplicated class name - Jean II */
2210 if(ias_opt->irda_class_name[0] == '\0')
2211 ias_obj = self->ias_obj;
2213 ias_obj = irias_find_object(ias_opt->irda_class_name);
2214 if(ias_obj == (struct ias_object *) NULL) {
2219 /* Find the attribute (in the object) we target */
2220 ias_attr = irias_find_attrib(ias_obj,
2221 ias_opt->irda_attrib_name);
2222 if(ias_attr == (struct ias_attrib *) NULL) {
2227 /* Translate from internal to user structure */
2228 err = irda_extract_ias_value(ias_opt, ias_attr->value);
2234 /* Copy reply to the user */
2235 if (copy_to_user((char *)optval, (char *) ias_opt,
2236 sizeof(struct irda_ias_set))) {
2240 /* Note : don't need to put optlen, we checked it */
2243 case IRLMP_IAS_QUERY:
2244 /* The user want an object from a remote IAS database.
2245 * We need to use IAP to query the remote database and
2246 * then wait for the answer to come back. */
2248 /* Check that the user has allocated the right space for us */
2249 if (len != sizeof(struct irda_ias_set))
2252 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2253 if (ias_opt == NULL)
2256 /* Copy query to the driver. */
2257 if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2262 /* At this point, there are two cases...
2263 * 1) the socket is connected - that's the easy case, we
2264 * just query the device we are connected to...
2265 * 2) the socket is not connected - the user doesn't want
2266 * to connect and/or may not have a valid service name
2267 * (so can't create a fake connection). In this case,
2268 * we assume that the user pass us a valid destination
2269 * address in the requesting structure...
2271 if(self->daddr != DEV_ADDR_ANY) {
2272 /* We are connected - reuse known daddr */
2273 daddr = self->daddr;
2275 /* We are not connected, we must specify a valid
2276 * destination address */
2277 daddr = ias_opt->daddr;
2278 if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2284 /* Check that we can proceed with IAP */
2286 WARNING(__FUNCTION__
2287 "(), busy with a previous query\n");
2292 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2293 irda_getvalue_confirm);
2295 /* Treat unexpected signals as disconnect */
2296 self->errno = -EHOSTUNREACH;
2298 /* Query remote LM-IAS */
2299 iriap_getvaluebyclass_request(self->iriap,
2301 ias_opt->irda_class_name,
2302 ias_opt->irda_attrib_name);
2303 /* Wait for answer (if not already failed) */
2304 if(self->iriap != NULL)
2305 interruptible_sleep_on(&self->query_wait);
2306 /* Check what happened */
2310 /* Requested object/attribute doesn't exist */
2311 if((self->errno == IAS_CLASS_UNKNOWN) ||
2312 (self->errno == IAS_ATTRIB_UNKNOWN))
2313 return (-EADDRNOTAVAIL);
2315 return (-EHOSTUNREACH);
2318 /* Translate from internal to user structure */
2319 err = irda_extract_ias_value(ias_opt, self->ias_result);
2320 if (self->ias_result)
2321 irias_delete_value(self->ias_result);
2327 /* Copy reply to the user */
2328 if (copy_to_user((char *)optval, (char *) ias_opt,
2329 sizeof(struct irda_ias_set))) {
2333 /* Note : don't need to put optlen, we checked it */
2336 case IRLMP_WAITDEVICE:
2337 /* This function is just another way of seeing life ;-)
2338 * IRLMP_ENUMDEVICES assumes that you have a static network,
2339 * and that you just want to pick one of the devices present.
2340 * On the other hand, in here we assume that no device is
2341 * present and that at some point in the future a device will
2342 * come into range. When this device arrive, we just wake
2343 * up the caller, so that he has time to connect to it before
2344 * the device goes away...
2345 * Note : once the node has been discovered for more than a
2346 * few second, it won't trigger this function, unless it
2347 * goes away and come back changes its hint bits (so we
2348 * might call it IRLMP_WAITNEWDEVICE).
2351 /* Check that the user is passing us an int */
2352 if (len != sizeof(int))
2354 /* Get timeout in ms (max time we block the caller) */
2355 if (get_user(val, (int *)optval))
2358 /* Tell IrLMP we want to be notified */
2359 irlmp_update_client(self->ckey, self->mask,
2360 irda_selective_discovery_indication,
2361 NULL, (void *) self);
2363 /* Do some discovery (and also return cached results) */
2364 irlmp_discovery_request(self->nslots);
2366 /* Wait until a node is discovered */
2367 if (!self->cachediscovery) {
2368 IRDA_DEBUG(1, __FUNCTION__
2369 "(), nothing discovered yet, going to sleep...\n");
2371 /* Set watchdog timer to expire in <val> ms. */
2372 self->watchdog.function = irda_discovery_timeout;
2373 self->watchdog.data = (unsigned long) self;
2374 self->watchdog.expires = jiffies + (val * HZ/1000);
2375 add_timer(&(self->watchdog));
2377 /* Wait for IR-LMP to call us back */
2378 interruptible_sleep_on(&self->query_wait);
2380 /* If watchdog is still activated, kill it! */
2381 if(timer_pending(&(self->watchdog)))
2382 del_timer(&(self->watchdog));
2384 IRDA_DEBUG(1, __FUNCTION__
2385 "(), ...waking up !\n");
2388 IRDA_DEBUG(1, __FUNCTION__
2389 "(), found immediately !\n");
2391 /* Tell IrLMP that we have been notified */
2392 irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
2394 /* Check if the we got some results */
2395 if (!self->cachediscovery)
2396 return -EAGAIN; /* Didn't find any devices */
2398 self->cachediscovery = NULL;
2400 /* Note : We don't return anything to the user.
2401 * We could return the device that triggered the wake up,
2402 * but it's probably better to force the user to query
2403 * the whole discovery log and let him pick one device...
2407 return -ENOPROTOOPT;
2413 static struct net_proto_family irda_family_ops =
2419 static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2422 release: irda_release,
2424 connect: irda_connect,
2425 socketpair: sock_no_socketpair,
2426 accept: irda_accept,
2427 getname: irda_getname,
2430 listen: irda_listen,
2431 shutdown: irda_shutdown,
2432 setsockopt: irda_setsockopt,
2433 getsockopt: irda_getsockopt,
2434 sendmsg: irda_sendmsg,
2435 recvmsg: irda_recvmsg_stream,
2437 sendpage: sock_no_sendpage,
2440 static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2443 release: irda_release,
2445 connect: irda_connect,
2446 socketpair: sock_no_socketpair,
2447 accept: irda_accept,
2448 getname: irda_getname,
2449 poll: datagram_poll,
2451 listen: irda_listen,
2452 shutdown: irda_shutdown,
2453 setsockopt: irda_setsockopt,
2454 getsockopt: irda_getsockopt,
2455 sendmsg: irda_sendmsg,
2456 recvmsg: irda_recvmsg_dgram,
2458 sendpage: sock_no_sendpage,
2461 static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2464 release: irda_release,
2466 connect: irda_connect,
2467 socketpair: sock_no_socketpair,
2468 accept: irda_accept,
2469 getname: irda_getname,
2470 poll: datagram_poll,
2472 listen: irda_listen,
2473 shutdown: irda_shutdown,
2474 setsockopt: irda_setsockopt,
2475 getsockopt: irda_getsockopt,
2476 sendmsg: irda_sendmsg_dgram,
2477 recvmsg: irda_recvmsg_dgram,
2479 sendpage: sock_no_sendpage,
2482 #ifdef CONFIG_IRDA_ULTRA
2483 static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2486 release: irda_release,
2488 connect: sock_no_connect,
2489 socketpair: sock_no_socketpair,
2490 accept: sock_no_accept,
2491 getname: irda_getname,
2492 poll: datagram_poll,
2494 listen: sock_no_listen,
2495 shutdown: irda_shutdown,
2496 setsockopt: irda_setsockopt,
2497 getsockopt: irda_getsockopt,
2498 sendmsg: irda_sendmsg_ultra,
2499 recvmsg: irda_recvmsg_dgram,
2501 sendpage: sock_no_sendpage,
2503 #endif /* CONFIG_IRDA_ULTRA */
2505 #include <linux/smp_lock.h>
2506 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2507 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2508 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2509 #ifdef CONFIG_IRDA_ULTRA
2510 SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2511 #endif /* CONFIG_IRDA_ULTRA */
2514 * Function irda_device_event (this, event, ptr)
2516 * Called when a device is taken up or down
2519 static int irda_device_event(struct notifier_block *this, unsigned long event,
2522 struct net_device *dev = (struct net_device *) ptr;
2524 /* Reject non IrDA devices */
2525 if (dev->type != ARPHRD_IRDA)
2530 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_UP\n");
2531 /* irda_dev_device_up(dev); */
2534 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_DOWN\n");
2535 /* irda_kill_by_device(dev); */
2536 /* irda_rt_device_down(dev); */
2537 /* irda_dev_device_down(dev); */
2546 static struct packet_type irda_packet_type =
2548 0, /* MUTTER ntohs(ETH_P_IRDA),*/
2555 static struct notifier_block irda_dev_notifier = {
2562 * Function irda_proc_modcount (inode, fill)
2564 * Use by the proc file system functions to prevent the irda module
2565 * being removed while the use is standing in the net/irda directory
2567 void irda_proc_modcount(struct inode *inode, int fill)
2570 #ifdef CONFIG_PROC_FS
2575 #endif /* CONFIG_PROC_FS */
2580 * Function irda_proto_init (pro)
2582 * Initialize IrDA protocol layer
2585 int __init irda_proto_init(void)
2587 sock_register(&irda_family_ops);
2589 irda_packet_type.type = htons(ETH_P_IRDA);
2590 dev_add_pack(&irda_packet_type);
2592 register_netdevice_notifier(&irda_dev_notifier);
2596 irda_device_init(); /* Called by init/main.c when non-modular */
2601 module_init(irda_proto_init); /* If non-module, called from init/main.c */
2605 * Function irda_proto_cleanup (void)
2607 * Remove IrDA protocol layer
2611 void irda_proto_cleanup(void)
2613 irda_packet_type.type = htons(ETH_P_IRDA);
2614 dev_remove_pack(&irda_packet_type);
2616 unregister_netdevice_notifier(&irda_dev_notifier);
2618 sock_unregister(PF_IRDA);
2623 module_exit(irda_proto_cleanup);
2625 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2626 MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem");
2627 MODULE_LICENSE("GPL");
2628 #ifdef CONFIG_IRDA_DEBUG
2629 MODULE_PARM(irda_debug, "1l");