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,
419 struct irda_sock *self;
421 IRDA_DEBUG(2, __FUNCTION__ "()\n");
423 self = (struct irda_sock *) priv;
425 WARNING(__FUNCTION__ "(), lost myself!\n");
429 /* Check if node is discovered is a new one or an old one.
430 * We check when how long ago this node was discovered, with a
431 * coarse timeout (we may miss some discovery events or be delayed).
432 * Note : by doing this test here, we avoid waking up a process ;-)
434 if((jiffies - discovery->first_timestamp) >
435 (sysctl_discovery_timeout * HZ)) {
436 return; /* Too old, not interesting -> goodbye */
439 /* Pass parameter to the caller */
440 self->cachediscovery = discovery;
442 /* Wake up process if its waiting for device to be discovered */
443 wake_up_interruptible(&self->query_wait);
447 * Function irda_discovery_timeout (priv)
449 * Timeout in the selective discovery process
451 * We were waiting for a node to be discovered, but nothing has come up
452 * so far. Wake up the user and tell him that we failed...
454 static void irda_discovery_timeout(u_long priv)
456 struct irda_sock *self;
458 IRDA_DEBUG(2, __FUNCTION__ "()\n");
460 self = (struct irda_sock *) priv;
461 ASSERT(self != NULL, return;);
463 /* Nothing for the caller */
464 self->cachelog = NULL;
465 self->cachediscovery = NULL;
466 self->errno = -ETIME;
468 /* Wake up process if its still waiting... */
469 wake_up_interruptible(&self->query_wait);
473 * Function irda_open_tsap (self)
475 * Open local Transport Service Access Point (TSAP)
478 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
483 WARNING(__FUNCTION__ "(), busy!\n");
487 /* Initialize callbacks to be used by the IrDA stack */
488 irda_notify_init(¬ify);
489 notify.connect_confirm = irda_connect_confirm;
490 notify.connect_indication = irda_connect_indication;
491 notify.disconnect_indication = irda_disconnect_indication;
492 notify.data_indication = irda_data_indication;
493 notify.udata_indication = irda_data_indication;
494 notify.flow_indication = irda_flow_indication;
495 notify.instance = self;
496 strncpy(notify.name, name, NOTIFY_MAX_NAME);
498 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
500 if (self->tsap == NULL) {
501 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate TSAP!\n");
504 /* Remember which TSAP selector we actually got */
505 self->stsap_sel = self->tsap->stsap_sel;
511 * Function irda_open_lsap (self)
513 * Open local Link Service Access Point (LSAP). Used for opening Ultra
516 #ifdef CONFIG_IRDA_ULTRA
517 static int irda_open_lsap(struct irda_sock *self, int pid)
522 WARNING(__FUNCTION__ "(), busy!\n");
526 /* Initialize callbacks to be used by the IrDA stack */
527 irda_notify_init(¬ify);
528 notify.udata_indication = irda_data_indication;
529 notify.instance = self;
530 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
532 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid);
533 if (self->lsap == NULL) {
534 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate LSAP!\n");
540 #endif /* CONFIG_IRDA_ULTRA */
543 * Function irda_find_lsap_sel (self, name)
545 * Try to lookup LSAP selector in remote LM-IAS
547 * Basically, we start a IAP query, and then go to sleep. When the query
548 * return, irda_getvalue_confirm will wake us up, and we can examine the
549 * result of the query...
550 * Note that in some case, the query fail even before we go to sleep,
551 * creating some races...
553 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
555 IRDA_DEBUG(2, __FUNCTION__ "(%p, %s)\n", self, name);
557 ASSERT(self != NULL, return -1;);
560 WARNING(__FUNCTION__ "(), busy with a previous query\n");
564 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
565 irda_getvalue_confirm);
566 if(self->iriap == NULL)
569 /* Treat unexpected signals as disconnect */
570 self->errno = -EHOSTUNREACH;
572 /* Query remote LM-IAS */
573 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
574 name, "IrDA:TinyTP:LsapSel");
575 /* Wait for answer (if not already failed) */
576 if(self->iriap != NULL)
577 interruptible_sleep_on(&self->query_wait);
579 /* Check what happened */
582 /* Requested object/attribute doesn't exist */
583 if((self->errno == IAS_CLASS_UNKNOWN) ||
584 (self->errno == IAS_ATTRIB_UNKNOWN))
585 return (-EADDRNOTAVAIL);
587 return (-EHOSTUNREACH);
590 /* Get the remote TSAP selector */
591 switch (self->ias_result->type) {
593 IRDA_DEBUG(4, __FUNCTION__ "() int=%d\n",
594 self->ias_result->t.integer);
596 if (self->ias_result->t.integer != -1)
597 self->dtsap_sel = self->ias_result->t.integer;
603 IRDA_DEBUG(0, __FUNCTION__ "(), bad type!\n");
606 if (self->ias_result)
607 irias_delete_value(self->ias_result);
612 return -EADDRNOTAVAIL;
616 * Function irda_discover_daddr_and_lsap_sel (self, name)
618 * This try to find a device with the requested service.
620 * It basically look into the discovery log. For each address in the list,
621 * it queries the LM-IAS of the device to find if this device offer
622 * the requested service.
623 * If there is more than one node supporting the service, we complain
624 * to the user (it should move devices around).
625 * The, we set both the destination address and the lsap selector to point
626 * on the service on the unique device we have found.
628 * Note : this function fails if there is more than one device in range,
629 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
630 * Moreover, we would need to wait the LAP disconnection...
632 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
634 struct irda_device_info *discoveries; /* Copy of the discovery log */
635 int number; /* Number of nodes in the log */
637 int err = -ENETUNREACH;
638 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
639 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
641 IRDA_DEBUG(2, __FUNCTION__ "(), name=%s\n", name);
643 ASSERT(self != NULL, return -1;);
645 /* Ask lmp for the current discovery log
646 * Note : we have to use irlmp_get_discoveries(), as opposed
647 * to play with the cachelog directly, because while we are
648 * making our ias query, le log might change... */
649 discoveries = irlmp_get_discoveries(&number, self->mask, self->nslots);
650 /* Check if the we got some results */
651 if (discoveries == NULL)
652 return -ENETUNREACH; /* No nodes discovered */
655 * Now, check all discovered devices (if any), and connect
656 * client only about the services that the client is
659 for(i = 0; i < number; i++) {
660 /* Try the address in the log */
661 self->daddr = discoveries[i].daddr;
663 IRDA_DEBUG(1, __FUNCTION__ "(), trying daddr = %08x\n",
666 /* Query remote LM-IAS for this service */
667 err = irda_find_lsap_sel(self, name);
670 /* We found the requested service */
671 if(daddr != DEV_ADDR_ANY) {
672 IRDA_DEBUG(1, __FUNCTION__
673 "(), discovered service ''%s'' in two different devices !!!\n",
675 self->daddr = DEV_ADDR_ANY;
679 /* First time we found that one, save it ! */
681 dtsap_sel = self->dtsap_sel;
684 /* Requested service simply doesn't exist on this node */
687 /* Something bad did happen :-( */
688 IRDA_DEBUG(0, __FUNCTION__
689 "(), unexpected IAS query failure\n");
690 self->daddr = DEV_ADDR_ANY;
692 return(-EHOSTUNREACH);
696 /* Cleanup our copy of the discovery log */
699 /* Check out what we found */
700 if(daddr == DEV_ADDR_ANY) {
701 IRDA_DEBUG(1, __FUNCTION__
702 "(), cannot discover service ''%s'' in any device !!!\n",
704 self->daddr = DEV_ADDR_ANY;
705 return(-EADDRNOTAVAIL);
708 /* Revert back to discovered device & service */
711 self->dtsap_sel = dtsap_sel;
713 IRDA_DEBUG(1, __FUNCTION__
714 "(), discovered requested service ''%s'' at address %08x\n",
721 * Function irda_getname (sock, uaddr, uaddr_len, peer)
723 * Return the our own, or peers socket address (sockaddr_irda)
726 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
727 int *uaddr_len, int peer)
729 struct sockaddr_irda saddr;
730 struct sock *sk = sock->sk;
731 struct irda_sock *self = sk->protinfo.irda;
734 if (sk->state != TCP_ESTABLISHED)
737 saddr.sir_family = AF_IRDA;
738 saddr.sir_lsap_sel = self->dtsap_sel;
739 saddr.sir_addr = self->daddr;
741 saddr.sir_family = AF_IRDA;
742 saddr.sir_lsap_sel = self->stsap_sel;
743 saddr.sir_addr = self->saddr;
746 IRDA_DEBUG(1, __FUNCTION__ "(), tsap_sel = %#x\n", saddr.sir_lsap_sel);
747 IRDA_DEBUG(1, __FUNCTION__ "(), addr = %08x\n", saddr.sir_addr);
749 /* uaddr_len come to us uninitialised */
750 *uaddr_len = sizeof (struct sockaddr_irda);
751 memcpy(uaddr, &saddr, *uaddr_len);
757 * Function irda_listen (sock, backlog)
759 * Just move to the listen state
762 static int irda_listen(struct socket *sock, int backlog)
764 struct sock *sk = sock->sk;
766 IRDA_DEBUG(2, __FUNCTION__ "()\n");
768 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
769 (sk->type != SOCK_DGRAM))
772 if (sk->state != TCP_LISTEN) {
773 sk->max_ack_backlog = backlog;
774 sk->state = TCP_LISTEN;
783 * Function irda_bind (sock, uaddr, addr_len)
785 * Used by servers to register their well known TSAP
788 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
790 struct sock *sk = sock->sk;
791 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
792 struct irda_sock *self;
795 self = sk->protinfo.irda;
796 ASSERT(self != NULL, return -1;);
798 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
800 if (addr_len != sizeof(struct sockaddr_irda))
803 #ifdef CONFIG_IRDA_ULTRA
804 /* Special care for Ultra sockets */
805 if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
806 self->pid = addr->sir_lsap_sel;
807 if (self->pid & 0x80) {
808 IRDA_DEBUG(0, __FUNCTION__
809 "(), extension in PID not supp!\n");
812 err = irda_open_lsap(self, self->pid);
816 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
817 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
819 /* Pretend we are connected */
820 sock->state = SS_CONNECTED;
821 sk->state = TCP_ESTABLISHED;
825 #endif /* CONFIG_IRDA_ULTRA */
827 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
831 /* Register with LM-IAS */
832 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
833 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
834 self->stsap_sel, IAS_KERNEL_ATTR);
835 irias_insert_object(self->ias_obj);
841 * Function irda_accept (sock, newsock, flags)
843 * Wait for incoming connection
846 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
848 struct irda_sock *self, *new;
849 struct sock *sk = sock->sk;
854 IRDA_DEBUG(2, __FUNCTION__ "()\n");
856 self = sk->protinfo.irda;
857 ASSERT(self != NULL, return -1;);
859 err = irda_create(newsock, sk->protocol);
863 if (sock->state != SS_UNCONNECTED)
866 if ((sk = sock->sk) == NULL)
869 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
870 (sk->type != SOCK_DGRAM))
873 if (sk->state != TCP_LISTEN)
877 * The read queue this time is holding sockets ready to use
878 * hooked into the SABM we saved
881 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
882 if (flags & O_NONBLOCK)
885 interruptible_sleep_on(sk->sleep);
886 if (signal_pending(current))
889 } while (skb == NULL);
892 newsk->state = TCP_ESTABLISHED;
894 new = newsk->protinfo.irda;
895 ASSERT(new != NULL, return -1;);
897 /* Now attach up the new socket */
898 new->tsap = irttp_dup(self->tsap, new);
900 IRDA_DEBUG(0, __FUNCTION__ "(), dup failed!\n");
904 new->stsap_sel = new->tsap->stsap_sel;
905 new->dtsap_sel = new->tsap->dtsap_sel;
906 new->saddr = irttp_get_saddr(new->tsap);
907 new->daddr = irttp_get_daddr(new->tsap);
909 new->max_sdu_size_tx = self->max_sdu_size_tx;
910 new->max_sdu_size_rx = self->max_sdu_size_rx;
911 new->max_data_size = self->max_data_size;
912 new->max_header_size = self->max_header_size;
914 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
916 /* Clean up the original one to keep it in listen state */
917 self->tsap->dtsap_sel = self->tsap->lsap->dlsap_sel = LSAP_ANY;
918 self->tsap->lsap->lsap_state = LSAP_DISCONNECTED;
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 cli(); /* To avoid races on the sleep */
1032 /* A Connect Ack with Choke or timeout or failed routing will go to
1034 while (sk->state == TCP_SYN_SENT) {
1035 interruptible_sleep_on(sk->sleep);
1036 if (signal_pending(current)) {
1038 return -ERESTARTSYS;
1042 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;
1052 /* At this point, IrLMP has assigned our source address */
1053 self->saddr = irttp_get_saddr(self->tsap);
1059 * Function irda_create (sock, protocol)
1061 * Create IrDA socket
1064 static int irda_create(struct socket *sock, int protocol)
1067 struct irda_sock *self;
1069 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1071 /* Check for valid socket type */
1072 switch (sock->type) {
1073 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1074 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1075 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1078 return -ESOCKTNOSUPPORT;
1081 /* Allocate networking socket */
1082 if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
1085 /* Allocate IrDA socket */
1086 self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
1091 memset(self, 0, sizeof(struct irda_sock));
1093 IRDA_DEBUG(2, __FUNCTION__ "() : self is %p\n", self);
1095 init_waitqueue_head(&self->query_wait);
1097 /* Initialise networking socket struct */
1098 sock_init_data(sock, sk); /* Note : set sk->refcnt to 1 */
1099 sk->family = PF_IRDA;
1100 sk->protocol = protocol;
1101 /* Link networking socket and IrDA socket structs together */
1102 sk->protinfo.irda = self;
1105 switch (sock->type) {
1107 sock->ops = &irda_stream_ops;
1108 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1110 case SOCK_SEQPACKET:
1111 sock->ops = &irda_seqpacket_ops;
1112 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1116 #ifdef CONFIG_IRDA_ULTRA
1117 case IRDAPROTO_ULTRA:
1118 sock->ops = &irda_ultra_ops;
1120 #endif /* CONFIG_IRDA_ULTRA */
1121 case IRDAPROTO_UNITDATA:
1122 sock->ops = &irda_dgram_ops;
1123 /* We let Unitdata conn. be like seqpack conn. */
1124 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1127 ERROR(__FUNCTION__ "(), protocol not supported!\n");
1128 return -ESOCKTNOSUPPORT;
1132 return -ESOCKTNOSUPPORT;
1135 /* Register as a client with IrLMP */
1136 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1137 self->mask = 0xffff;
1138 self->rx_flow = self->tx_flow = FLOW_START;
1139 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1140 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1141 self->saddr = 0x0; /* so IrLMP assign us any link */
1149 * Function irda_destroy_socket (self)
1154 void irda_destroy_socket(struct irda_sock *self)
1156 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1158 ASSERT(self != NULL, return;);
1160 /* Unregister with IrLMP */
1161 irlmp_unregister_client(self->ckey);
1162 irlmp_unregister_service(self->skey);
1164 /* Unregister with LM-IAS */
1165 if (self->ias_obj) {
1166 irias_delete_object(self->ias_obj);
1167 self->ias_obj = NULL;
1171 iriap_close(self->iriap);
1176 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1177 irttp_close_tsap(self->tsap);
1180 #ifdef CONFIG_IRDA_ULTRA
1182 irlmp_close_lsap(self->lsap);
1185 #endif /* CONFIG_IRDA_ULTRA */
1193 * Function irda_release (sock)
1198 static int irda_release(struct socket *sock)
1200 struct sock *sk = sock->sk;
1202 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1207 sk->state = TCP_CLOSE;
1208 sk->shutdown |= SEND_SHUTDOWN;
1209 sk->state_change(sk);
1211 /* Destroy IrDA socket */
1212 irda_destroy_socket(sk->protinfo.irda);
1213 /* Prevent sock_def_destruct() to create havoc */
1214 sk->protinfo.irda = NULL;
1219 /* Purge queues (see sock_init_data()) */
1220 skb_queue_purge(&sk->receive_queue);
1222 /* Destroy networking socket if we are the last reference on it,
1223 * i.e. if(sk->refcnt == 0) -> sk_free(sk) */
1226 /* Notes on socket locking and deallocation... - Jean II
1227 * In theory we should put pairs of sock_hold() / sock_put() to
1228 * prevent the socket to be destroyed whenever there is an
1229 * outstanding request or outstanding incomming packet or event.
1231 * 1) This may include IAS request, both in connect and getsockopt.
1232 * Unfortunately, the situation is a bit more messy than it looks,
1233 * because we close iriap and kfree(self) above.
1235 * 2) This may include selective discovery in getsockopt.
1236 * Same stuff as above, irlmp registration and self are gone.
1238 * Probably 1 and 2 may not matter, because it's all triggered
1239 * by a process and the socket layer already prevent the
1240 * socket to go away while a process is holding it, through
1241 * sockfd_put() and fput()...
1243 * 3) This may include deferred TSAP closure. In particular,
1244 * we may receive a late irda_disconnect_indication()
1245 * Fortunately, (tsap_cb *)->close_pend should protect us
1248 * I did some testing on SMP, and it looks solid. And the socket
1249 * memory leak is now gone... - Jean II
1256 * Function irda_sendmsg (sock, msg, len, scm)
1258 * Send message down to TinyTP. This function is used for both STREAM and
1259 * SEQPACK services. This is possible since it forces the client to
1260 * fragment the message if necessary
1262 static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1263 struct scm_cookie *scm)
1265 struct sock *sk = sock->sk;
1266 struct irda_sock *self;
1267 struct sk_buff *skb;
1268 unsigned char *asmptr;
1271 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1273 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1274 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
1277 if (sk->shutdown & SEND_SHUTDOWN) {
1278 send_sig(SIGPIPE, current, 0);
1282 if (sk->state != TCP_ESTABLISHED)
1285 self = sk->protinfo.irda;
1286 ASSERT(self != NULL, return -1;);
1288 /* Check if IrTTP is wants us to slow down */
1289 while (self->tx_flow == FLOW_STOP) {
1290 IRDA_DEBUG(2, __FUNCTION__ "(), IrTTP is busy, going to sleep!\n");
1291 interruptible_sleep_on(sk->sleep);
1293 /* Check if we are still connected */
1294 if (sk->state != TCP_ESTABLISHED)
1298 /* Check that we don't send out to big frames */
1299 if (len > self->max_data_size) {
1300 IRDA_DEBUG(2, __FUNCTION__
1301 "(), Chopping frame from %d to %d bytes!\n", len,
1302 self->max_data_size);
1303 len = self->max_data_size;
1306 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1307 msg->msg_flags & MSG_DONTWAIT, &err);
1311 skb_reserve(skb, self->max_header_size);
1313 asmptr = skb->h.raw = skb_put(skb, len);
1314 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1317 * Just send the message to TinyTP, and let it deal with possible
1318 * errors. No need to duplicate all that here
1320 err = irttp_data_request(self->tsap, skb);
1322 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1325 /* Tell client how much data we actually sent */
1330 * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
1332 * Try to receive message and copy it to user. The frame is discarded
1333 * after being read, regardless of how much the user actually read
1335 static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
1336 int size, int flags, struct scm_cookie *scm)
1338 struct irda_sock *self;
1339 struct sock *sk = sock->sk;
1340 struct sk_buff *skb;
1343 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1345 self = sk->protinfo.irda;
1346 ASSERT(self != NULL, return -1;);
1348 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1349 flags & MSG_DONTWAIT, &err);
1353 skb->h.raw = skb->data;
1356 if (copied > size) {
1357 IRDA_DEBUG(2, __FUNCTION__
1358 "(), Received truncated frame (%d < %d)!\n",
1361 msg->msg_flags |= MSG_TRUNC;
1363 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1365 skb_free_datagram(sk, skb);
1368 * Check if we have previously stopped IrTTP and we know
1369 * have more free space in our rx_queue. If so tell IrTTP
1370 * to start delivering frames again before our rx_queue gets
1373 if (self->rx_flow == FLOW_STOP) {
1374 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1375 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1376 self->rx_flow = FLOW_START;
1377 irttp_flow_request(self->tsap, FLOW_START);
1385 * Function irda_data_wait (sk)
1387 * Sleep until data has arrive. But check for races..
1390 static void irda_data_wait(struct sock *sk)
1392 if (!skb_peek(&sk->receive_queue)) {
1393 set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1394 interruptible_sleep_on(sk->sleep);
1395 clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1400 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
1405 static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
1406 int size, int flags, struct scm_cookie *scm)
1408 struct irda_sock *self;
1409 struct sock *sk = sock->sk;
1410 int noblock = flags & MSG_DONTWAIT;
1414 IRDA_DEBUG(3, __FUNCTION__ "()\n");
1416 self = sk->protinfo.irda;
1417 ASSERT(self != NULL, return -1;);
1419 if (sock->flags & __SO_ACCEPTCON)
1422 if (flags & MSG_OOB)
1425 if (flags & MSG_WAITALL)
1428 msg->msg_namelen = 0;
1432 struct sk_buff *skb;
1434 skb=skb_dequeue(&sk->receive_queue);
1436 if (copied >= target)
1440 * POSIX 1003.1g mandates this order.
1444 return sock_error(sk);
1447 if (sk->shutdown & RCV_SHUTDOWN)
1453 if (signal_pending(current))
1454 return -ERESTARTSYS;
1458 chunk = min_t(unsigned int, skb->len, size);
1459 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1460 skb_queue_head(&sk->receive_queue, skb);
1468 /* Mark read part of skb as used */
1469 if (!(flags & MSG_PEEK)) {
1470 skb_pull(skb, chunk);
1472 /* put the skb back if we didn't use it up.. */
1474 IRDA_DEBUG(1, __FUNCTION__ "(), back on q!\n");
1475 skb_queue_head(&sk->receive_queue, skb);
1481 IRDA_DEBUG(0, __FUNCTION__ "() questionable!?\n");
1483 /* put message back and return */
1484 skb_queue_head(&sk->receive_queue, skb);
1490 * Check if we have previously stopped IrTTP and we know
1491 * have more free space in our rx_queue. If so tell IrTTP
1492 * to start delivering frames again before our rx_queue gets
1495 if (self->rx_flow == FLOW_STOP) {
1496 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1497 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1498 self->rx_flow = FLOW_START;
1499 irttp_flow_request(self->tsap, FLOW_START);
1507 * Function irda_sendmsg_dgram (sock, msg, len, scm)
1509 * Send message down to TinyTP for the unreliable sequenced
1513 static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
1514 int len, struct scm_cookie *scm)
1516 struct sock *sk = sock->sk;
1517 struct irda_sock *self;
1518 struct sk_buff *skb;
1519 unsigned char *asmptr;
1522 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1524 if (msg->msg_flags & ~MSG_DONTWAIT)
1527 if (sk->shutdown & SEND_SHUTDOWN) {
1528 send_sig(SIGPIPE, current, 0);
1532 if (sk->state != TCP_ESTABLISHED)
1535 self = sk->protinfo.irda;
1536 ASSERT(self != NULL, return -1;);
1539 * Check that we don't send out to big frames. This is an unreliable
1540 * service, so we have no fragmentation and no coalescence
1542 if (len > self->max_data_size) {
1543 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1544 "Chopping frame from %d to %d bytes!\n", len,
1545 self->max_data_size);
1546 len = self->max_data_size;
1549 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1550 msg->msg_flags & MSG_DONTWAIT, &err);
1554 skb_reserve(skb, self->max_header_size);
1556 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1557 asmptr = skb->h.raw = skb_put(skb, len);
1558 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1561 * Just send the message to TinyTP, and let it deal with possible
1562 * errors. No need to duplicate all that here
1564 err = irttp_udata_request(self->tsap, skb);
1566 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1573 * Function irda_sendmsg_ultra (sock, msg, len, scm)
1575 * Send message down to IrLMP for the unreliable Ultra
1578 #ifdef CONFIG_IRDA_ULTRA
1579 static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
1580 int len, struct scm_cookie *scm)
1582 struct sock *sk = sock->sk;
1583 struct irda_sock *self;
1584 struct sk_buff *skb;
1585 unsigned char *asmptr;
1588 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1590 if (msg->msg_flags & ~MSG_DONTWAIT)
1593 if (sk->shutdown & SEND_SHUTDOWN) {
1594 send_sig(SIGPIPE, current, 0);
1598 self = sk->protinfo.irda;
1599 ASSERT(self != NULL, return -1;);
1602 * Check that we don't send out to big frames. This is an unreliable
1603 * service, so we have no fragmentation and no coalescence
1605 if (len > self->max_data_size) {
1606 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1607 "Chopping frame from %d to %d bytes!\n", len,
1608 self->max_data_size);
1609 len = self->max_data_size;
1612 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1613 msg->msg_flags & MSG_DONTWAIT, &err);
1617 skb_reserve(skb, self->max_header_size);
1619 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1620 asmptr = skb->h.raw = skb_put(skb, len);
1621 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1623 err = irlmp_connless_data_request(self->lsap, skb);
1625 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1630 #endif /* CONFIG_IRDA_ULTRA */
1633 * Function irda_shutdown (sk, how)
1638 static int irda_shutdown(struct socket *sock, int how)
1640 struct irda_sock *self;
1641 struct sock *sk = sock->sk;
1643 self = sk->protinfo.irda;
1644 ASSERT(self != NULL, return -1;);
1646 IRDA_DEBUG(1, __FUNCTION__ "(%p)\n", self);
1648 sk->state = TCP_CLOSE;
1649 sk->shutdown |= SEND_SHUTDOWN;
1650 sk->state_change(sk);
1653 iriap_close(self->iriap);
1658 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1659 irttp_close_tsap(self->tsap);
1663 /* A few cleanup so the socket look as good as new... */
1664 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1665 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1666 self->saddr = 0x0; /* so IrLMP assign us any link */
1672 * Function irda_poll (file, sock, wait)
1677 static unsigned int irda_poll(struct file * file, struct socket *sock,
1680 struct sock *sk = sock->sk;
1682 struct irda_sock *self;
1684 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1686 self = sk->protinfo.irda;
1687 poll_wait(file, sk->sleep, wait);
1690 /* Exceptional events? */
1693 if (sk->shutdown & RCV_SHUTDOWN) {
1694 IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1699 if (!skb_queue_empty(&sk->receive_queue)) {
1700 IRDA_DEBUG(4, "Socket is readable\n");
1701 mask |= POLLIN | POLLRDNORM;
1704 /* Connection-based need to check for termination and startup */
1707 if (sk->state == TCP_CLOSE) {
1708 IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1712 if (sk->state == TCP_ESTABLISHED) {
1713 if ((self->tx_flow == FLOW_START) &&
1714 (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE))
1716 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1720 case SOCK_SEQPACKET:
1721 if ((self->tx_flow == FLOW_START) &&
1722 (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE))
1724 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1728 if (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE)
1729 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1738 * Function irda_ioctl (sock, cmd, arg)
1743 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1745 struct sock *sk = sock->sk;
1747 IRDA_DEBUG(4, __FUNCTION__ "(), cmd=%#x\n", cmd);
1752 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1755 if (put_user(amount, (unsigned int *)arg))
1761 struct sk_buff *skb;
1763 /* These two are safe on a single CPU system as only user tasks fiddle here */
1764 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1766 if (put_user(amount, (unsigned int *)arg))
1773 if (sk->stamp.tv_sec == 0)
1775 if (copy_to_user((void *)arg, &sk->stamp,
1776 sizeof(struct timeval)))
1784 case SIOCGIFDSTADDR:
1785 case SIOCSIFDSTADDR:
1786 case SIOCGIFBRDADDR:
1787 case SIOCSIFBRDADDR:
1788 case SIOCGIFNETMASK:
1789 case SIOCSIFNETMASK:
1794 IRDA_DEBUG(1, __FUNCTION__ "(), doing device ioctl!\n");
1795 return dev_ioctl(cmd, (void *) arg);
1803 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1805 * Set some options for the socket
1808 static int irda_setsockopt(struct socket *sock, int level, int optname,
1809 char *optval, int optlen)
1811 struct sock *sk = sock->sk;
1812 struct irda_sock *self;
1813 struct irda_ias_set *ias_opt;
1814 struct ias_object *ias_obj;
1815 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1818 self = sk->protinfo.irda;
1819 ASSERT(self != NULL, return -1;);
1821 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1823 if (level != SOL_IRLMP)
1824 return -ENOPROTOOPT;
1828 /* The user want to add an attribute to an existing IAS object
1829 * (in the IAS database) or to create a new object with this
1831 * We first query IAS to know if the object exist, and then
1832 * create the right attribute...
1835 if (optlen != sizeof(struct irda_ias_set))
1838 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1839 if (ias_opt == NULL)
1842 /* Copy query to the driver. */
1843 if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1848 /* Find the object we target */
1849 ias_obj = irias_find_object(ias_opt->irda_class_name);
1850 if(ias_obj == (struct ias_object *) NULL) {
1851 /* Create a new object */
1852 ias_obj = irias_new_object(ias_opt->irda_class_name,
1856 /* Do we have it already ? */
1857 if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1862 /* Look at the type */
1863 switch(ias_opt->irda_attrib_type) {
1865 /* Add an integer attribute */
1866 irias_add_integer_attrib(
1868 ias_opt->irda_attrib_name,
1869 ias_opt->attribute.irda_attrib_int,
1874 if(ias_opt->attribute.irda_attrib_octet_seq.len >
1875 IAS_MAX_OCTET_STRING) {
1879 /* Add an octet sequence attribute */
1880 irias_add_octseq_attrib(
1882 ias_opt->irda_attrib_name,
1883 ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1884 ias_opt->attribute.irda_attrib_octet_seq.len,
1888 /* Should check charset & co */
1890 if(ias_opt->attribute.irda_attrib_string.len >
1895 /* NULL terminate the string (avoid troubles) */
1896 ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1897 /* Add a string attribute */
1898 irias_add_string_attrib(
1900 ias_opt->irda_attrib_name,
1901 ias_opt->attribute.irda_attrib_string.string,
1908 irias_insert_object(ias_obj);
1912 /* The user want to delete an object from our local IAS
1913 * database. We just need to query the IAS, check is the
1914 * object is not owned by the kernel and delete it.
1917 if (optlen != sizeof(struct irda_ias_set))
1920 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1921 if (ias_opt == NULL)
1924 /* Copy query to the driver. */
1925 if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1930 /* Find the object we target */
1931 ias_obj = irias_find_object(ias_opt->irda_class_name);
1932 if(ias_obj == (struct ias_object *) NULL) {
1937 /* Find the attribute (in the object) we target */
1938 ias_attr = irias_find_attrib(ias_obj,
1939 ias_opt->irda_attrib_name);
1940 if(ias_attr == (struct ias_attrib *) NULL) {
1945 /* Check is the user space own the object */
1946 if(ias_attr->value->owner != IAS_USER_ATTR) {
1947 IRDA_DEBUG(1, __FUNCTION__
1948 "(), attempting to delete a kernel attribute\n");
1953 /* Remove the attribute (and maybe the object) */
1954 irias_delete_attrib(ias_obj, ias_attr);
1957 case IRLMP_MAX_SDU_SIZE:
1958 if (optlen < sizeof(int))
1961 if (get_user(opt, (int *)optval))
1964 /* Only possible for a seqpacket service (TTP with SAR) */
1965 if (sk->type != SOCK_SEQPACKET) {
1966 IRDA_DEBUG(2, __FUNCTION__
1967 "(), setting max_sdu_size = %d\n", opt);
1968 self->max_sdu_size_rx = opt;
1970 WARNING(__FUNCTION__
1971 "(), not allowed to set MAXSDUSIZE for this "
1973 return -ENOPROTOOPT;
1976 case IRLMP_HINTS_SET:
1977 if (optlen < sizeof(int))
1980 if (get_user(opt, (int *)optval))
1983 /* Unregister any old registration */
1985 irlmp_unregister_service(self->skey);
1987 self->skey = irlmp_register_service((__u16) opt);
1989 case IRLMP_HINT_MASK_SET:
1990 /* As opposed to the previous case which set the hint bits
1991 * that we advertise, this one set the filter we use when
1992 * making a discovery (nodes which don't match any hint
1993 * bit in the mask are not reported).
1995 if (optlen < sizeof(int))
1998 if (get_user(opt, (int *)optval))
2001 /* Set the new hint mask */
2002 self->mask = (__u16) opt;
2003 /* Mask out extension bits */
2004 self->mask &= 0x7f7f;
2005 /* Check if no bits */
2007 self->mask = 0xFFFF;
2011 return -ENOPROTOOPT;
2017 * Function irda_extract_ias_value(ias_opt, ias_value)
2019 * Translate internal IAS value structure to the user space representation
2021 * The external representation of IAS values, as we exchange them with
2022 * user space program is quite different from the internal representation,
2023 * as stored in the IAS database (because we need a flat structure for
2024 * crossing kernel boundary).
2025 * This function transform the former in the latter. We also check
2026 * that the value type is valid.
2028 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2029 struct ias_value *ias_value)
2031 /* Look at the type */
2032 switch (ias_value->type) {
2034 /* Copy the integer */
2035 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2039 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2041 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2042 ias_value->t.oct_seq, ias_value->len);
2046 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2047 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2049 memcpy(ias_opt->attribute.irda_attrib_string.string,
2050 ias_value->t.string, ias_value->len);
2051 /* NULL terminate the string (avoid troubles) */
2052 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2059 /* Copy type over */
2060 ias_opt->irda_attrib_type = ias_value->type;
2066 * Function irda_getsockopt (sock, level, optname, optval, optlen)
2071 static int irda_getsockopt(struct socket *sock, int level, int optname,
2072 char *optval, int *optlen)
2074 struct sock *sk = sock->sk;
2075 struct irda_sock *self;
2076 struct irda_device_list list;
2077 struct irda_device_info *discoveries;
2078 struct irda_ias_set * ias_opt; /* IAS get/query params */
2079 struct ias_object * ias_obj; /* Object in IAS */
2080 struct ias_attrib * ias_attr; /* Attribute in IAS object */
2081 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
2087 self = sk->protinfo.irda;
2089 IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
2091 if (level != SOL_IRLMP)
2092 return -ENOPROTOOPT;
2094 if (get_user(len, optlen))
2101 case IRLMP_ENUMDEVICES:
2102 /* Ask lmp for the current discovery log */
2103 discoveries = irlmp_get_discoveries(&list.len, self->mask,
2105 /* Check if the we got some results */
2106 if (discoveries == NULL)
2107 return -EAGAIN; /* Didn't find any devices */
2110 /* Write total list length back to client */
2111 if (copy_to_user(optval, &list,
2112 sizeof(struct irda_device_list) -
2113 sizeof(struct irda_device_info)))
2116 /* Offset to first device entry */
2117 offset = sizeof(struct irda_device_list) -
2118 sizeof(struct irda_device_info);
2120 /* Copy the list itself - watch for overflow */
2126 total = offset + (list.len * sizeof(struct irda_device_info));
2129 if (copy_to_user(optval+offset, discoveries, total - offset))
2132 /* Write total number of bytes used back to client */
2133 if (put_user(total, optlen))
2136 /* Free up our buffer */
2141 case IRLMP_MAX_SDU_SIZE:
2142 val = self->max_data_size;
2144 if (put_user(len, optlen))
2147 if (copy_to_user(optval, &val, len))
2151 /* The user want an object from our local IAS database.
2152 * We just need to query the IAS and return the value
2155 /* Check that the user has allocated the right space for us */
2156 if (len != sizeof(struct irda_ias_set))
2159 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2160 if (ias_opt == NULL)
2163 /* Copy query to the driver. */
2164 if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2169 /* Find the object we target */
2170 ias_obj = irias_find_object(ias_opt->irda_class_name);
2171 if(ias_obj == (struct ias_object *) NULL) {
2176 /* Find the attribute (in the object) we target */
2177 ias_attr = irias_find_attrib(ias_obj,
2178 ias_opt->irda_attrib_name);
2179 if(ias_attr == (struct ias_attrib *) NULL) {
2184 /* Translate from internal to user structure */
2185 err = irda_extract_ias_value(ias_opt, ias_attr->value);
2191 /* Copy reply to the user */
2192 if (copy_to_user((char *)optval, (char *) ias_opt,
2193 sizeof(struct irda_ias_set))) {
2197 /* Note : don't need to put optlen, we checked it */
2200 case IRLMP_IAS_QUERY:
2201 /* The user want an object from a remote IAS database.
2202 * We need to use IAP to query the remote database and
2203 * then wait for the answer to come back. */
2205 /* Check that the user has allocated the right space for us */
2206 if (len != sizeof(struct irda_ias_set))
2209 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2210 if (ias_opt == NULL)
2213 /* Copy query to the driver. */
2214 if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2219 /* At this point, there are two cases...
2220 * 1) the socket is connected - that's the easy case, we
2221 * just query the device we are connected to...
2222 * 2) the socket is not connected - the user doesn't want
2223 * to connect and/or may not have a valid service name
2224 * (so can't create a fake connection). In this case,
2225 * we assume that the user pass us a valid destination
2226 * address in the requesting structure...
2228 if(self->daddr != DEV_ADDR_ANY) {
2229 /* We are connected - reuse known daddr */
2230 daddr = self->daddr;
2232 /* We are not connected, we must specify a valid
2233 * destination address */
2234 daddr = ias_opt->daddr;
2235 if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2241 /* Check that we can proceed with IAP */
2243 WARNING(__FUNCTION__
2244 "(), busy with a previous query\n");
2249 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2250 irda_getvalue_confirm);
2252 /* Treat unexpected signals as disconnect */
2253 self->errno = -EHOSTUNREACH;
2255 /* Query remote LM-IAS */
2256 iriap_getvaluebyclass_request(self->iriap,
2258 ias_opt->irda_class_name,
2259 ias_opt->irda_attrib_name);
2260 /* Wait for answer (if not already failed) */
2261 if(self->iriap != NULL)
2262 interruptible_sleep_on(&self->query_wait);
2263 /* Check what happened */
2267 /* Requested object/attribute doesn't exist */
2268 if((self->errno == IAS_CLASS_UNKNOWN) ||
2269 (self->errno == IAS_ATTRIB_UNKNOWN))
2270 return (-EADDRNOTAVAIL);
2272 return (-EHOSTUNREACH);
2275 /* Translate from internal to user structure */
2276 err = irda_extract_ias_value(ias_opt, self->ias_result);
2277 if (self->ias_result)
2278 irias_delete_value(self->ias_result);
2284 /* Copy reply to the user */
2285 if (copy_to_user((char *)optval, (char *) ias_opt,
2286 sizeof(struct irda_ias_set))) {
2290 /* Note : don't need to put optlen, we checked it */
2293 case IRLMP_WAITDEVICE:
2294 /* This function is just another way of seeing life ;-)
2295 * IRLMP_ENUMDEVICES assumes that you have a static network,
2296 * and that you just want to pick one of the devices present.
2297 * On the other hand, in here we assume that no device is
2298 * present and that at some point in the future a device will
2299 * come into range. When this device arrive, we just wake
2300 * up the caller, so that he has time to connect to it before
2301 * the device goes away...
2302 * Note : once the node has been discovered for more than a
2303 * few second, it won't trigger this function, unless it
2304 * goes away and come back changes its hint bits (so we
2305 * might call it IRLMP_WAITNEWDEVICE).
2308 /* Check that the user is passing us an int */
2309 if (len != sizeof(int))
2311 /* Get timeout in ms (max time we block the caller) */
2312 if (get_user(val, (int *)optval))
2315 /* Tell IrLMP we want to be notified */
2316 irlmp_update_client(self->ckey, self->mask,
2317 irda_selective_discovery_indication,
2318 NULL, (void *) self);
2320 /* Do some discovery (and also return cached results) */
2321 irlmp_discovery_request(self->nslots);
2323 /* Wait until a node is discovered */
2324 if (!self->cachediscovery) {
2325 IRDA_DEBUG(1, __FUNCTION__
2326 "(), nothing discovered yet, going to sleep...\n");
2328 /* Set watchdog timer to expire in <val> ms. */
2329 self->watchdog.function = irda_discovery_timeout;
2330 self->watchdog.data = (unsigned long) self;
2331 self->watchdog.expires = jiffies + (val * HZ/1000);
2332 add_timer(&(self->watchdog));
2334 /* Wait for IR-LMP to call us back */
2335 interruptible_sleep_on(&self->query_wait);
2337 /* If watchdog is still activated, kill it! */
2338 if(timer_pending(&(self->watchdog)))
2339 del_timer(&(self->watchdog));
2341 IRDA_DEBUG(1, __FUNCTION__
2342 "(), ...waking up !\n");
2345 IRDA_DEBUG(1, __FUNCTION__
2346 "(), found immediately !\n");
2348 /* Tell IrLMP that we have been notified */
2349 irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
2351 /* Check if the we got some results */
2352 if (!self->cachediscovery)
2353 return -EAGAIN; /* Didn't find any devices */
2355 self->cachediscovery = NULL;
2357 /* Note : We don't return anything to the user.
2358 * We could return the device that triggered the wake up,
2359 * but it's probably better to force the user to query
2360 * the whole discovery log and let him pick one device...
2364 return -ENOPROTOOPT;
2370 static struct net_proto_family irda_family_ops =
2376 static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2379 release: irda_release,
2381 connect: irda_connect,
2382 socketpair: sock_no_socketpair,
2383 accept: irda_accept,
2384 getname: irda_getname,
2387 listen: irda_listen,
2388 shutdown: irda_shutdown,
2389 setsockopt: irda_setsockopt,
2390 getsockopt: irda_getsockopt,
2391 sendmsg: irda_sendmsg,
2392 recvmsg: irda_recvmsg_stream,
2394 sendpage: sock_no_sendpage,
2397 static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2400 release: irda_release,
2402 connect: irda_connect,
2403 socketpair: sock_no_socketpair,
2404 accept: irda_accept,
2405 getname: irda_getname,
2406 poll: datagram_poll,
2408 listen: irda_listen,
2409 shutdown: irda_shutdown,
2410 setsockopt: irda_setsockopt,
2411 getsockopt: irda_getsockopt,
2412 sendmsg: irda_sendmsg,
2413 recvmsg: irda_recvmsg_dgram,
2415 sendpage: sock_no_sendpage,
2418 static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2421 release: irda_release,
2423 connect: irda_connect,
2424 socketpair: sock_no_socketpair,
2425 accept: irda_accept,
2426 getname: irda_getname,
2427 poll: datagram_poll,
2429 listen: irda_listen,
2430 shutdown: irda_shutdown,
2431 setsockopt: irda_setsockopt,
2432 getsockopt: irda_getsockopt,
2433 sendmsg: irda_sendmsg_dgram,
2434 recvmsg: irda_recvmsg_dgram,
2436 sendpage: sock_no_sendpage,
2439 #ifdef CONFIG_IRDA_ULTRA
2440 static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2443 release: irda_release,
2445 connect: sock_no_connect,
2446 socketpair: sock_no_socketpair,
2447 accept: sock_no_accept,
2448 getname: irda_getname,
2449 poll: datagram_poll,
2451 listen: sock_no_listen,
2452 shutdown: irda_shutdown,
2453 setsockopt: irda_setsockopt,
2454 getsockopt: irda_getsockopt,
2455 sendmsg: irda_sendmsg_ultra,
2456 recvmsg: irda_recvmsg_dgram,
2458 sendpage: sock_no_sendpage,
2460 #endif /* CONFIG_IRDA_ULTRA */
2462 #include <linux/smp_lock.h>
2463 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2464 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2465 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2466 #ifdef CONFIG_IRDA_ULTRA
2467 SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2468 #endif /* CONFIG_IRDA_ULTRA */
2471 * Function irda_device_event (this, event, ptr)
2473 * Called when a device is taken up or down
2476 static int irda_device_event(struct notifier_block *this, unsigned long event,
2479 struct net_device *dev = (struct net_device *) ptr;
2481 /* Reject non IrDA devices */
2482 if (dev->type != ARPHRD_IRDA)
2487 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_UP\n");
2488 /* irda_dev_device_up(dev); */
2491 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_DOWN\n");
2492 /* irda_kill_by_device(dev); */
2493 /* irda_rt_device_down(dev); */
2494 /* irda_dev_device_down(dev); */
2503 static struct packet_type irda_packet_type =
2505 0, /* MUTTER ntohs(ETH_P_IRDA),*/
2512 static struct notifier_block irda_dev_notifier = {
2519 * Function irda_proc_modcount (inode, fill)
2521 * Use by the proc file system functions to prevent the irda module
2522 * being removed while the use is standing in the net/irda directory
2524 void irda_proc_modcount(struct inode *inode, int fill)
2527 #ifdef CONFIG_PROC_FS
2532 #endif /* CONFIG_PROC_FS */
2537 * Function irda_proto_init (pro)
2539 * Initialize IrDA protocol layer
2542 int __init irda_proto_init(void)
2544 sock_register(&irda_family_ops);
2546 irda_packet_type.type = htons(ETH_P_IRDA);
2547 dev_add_pack(&irda_packet_type);
2549 register_netdevice_notifier(&irda_dev_notifier);
2553 irda_device_init(); /* Called by init/main.c when non-modular */
2558 module_init(irda_proto_init); /* If non-module, called from init/main.c */
2562 * Function irda_proto_cleanup (void)
2564 * Remove IrDA protocol layer
2568 void irda_proto_cleanup(void)
2570 irda_packet_type.type = htons(ETH_P_IRDA);
2571 dev_remove_pack(&irda_packet_type);
2573 unregister_netdevice_notifier(&irda_dev_notifier);
2575 sock_unregister(PF_IRDA);
2580 module_exit(irda_proto_cleanup);
2582 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2583 MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem");
2584 MODULE_LICENSE("GPL");
2585 #ifdef CONFIG_IRDA_DEBUG
2586 MODULE_PARM(irda_debug, "1l");