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-2003 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/capability.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/net.h>
52 #include <linux/irda.h>
53 #include <linux/poll.h>
55 #include <asm/ioctls.h> /* TIOCOUTQ, TIOCINQ */
56 #include <asm/uaccess.h>
59 #include <net/tcp_states.h>
61 #include <net/irda/af_irda.h>
63 static int irda_create(struct socket *sock, int protocol);
65 static const struct proto_ops irda_stream_ops;
66 static const struct proto_ops irda_seqpacket_ops;
67 static const struct proto_ops irda_dgram_ops;
69 #ifdef CONFIG_IRDA_ULTRA
70 static const struct proto_ops irda_ultra_ops;
71 #define ULTRA_MAX_DATA 382
72 #endif /* CONFIG_IRDA_ULTRA */
74 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
77 * Function irda_data_indication (instance, sap, skb)
79 * Received some data from TinyTP. Just queue it on the receive queue
82 static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
84 struct irda_sock *self;
88 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
92 IRDA_ASSERT(sk != NULL, return -1;);
94 err = sock_queue_rcv_skb(sk, skb);
96 IRDA_DEBUG(1, "%s(), error: no more mem!\n", __FUNCTION__);
97 self->rx_flow = FLOW_STOP;
99 /* When we return error, TTP will need to requeue the skb */
107 * Function irda_disconnect_indication (instance, sap, reason, skb)
109 * Connection has been closed. Check reason to find out why
112 static void irda_disconnect_indication(void *instance, void *sap,
113 LM_REASON reason, struct sk_buff *skb)
115 struct irda_sock *self;
120 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
122 /* Don't care about it, but let's not leak it */
128 IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n",
133 /* Prevent race conditions with irda_release() and irda_shutdown() */
134 if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) {
136 sk->sk_state = TCP_CLOSE;
137 sk->sk_err = ECONNRESET;
138 sk->sk_shutdown |= SEND_SHUTDOWN;
140 sk->sk_state_change(sk);
144 * If we leave it open, IrLMP put it back into the list of
145 * unconnected LSAPs. The problem is that any incoming request
146 * can then be matched to this socket (and it will be, because
147 * it is at the head of the list). This would prevent any
148 * listening socket waiting on the same TSAP to get those
149 * requests. Some apps forget to close sockets, or hang to it
150 * a bit too long, so we may stay in this dead state long
151 * enough to be noticed...
152 * Note : all socket function do check sk->sk_state, so we are
157 irttp_close_tsap(self->tsap);
162 /* Note : once we are there, there is not much you want to do
163 * with the socket anymore, apart from closing it.
164 * For example, bind() and connect() won't reset sk->sk_err,
165 * sk->sk_shutdown and sk->sk_flags to valid values...
171 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
173 * Connections has been confirmed by the remote device
176 static void irda_connect_confirm(void *instance, void *sap,
177 struct qos_info *qos,
178 __u32 max_sdu_size, __u8 max_header_size,
181 struct irda_sock *self;
186 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
195 // Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb);
197 /* How much header space do we need to reserve */
198 self->max_header_size = max_header_size;
200 /* IrTTP max SDU size in transmit direction */
201 self->max_sdu_size_tx = max_sdu_size;
203 /* Find out what the largest chunk of data that we can transmit is */
204 switch (sk->sk_type) {
206 if (max_sdu_size != 0) {
207 IRDA_ERROR("%s: max_sdu_size must be 0\n",
211 self->max_data_size = irttp_get_max_seg_size(self->tsap);
214 if (max_sdu_size == 0) {
215 IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
219 self->max_data_size = max_sdu_size;
222 self->max_data_size = irttp_get_max_seg_size(self->tsap);
225 IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
226 self->max_data_size);
228 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
230 /* We are now connected! */
231 sk->sk_state = TCP_ESTABLISHED;
232 sk->sk_state_change(sk);
236 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
238 * Incoming connection
241 static void irda_connect_indication(void *instance, void *sap,
242 struct qos_info *qos, __u32 max_sdu_size,
243 __u8 max_header_size, struct sk_buff *skb)
245 struct irda_sock *self;
250 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
258 /* How much header space do we need to reserve */
259 self->max_header_size = max_header_size;
261 /* IrTTP max SDU size in transmit direction */
262 self->max_sdu_size_tx = max_sdu_size;
264 /* Find out what the largest chunk of data that we can transmit is */
265 switch (sk->sk_type) {
267 if (max_sdu_size != 0) {
268 IRDA_ERROR("%s: max_sdu_size must be 0\n",
273 self->max_data_size = irttp_get_max_seg_size(self->tsap);
276 if (max_sdu_size == 0) {
277 IRDA_ERROR("%s: 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, "%s(), max_data_size=%d\n", __FUNCTION__,
289 self->max_data_size);
291 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
293 skb_queue_tail(&sk->sk_receive_queue, skb);
294 sk->sk_state_change(sk);
298 * Function irda_connect_response (handle)
300 * Accept incoming connection
303 static void irda_connect_response(struct irda_sock *self)
307 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
309 IRDA_ASSERT(self != NULL, return;);
311 skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
314 IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n",
319 /* Reserve space for MUX_CONTROL and LAP header */
320 skb_reserve(skb, IRDA_MAX_HEADER);
322 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
326 * Function irda_flow_indication (instance, sap, flow)
328 * Used by TinyTP to tell us if it can accept more data or not
331 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
333 struct irda_sock *self;
336 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
340 IRDA_ASSERT(sk != NULL, return;);
344 IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n",
346 self->tx_flow = flow;
349 self->tx_flow = flow;
350 IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n",
352 wake_up_interruptible(sk->sk_sleep);
355 IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __FUNCTION__);
356 /* Unknown flow command, better stop */
357 self->tx_flow = flow;
363 * Function irda_getvalue_confirm (obj_id, value, priv)
365 * Got answer from remote LM-IAS, just pass object to requester...
367 * Note : duplicate from above, but we need our own version that
368 * doesn't touch the dtsap_sel and save the full value structure...
370 static void irda_getvalue_confirm(int result, __u16 obj_id,
371 struct ias_value *value, void *priv)
373 struct irda_sock *self;
375 self = (struct irda_sock *) priv;
377 IRDA_WARNING("%s: lost myself!\n", __FUNCTION__);
381 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
383 /* We probably don't need to make any more queries */
384 iriap_close(self->iriap);
387 /* Check if request succeeded */
388 if (result != IAS_SUCCESS) {
389 IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __FUNCTION__,
392 self->errno = result; /* We really need it later */
394 /* Wake up any processes waiting for result */
395 wake_up_interruptible(&self->query_wait);
400 /* Pass the object to the caller (so the caller must delete it) */
401 self->ias_result = value;
404 /* Wake up any processes waiting for result */
405 wake_up_interruptible(&self->query_wait);
409 * Function irda_selective_discovery_indication (discovery)
411 * Got a selective discovery indication from IrLMP.
413 * IrLMP is telling us that this node is new and matching our hint bit
414 * filter. Wake up any process waiting for answer...
416 static void irda_selective_discovery_indication(discinfo_t *discovery,
420 struct irda_sock *self;
422 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
424 self = (struct irda_sock *) priv;
426 IRDA_WARNING("%s: lost myself!\n", __FUNCTION__);
430 /* Pass parameter to the caller */
431 self->cachedaddr = discovery->daddr;
433 /* Wake up process if its waiting for device to be discovered */
434 wake_up_interruptible(&self->query_wait);
438 * Function irda_discovery_timeout (priv)
440 * Timeout in the selective discovery process
442 * We were waiting for a node to be discovered, but nothing has come up
443 * so far. Wake up the user and tell him that we failed...
445 static void irda_discovery_timeout(u_long priv)
447 struct irda_sock *self;
449 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
451 self = (struct irda_sock *) priv;
452 IRDA_ASSERT(self != NULL, return;);
454 /* Nothing for the caller */
455 self->cachelog = NULL;
456 self->cachedaddr = 0;
457 self->errno = -ETIME;
459 /* Wake up process if its still waiting... */
460 wake_up_interruptible(&self->query_wait);
464 * Function irda_open_tsap (self)
466 * Open local Transport Service Access Point (TSAP)
469 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
474 IRDA_WARNING("%s: busy!\n", __FUNCTION__);
478 /* Initialize callbacks to be used by the IrDA stack */
479 irda_notify_init(¬ify);
480 notify.connect_confirm = irda_connect_confirm;
481 notify.connect_indication = irda_connect_indication;
482 notify.disconnect_indication = irda_disconnect_indication;
483 notify.data_indication = irda_data_indication;
484 notify.udata_indication = irda_data_indication;
485 notify.flow_indication = irda_flow_indication;
486 notify.instance = self;
487 strncpy(notify.name, name, NOTIFY_MAX_NAME);
489 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
491 if (self->tsap == NULL) {
492 IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n",
496 /* Remember which TSAP selector we actually got */
497 self->stsap_sel = self->tsap->stsap_sel;
503 * Function irda_open_lsap (self)
505 * Open local Link Service Access Point (LSAP). Used for opening Ultra
508 #ifdef CONFIG_IRDA_ULTRA
509 static int irda_open_lsap(struct irda_sock *self, int pid)
514 IRDA_WARNING("%s(), busy!\n", __FUNCTION__);
518 /* Initialize callbacks to be used by the IrDA stack */
519 irda_notify_init(¬ify);
520 notify.udata_indication = irda_data_indication;
521 notify.instance = self;
522 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
524 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid);
525 if (self->lsap == NULL) {
526 IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __FUNCTION__);
532 #endif /* CONFIG_IRDA_ULTRA */
535 * Function irda_find_lsap_sel (self, name)
537 * Try to lookup LSAP selector in remote LM-IAS
539 * Basically, we start a IAP query, and then go to sleep. When the query
540 * return, irda_getvalue_confirm will wake us up, and we can examine the
541 * result of the query...
542 * Note that in some case, the query fail even before we go to sleep,
543 * creating some races...
545 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
547 IRDA_DEBUG(2, "%s(%p, %s)\n", __FUNCTION__, self, name);
549 IRDA_ASSERT(self != NULL, return -1;);
552 IRDA_WARNING("%s(): busy with a previous query\n",
557 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
558 irda_getvalue_confirm);
559 if(self->iriap == NULL)
562 /* Treat unexpected wakeup as disconnect */
563 self->errno = -EHOSTUNREACH;
565 /* Query remote LM-IAS */
566 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
567 name, "IrDA:TinyTP:LsapSel");
569 /* Wait for answer, if not yet finished (or failed) */
570 if (wait_event_interruptible(self->query_wait, (self->iriap==NULL)))
571 /* Treat signals as disconnect */
572 return -EHOSTUNREACH;
574 /* Check what happened */
577 /* Requested object/attribute doesn't exist */
578 if((self->errno == IAS_CLASS_UNKNOWN) ||
579 (self->errno == IAS_ATTRIB_UNKNOWN))
580 return (-EADDRNOTAVAIL);
582 return (-EHOSTUNREACH);
585 /* Get the remote TSAP selector */
586 switch (self->ias_result->type) {
588 IRDA_DEBUG(4, "%s() int=%d\n",
589 __FUNCTION__, self->ias_result->t.integer);
591 if (self->ias_result->t.integer != -1)
592 self->dtsap_sel = self->ias_result->t.integer;
598 IRDA_DEBUG(0, "%s(), bad type!\n", __FUNCTION__);
601 if (self->ias_result)
602 irias_delete_value(self->ias_result);
607 return -EADDRNOTAVAIL;
611 * Function irda_discover_daddr_and_lsap_sel (self, name)
613 * This try to find a device with the requested service.
615 * It basically look into the discovery log. For each address in the list,
616 * it queries the LM-IAS of the device to find if this device offer
617 * the requested service.
618 * If there is more than one node supporting the service, we complain
619 * to the user (it should move devices around).
620 * The, we set both the destination address and the lsap selector to point
621 * on the service on the unique device we have found.
623 * Note : this function fails if there is more than one device in range,
624 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
625 * Moreover, we would need to wait the LAP disconnection...
627 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
629 discinfo_t *discoveries; /* Copy of the discovery log */
630 int number; /* Number of nodes in the log */
632 int err = -ENETUNREACH;
633 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
634 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
636 IRDA_DEBUG(2, "%s(), name=%s\n", __FUNCTION__, name);
638 IRDA_ASSERT(self != NULL, return -1;);
640 /* Ask lmp for the current discovery log
641 * Note : we have to use irlmp_get_discoveries(), as opposed
642 * to play with the cachelog directly, because while we are
643 * making our ias query, le log might change... */
644 discoveries = irlmp_get_discoveries(&number, self->mask.word,
646 /* Check if the we got some results */
647 if (discoveries == NULL)
648 return -ENETUNREACH; /* No nodes discovered */
651 * Now, check all discovered devices (if any), and connect
652 * client only about the services that the client is
655 for(i = 0; i < number; i++) {
656 /* Try the address in the log */
657 self->daddr = discoveries[i].daddr;
659 IRDA_DEBUG(1, "%s(), trying daddr = %08x\n",
660 __FUNCTION__, self->daddr);
662 /* Query remote LM-IAS for this service */
663 err = irda_find_lsap_sel(self, name);
666 /* We found the requested service */
667 if(daddr != DEV_ADDR_ANY) {
668 IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
670 self->daddr = DEV_ADDR_ANY;
674 /* First time we found that one, save it ! */
676 dtsap_sel = self->dtsap_sel;
679 /* Requested service simply doesn't exist on this node */
682 /* Something bad did happen :-( */
683 IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __FUNCTION__);
684 self->daddr = DEV_ADDR_ANY;
686 return(-EHOSTUNREACH);
690 /* Cleanup our copy of the discovery log */
693 /* Check out what we found */
694 if(daddr == DEV_ADDR_ANY) {
695 IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
697 self->daddr = DEV_ADDR_ANY;
698 return(-EADDRNOTAVAIL);
701 /* Revert back to discovered device & service */
704 self->dtsap_sel = dtsap_sel;
706 IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
707 __FUNCTION__, name, self->daddr);
713 * Function irda_getname (sock, uaddr, uaddr_len, peer)
715 * Return the our own, or peers socket address (sockaddr_irda)
718 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
719 int *uaddr_len, int peer)
721 struct sockaddr_irda saddr;
722 struct sock *sk = sock->sk;
723 struct irda_sock *self = irda_sk(sk);
726 if (sk->sk_state != TCP_ESTABLISHED)
729 saddr.sir_family = AF_IRDA;
730 saddr.sir_lsap_sel = self->dtsap_sel;
731 saddr.sir_addr = self->daddr;
733 saddr.sir_family = AF_IRDA;
734 saddr.sir_lsap_sel = self->stsap_sel;
735 saddr.sir_addr = self->saddr;
738 IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __FUNCTION__, saddr.sir_lsap_sel);
739 IRDA_DEBUG(1, "%s(), addr = %08x\n", __FUNCTION__, saddr.sir_addr);
741 /* uaddr_len come to us uninitialised */
742 *uaddr_len = sizeof (struct sockaddr_irda);
743 memcpy(uaddr, &saddr, *uaddr_len);
749 * Function irda_listen (sock, backlog)
751 * Just move to the listen state
754 static int irda_listen(struct socket *sock, int backlog)
756 struct sock *sk = sock->sk;
758 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
760 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
761 (sk->sk_type != SOCK_DGRAM))
764 if (sk->sk_state != TCP_LISTEN) {
765 sk->sk_max_ack_backlog = backlog;
766 sk->sk_state = TCP_LISTEN;
775 * Function irda_bind (sock, uaddr, addr_len)
777 * Used by servers to register their well known TSAP
780 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
782 struct sock *sk = sock->sk;
783 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
784 struct irda_sock *self = irda_sk(sk);
787 IRDA_ASSERT(self != NULL, return -1;);
789 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
791 if (addr_len != sizeof(struct sockaddr_irda))
794 #ifdef CONFIG_IRDA_ULTRA
795 /* Special care for Ultra sockets */
796 if ((sk->sk_type == SOCK_DGRAM) &&
797 (sk->sk_protocol == IRDAPROTO_ULTRA)) {
798 self->pid = addr->sir_lsap_sel;
799 if (self->pid & 0x80) {
800 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
803 err = irda_open_lsap(self, self->pid);
807 /* Pretend we are connected */
808 sock->state = SS_CONNECTED;
809 sk->sk_state = TCP_ESTABLISHED;
813 #endif /* CONFIG_IRDA_ULTRA */
815 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
819 /* Register with LM-IAS */
820 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
821 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
822 self->stsap_sel, IAS_KERNEL_ATTR);
823 irias_insert_object(self->ias_obj);
829 * Function irda_accept (sock, newsock, flags)
831 * Wait for incoming connection
834 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
836 struct sock *sk = sock->sk;
837 struct irda_sock *new, *self = irda_sk(sk);
842 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
844 IRDA_ASSERT(self != NULL, return -1;);
846 err = irda_create(newsock, sk->sk_protocol);
850 if (sock->state != SS_UNCONNECTED)
853 if ((sk = sock->sk) == NULL)
856 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
857 (sk->sk_type != SOCK_DGRAM))
860 if (sk->sk_state != TCP_LISTEN)
864 * The read queue this time is holding sockets ready to use
865 * hooked into the SABM we saved
869 * We can perform the accept only if there is incoming data
870 * on the listening socket.
871 * So, we will block the caller until we receive any data.
872 * If the caller was waiting on select() or poll() before
873 * calling us, the data is waiting for us ;-)
876 skb = skb_dequeue(&sk->sk_receive_queue);
879 DECLARE_WAITQUEUE(waitq, current);
881 /* Non blocking operation */
882 if (flags & O_NONBLOCK)
885 /* The following code is a cut'n'paste of the
886 * wait_event_interruptible() macro.
887 * We don't us the macro because the condition has
888 * side effects : we want to make sure that only one
889 * skb get dequeued - Jean II */
890 add_wait_queue(sk->sk_sleep, &waitq);
892 set_current_state(TASK_INTERRUPTIBLE);
893 skb = skb_dequeue(&sk->sk_receive_queue);
896 if (!signal_pending(current)) {
903 current->state = TASK_RUNNING;
904 remove_wait_queue(sk->sk_sleep, &waitq);
910 newsk->sk_state = TCP_ESTABLISHED;
912 new = irda_sk(newsk);
913 IRDA_ASSERT(new != NULL, return -1;);
915 /* Now attach up the new socket */
916 new->tsap = irttp_dup(self->tsap, new);
918 IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
923 new->stsap_sel = new->tsap->stsap_sel;
924 new->dtsap_sel = new->tsap->dtsap_sel;
925 new->saddr = irttp_get_saddr(new->tsap);
926 new->daddr = irttp_get_daddr(new->tsap);
928 new->max_sdu_size_tx = self->max_sdu_size_tx;
929 new->max_sdu_size_rx = self->max_sdu_size_rx;
930 new->max_data_size = self->max_data_size;
931 new->max_header_size = self->max_header_size;
933 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
935 /* Clean up the original one to keep it in listen state */
936 irttp_listen(self->tsap);
938 /* Wow ! What is that ? Jean II */
940 skb->destructor = NULL;
942 sk->sk_ack_backlog--;
944 newsock->state = SS_CONNECTED;
946 irda_connect_response(new);
952 * Function irda_connect (sock, uaddr, addr_len, flags)
954 * Connect to a IrDA device
956 * The main difference with a "standard" connect is that with IrDA we need
957 * to resolve the service name into a TSAP selector (in TCP, port number
958 * doesn't have to be resolved).
959 * Because of this service name resoltion, we can offer "auto-connect",
960 * where we connect to a service without specifying a destination address.
962 * Note : by consulting "errno", the user space caller may learn the cause
963 * of the failure. Most of them are visible in the function, others may come
964 * from subroutines called and are listed here :
965 * o EBUSY : already processing a connect
966 * o EHOSTUNREACH : bad addr->sir_addr argument
967 * o EADDRNOTAVAIL : bad addr->sir_name argument
968 * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
969 * o ENETUNREACH : no node found on the network (auto-connect)
971 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
972 int addr_len, int flags)
974 struct sock *sk = sock->sk;
975 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
976 struct irda_sock *self = irda_sk(sk);
979 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
981 /* Don't allow connect for Ultra sockets */
982 if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA))
983 return -ESOCKTNOSUPPORT;
985 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
986 sock->state = SS_CONNECTED;
987 return 0; /* Connect completed during a ERESTARTSYS event */
990 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
991 sock->state = SS_UNCONNECTED;
992 return -ECONNREFUSED;
995 if (sk->sk_state == TCP_ESTABLISHED)
996 return -EISCONN; /* No reconnect on a seqpacket socket */
998 sk->sk_state = TCP_CLOSE;
999 sock->state = SS_UNCONNECTED;
1001 if (addr_len != sizeof(struct sockaddr_irda))
1004 /* Check if user supplied any destination device address */
1005 if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
1006 /* Try to find one suitable */
1007 err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
1009 IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __FUNCTION__);
1013 /* Use the one provided by the user */
1014 self->daddr = addr->sir_addr;
1015 IRDA_DEBUG(1, "%s(), daddr = %08x\n", __FUNCTION__, self->daddr);
1017 /* If we don't have a valid service name, we assume the
1018 * user want to connect on a specific LSAP. Prevent
1019 * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */
1020 if((addr->sir_name[0] != '\0') ||
1021 (addr->sir_lsap_sel >= 0x70)) {
1022 /* Query remote LM-IAS using service name */
1023 err = irda_find_lsap_sel(self, addr->sir_name);
1025 IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
1029 /* Directly connect to the remote LSAP
1030 * specified by the sir_lsap field.
1031 * Please use with caution, in IrDA LSAPs are
1032 * dynamic and there is no "well-known" LSAP. */
1033 self->dtsap_sel = addr->sir_lsap_sel;
1037 /* Check if we have opened a local TSAP */
1039 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1041 /* Move to connecting socket, start sending Connect Requests */
1042 sock->state = SS_CONNECTING;
1043 sk->sk_state = TCP_SYN_SENT;
1045 /* Connect to remote device */
1046 err = irttp_connect_request(self->tsap, self->dtsap_sel,
1047 self->saddr, self->daddr, NULL,
1048 self->max_sdu_size_rx, NULL);
1050 IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
1055 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1056 return -EINPROGRESS;
1058 if (wait_event_interruptible(*(sk->sk_sleep),
1059 (sk->sk_state != TCP_SYN_SENT)))
1060 return -ERESTARTSYS;
1062 if (sk->sk_state != TCP_ESTABLISHED) {
1063 sock->state = SS_UNCONNECTED;
1064 return sock_error(sk); /* Always set at this point */
1067 sock->state = SS_CONNECTED;
1069 /* At this point, IrLMP has assigned our source address */
1070 self->saddr = irttp_get_saddr(self->tsap);
1075 static struct proto irda_proto = {
1077 .owner = THIS_MODULE,
1078 .obj_size = sizeof(struct irda_sock),
1082 * Function irda_create (sock, protocol)
1084 * Create IrDA socket
1087 static int irda_create(struct socket *sock, int protocol)
1090 struct irda_sock *self;
1092 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1094 /* Check for valid socket type */
1095 switch (sock->type) {
1096 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1097 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1098 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1101 return -ESOCKTNOSUPPORT;
1104 /* Allocate networking socket */
1105 sk = sk_alloc(PF_IRDA, GFP_ATOMIC, &irda_proto, 1);
1110 IRDA_DEBUG(2, "%s() : self is %p\n", __FUNCTION__, self);
1112 init_waitqueue_head(&self->query_wait);
1114 /* Initialise networking socket struct */
1115 sock_init_data(sock, sk); /* Note : set sk->sk_refcnt to 1 */
1116 sk->sk_family = PF_IRDA;
1117 sk->sk_protocol = protocol;
1119 switch (sock->type) {
1121 sock->ops = &irda_stream_ops;
1122 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1124 case SOCK_SEQPACKET:
1125 sock->ops = &irda_seqpacket_ops;
1126 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1130 #ifdef CONFIG_IRDA_ULTRA
1131 case IRDAPROTO_ULTRA:
1132 sock->ops = &irda_ultra_ops;
1133 /* Initialise now, because we may send on unbound
1134 * sockets. Jean II */
1135 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
1136 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
1138 #endif /* CONFIG_IRDA_ULTRA */
1139 case IRDAPROTO_UNITDATA:
1140 sock->ops = &irda_dgram_ops;
1141 /* We let Unitdata conn. be like seqpack conn. */
1142 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1145 IRDA_ERROR("%s: protocol not supported!\n",
1147 return -ESOCKTNOSUPPORT;
1151 return -ESOCKTNOSUPPORT;
1154 /* Register as a client with IrLMP */
1155 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1156 self->mask.word = 0xffff;
1157 self->rx_flow = self->tx_flow = FLOW_START;
1158 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1159 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1160 self->saddr = 0x0; /* so IrLMP assign us any link */
1165 * Function irda_destroy_socket (self)
1170 static void irda_destroy_socket(struct irda_sock *self)
1172 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
1174 IRDA_ASSERT(self != NULL, return;);
1176 /* Unregister with IrLMP */
1177 irlmp_unregister_client(self->ckey);
1178 irlmp_unregister_service(self->skey);
1180 /* Unregister with LM-IAS */
1181 if (self->ias_obj) {
1182 irias_delete_object(self->ias_obj);
1183 self->ias_obj = NULL;
1187 iriap_close(self->iriap);
1192 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1193 irttp_close_tsap(self->tsap);
1196 #ifdef CONFIG_IRDA_ULTRA
1198 irlmp_close_lsap(self->lsap);
1201 #endif /* CONFIG_IRDA_ULTRA */
1205 * Function irda_release (sock)
1207 static int irda_release(struct socket *sock)
1209 struct sock *sk = sock->sk;
1211 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1217 sk->sk_state = TCP_CLOSE;
1218 sk->sk_shutdown |= SEND_SHUTDOWN;
1219 sk->sk_state_change(sk);
1221 /* Destroy IrDA socket */
1222 irda_destroy_socket(irda_sk(sk));
1228 /* Purge queues (see sock_init_data()) */
1229 skb_queue_purge(&sk->sk_receive_queue);
1231 /* Destroy networking socket if we are the last reference on it,
1232 * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */
1235 /* Notes on socket locking and deallocation... - Jean II
1236 * In theory we should put pairs of sock_hold() / sock_put() to
1237 * prevent the socket to be destroyed whenever there is an
1238 * outstanding request or outstanding incoming packet or event.
1240 * 1) This may include IAS request, both in connect and getsockopt.
1241 * Unfortunately, the situation is a bit more messy than it looks,
1242 * because we close iriap and kfree(self) above.
1244 * 2) This may include selective discovery in getsockopt.
1245 * Same stuff as above, irlmp registration and self are gone.
1247 * Probably 1 and 2 may not matter, because it's all triggered
1248 * by a process and the socket layer already prevent the
1249 * socket to go away while a process is holding it, through
1250 * sockfd_put() and fput()...
1252 * 3) This may include deferred TSAP closure. In particular,
1253 * we may receive a late irda_disconnect_indication()
1254 * Fortunately, (tsap_cb *)->close_pend should protect us
1257 * I did some testing on SMP, and it looks solid. And the socket
1258 * memory leak is now gone... - Jean II
1265 * Function irda_sendmsg (iocb, sock, msg, len)
1267 * Send message down to TinyTP. This function is used for both STREAM and
1268 * SEQPACK services. This is possible since it forces the client to
1269 * fragment the message if necessary
1271 static int irda_sendmsg(struct kiocb *iocb, struct socket *sock,
1272 struct msghdr *msg, size_t len)
1274 struct sock *sk = sock->sk;
1275 struct irda_sock *self;
1276 struct sk_buff *skb;
1279 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1281 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1282 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1285 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1286 send_sig(SIGPIPE, current, 0);
1290 if (sk->sk_state != TCP_ESTABLISHED)
1294 IRDA_ASSERT(self != NULL, return -1;);
1296 /* Check if IrTTP is wants us to slow down */
1298 if (wait_event_interruptible(*(sk->sk_sleep),
1299 (self->tx_flow != FLOW_STOP || sk->sk_state != TCP_ESTABLISHED)))
1300 return -ERESTARTSYS;
1302 /* Check if we are still connected */
1303 if (sk->sk_state != TCP_ESTABLISHED)
1306 /* Check that we don't send out too big frames */
1307 if (len > self->max_data_size) {
1308 IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n",
1309 __FUNCTION__, len, self->max_data_size);
1310 len = self->max_data_size;
1313 skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16,
1314 msg->msg_flags & MSG_DONTWAIT, &err);
1318 skb_reserve(skb, self->max_header_size + 16);
1319 skb_reset_transport_header(skb);
1321 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1328 * Just send the message to TinyTP, and let it deal with possible
1329 * errors. No need to duplicate all that here
1331 err = irttp_data_request(self->tsap, skb);
1333 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1336 /* Tell client how much data we actually sent */
1341 * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags)
1343 * Try to receive message and copy it to user. The frame is discarded
1344 * after being read, regardless of how much the user actually read
1346 static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
1347 struct msghdr *msg, size_t size, int flags)
1349 struct sock *sk = sock->sk;
1350 struct irda_sock *self = irda_sk(sk);
1351 struct sk_buff *skb;
1355 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1357 IRDA_ASSERT(self != NULL, return -1;);
1358 IRDA_ASSERT(!sock_error(sk), return -1;);
1360 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1361 flags & MSG_DONTWAIT, &err);
1365 skb_reset_transport_header(skb);
1368 if (copied > size) {
1369 IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n",
1370 __FUNCTION__, copied, size);
1372 msg->msg_flags |= MSG_TRUNC;
1374 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1376 skb_free_datagram(sk, skb);
1379 * Check if we have previously stopped IrTTP and we know
1380 * have more free space in our rx_queue. If so tell IrTTP
1381 * to start delivering frames again before our rx_queue gets
1384 if (self->rx_flow == FLOW_STOP) {
1385 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1386 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
1387 self->rx_flow = FLOW_START;
1388 irttp_flow_request(self->tsap, FLOW_START);
1396 * Function irda_recvmsg_stream (iocb, sock, msg, size, flags)
1398 static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
1399 struct msghdr *msg, size_t size, int flags)
1401 struct sock *sk = sock->sk;
1402 struct irda_sock *self = irda_sk(sk);
1403 int noblock = flags & MSG_DONTWAIT;
1408 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1410 IRDA_ASSERT(self != NULL, return -1;);
1411 IRDA_ASSERT(!sock_error(sk), return -1;);
1413 if (sock->flags & __SO_ACCEPTCON)
1416 if (flags & MSG_OOB)
1419 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
1420 timeo = sock_rcvtimeo(sk, noblock);
1422 msg->msg_namelen = 0;
1426 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1432 if (copied >= target)
1435 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1438 * POSIX 1003.1g mandates this order.
1440 ret = sock_error(sk);
1443 else if (sk->sk_shutdown & RCV_SHUTDOWN)
1447 else if (signal_pending(current))
1448 ret = sock_intr_errno(timeo);
1449 else if (sk->sk_state != TCP_ESTABLISHED)
1451 else if (skb_peek(&sk->sk_receive_queue) == NULL)
1452 /* Wait process until data arrives */
1455 finish_wait(sk->sk_sleep, &wait);
1459 if (sk->sk_shutdown & RCV_SHUTDOWN)
1465 chunk = min_t(unsigned int, skb->len, size);
1466 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1467 skb_queue_head(&sk->sk_receive_queue, skb);
1475 /* Mark read part of skb as used */
1476 if (!(flags & MSG_PEEK)) {
1477 skb_pull(skb, chunk);
1479 /* put the skb back if we didn't use it up.. */
1481 IRDA_DEBUG(1, "%s(), back on q!\n",
1483 skb_queue_head(&sk->sk_receive_queue, skb);
1489 IRDA_DEBUG(0, "%s() questionable!?\n", __FUNCTION__);
1491 /* put message back and return */
1492 skb_queue_head(&sk->sk_receive_queue, skb);
1498 * Check if we have previously stopped IrTTP and we know
1499 * have more free space in our rx_queue. If so tell IrTTP
1500 * to start delivering frames again before our rx_queue gets
1503 if (self->rx_flow == FLOW_STOP) {
1504 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1505 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
1506 self->rx_flow = FLOW_START;
1507 irttp_flow_request(self->tsap, FLOW_START);
1515 * Function irda_sendmsg_dgram (iocb, sock, msg, len)
1517 * Send message down to TinyTP for the unreliable sequenced
1521 static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock,
1522 struct msghdr *msg, size_t len)
1524 struct sock *sk = sock->sk;
1525 struct irda_sock *self;
1526 struct sk_buff *skb;
1529 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1531 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1534 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1535 send_sig(SIGPIPE, current, 0);
1539 if (sk->sk_state != TCP_ESTABLISHED)
1543 IRDA_ASSERT(self != NULL, return -1;);
1546 * Check that we don't send out too big frames. This is an unreliable
1547 * service, so we have no fragmentation and no coalescence
1549 if (len > self->max_data_size) {
1550 IRDA_DEBUG(0, "%s(), Warning to much data! "
1551 "Chopping frame from %zd to %d bytes!\n",
1552 __FUNCTION__, len, self->max_data_size);
1553 len = self->max_data_size;
1556 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1557 msg->msg_flags & MSG_DONTWAIT, &err);
1561 skb_reserve(skb, self->max_header_size);
1562 skb_reset_transport_header(skb);
1564 IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
1566 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1573 * Just send the message to TinyTP, and let it deal with possible
1574 * errors. No need to duplicate all that here
1576 err = irttp_udata_request(self->tsap, skb);
1578 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1585 * Function irda_sendmsg_ultra (iocb, sock, msg, len)
1587 * Send message down to IrLMP for the unreliable Ultra
1590 #ifdef CONFIG_IRDA_ULTRA
1591 static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock,
1592 struct msghdr *msg, size_t len)
1594 struct sock *sk = sock->sk;
1595 struct irda_sock *self;
1598 struct sk_buff *skb;
1601 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1603 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1606 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1607 send_sig(SIGPIPE, current, 0);
1612 IRDA_ASSERT(self != NULL, return -1;);
1614 /* Check if an address was specified with sendto. Jean II */
1615 if (msg->msg_name) {
1616 struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name;
1617 /* Check address, extract pid. Jean II */
1618 if (msg->msg_namelen < sizeof(*addr))
1620 if (addr->sir_family != AF_IRDA)
1623 pid = addr->sir_lsap_sel;
1625 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
1629 /* Check that the socket is properly bound to an Ultra
1631 if ((self->lsap == NULL) ||
1632 (sk->sk_state != TCP_ESTABLISHED)) {
1633 IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n",
1637 /* Use PID from socket */
1642 * Check that we don't send out too big frames. This is an unreliable
1643 * service, so we have no fragmentation and no coalescence
1645 if (len > self->max_data_size) {
1646 IRDA_DEBUG(0, "%s(), Warning to much data! "
1647 "Chopping frame from %zd to %d bytes!\n",
1648 __FUNCTION__, len, self->max_data_size);
1649 len = self->max_data_size;
1652 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1653 msg->msg_flags & MSG_DONTWAIT, &err);
1657 skb_reserve(skb, self->max_header_size);
1658 skb_reset_transport_header(skb);
1660 IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
1662 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1668 err = irlmp_connless_data_request((bound ? self->lsap : NULL),
1671 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1676 #endif /* CONFIG_IRDA_ULTRA */
1679 * Function irda_shutdown (sk, how)
1681 static int irda_shutdown(struct socket *sock, int how)
1683 struct sock *sk = sock->sk;
1684 struct irda_sock *self = irda_sk(sk);
1686 IRDA_ASSERT(self != NULL, return -1;);
1688 IRDA_DEBUG(1, "%s(%p)\n", __FUNCTION__, self);
1690 sk->sk_state = TCP_CLOSE;
1691 sk->sk_shutdown |= SEND_SHUTDOWN;
1692 sk->sk_state_change(sk);
1695 iriap_close(self->iriap);
1700 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1701 irttp_close_tsap(self->tsap);
1705 /* A few cleanup so the socket look as good as new... */
1706 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1707 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1708 self->saddr = 0x0; /* so IrLMP assign us any link */
1714 * Function irda_poll (file, sock, wait)
1716 static unsigned int irda_poll(struct file * file, struct socket *sock,
1719 struct sock *sk = sock->sk;
1720 struct irda_sock *self = irda_sk(sk);
1723 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1725 poll_wait(file, sk->sk_sleep, wait);
1728 /* Exceptional events? */
1731 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1732 IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
1737 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1738 IRDA_DEBUG(4, "Socket is readable\n");
1739 mask |= POLLIN | POLLRDNORM;
1742 /* Connection-based need to check for termination and startup */
1743 switch (sk->sk_type) {
1745 if (sk->sk_state == TCP_CLOSE) {
1746 IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
1750 if (sk->sk_state == TCP_ESTABLISHED) {
1751 if ((self->tx_flow == FLOW_START) &&
1754 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1758 case SOCK_SEQPACKET:
1759 if ((self->tx_flow == FLOW_START) &&
1762 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1766 if (sock_writeable(sk))
1767 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1776 * Function irda_ioctl (sock, cmd, arg)
1778 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1780 struct sock *sk = sock->sk;
1782 IRDA_DEBUG(4, "%s(), cmd=%#x\n", __FUNCTION__, cmd);
1787 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1790 if (put_user(amount, (unsigned int __user *)arg))
1796 struct sk_buff *skb;
1798 /* These two are safe on a single CPU system as only user tasks fiddle here */
1799 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1801 if (put_user(amount, (unsigned int __user *)arg))
1808 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1813 case SIOCGIFDSTADDR:
1814 case SIOCSIFDSTADDR:
1815 case SIOCGIFBRDADDR:
1816 case SIOCSIFBRDADDR:
1817 case SIOCGIFNETMASK:
1818 case SIOCSIFNETMASK:
1823 IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __FUNCTION__);
1824 return -ENOIOCTLCMD;
1831 #ifdef CONFIG_COMPAT
1833 * Function irda_ioctl (sock, cmd, arg)
1835 static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1838 * All IRDA's ioctl are standard ones.
1840 return -ENOIOCTLCMD;
1845 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1847 * Set some options for the socket
1850 static int irda_setsockopt(struct socket *sock, int level, int optname,
1851 char __user *optval, int optlen)
1853 struct sock *sk = sock->sk;
1854 struct irda_sock *self = irda_sk(sk);
1855 struct irda_ias_set *ias_opt;
1856 struct ias_object *ias_obj;
1857 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1860 IRDA_ASSERT(self != NULL, return -1;);
1862 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
1864 if (level != SOL_IRLMP)
1865 return -ENOPROTOOPT;
1869 /* The user want to add an attribute to an existing IAS object
1870 * (in the IAS database) or to create a new object with this
1872 * We first query IAS to know if the object exist, and then
1873 * create the right attribute...
1876 if (optlen != sizeof(struct irda_ias_set))
1879 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1880 if (ias_opt == NULL)
1883 /* Copy query to the driver. */
1884 if (copy_from_user(ias_opt, optval, optlen)) {
1889 /* Find the object we target.
1890 * If the user gives us an empty string, we use the object
1891 * associated with this socket. This will workaround
1892 * duplicated class name - Jean II */
1893 if(ias_opt->irda_class_name[0] == '\0') {
1894 if(self->ias_obj == NULL) {
1898 ias_obj = self->ias_obj;
1900 ias_obj = irias_find_object(ias_opt->irda_class_name);
1902 /* Only ROOT can mess with the global IAS database.
1903 * Users can only add attributes to the object associated
1904 * with the socket they own - Jean II */
1905 if((!capable(CAP_NET_ADMIN)) &&
1906 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
1911 /* If the object doesn't exist, create it */
1912 if(ias_obj == (struct ias_object *) NULL) {
1913 /* Create a new object */
1914 ias_obj = irias_new_object(ias_opt->irda_class_name,
1918 /* Do we have the attribute already ? */
1919 if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1924 /* Look at the type */
1925 switch(ias_opt->irda_attrib_type) {
1927 /* Add an integer attribute */
1928 irias_add_integer_attrib(
1930 ias_opt->irda_attrib_name,
1931 ias_opt->attribute.irda_attrib_int,
1936 if(ias_opt->attribute.irda_attrib_octet_seq.len >
1937 IAS_MAX_OCTET_STRING) {
1941 /* Add an octet sequence attribute */
1942 irias_add_octseq_attrib(
1944 ias_opt->irda_attrib_name,
1945 ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1946 ias_opt->attribute.irda_attrib_octet_seq.len,
1950 /* Should check charset & co */
1952 /* The length is encoded in a __u8, and
1953 * IAS_MAX_STRING == 256, so there is no way
1954 * userspace can pass us a string too large.
1956 /* NULL terminate the string (avoid troubles) */
1957 ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1958 /* Add a string attribute */
1959 irias_add_string_attrib(
1961 ias_opt->irda_attrib_name,
1962 ias_opt->attribute.irda_attrib_string.string,
1969 irias_insert_object(ias_obj);
1973 /* The user want to delete an object from our local IAS
1974 * database. We just need to query the IAS, check is the
1975 * object is not owned by the kernel and delete it.
1978 if (optlen != sizeof(struct irda_ias_set))
1981 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1982 if (ias_opt == NULL)
1985 /* Copy query to the driver. */
1986 if (copy_from_user(ias_opt, optval, optlen)) {
1991 /* Find the object we target.
1992 * If the user gives us an empty string, we use the object
1993 * associated with this socket. This will workaround
1994 * duplicated class name - Jean II */
1995 if(ias_opt->irda_class_name[0] == '\0')
1996 ias_obj = self->ias_obj;
1998 ias_obj = irias_find_object(ias_opt->irda_class_name);
1999 if(ias_obj == (struct ias_object *) NULL) {
2004 /* Only ROOT can mess with the global IAS database.
2005 * Users can only del attributes from the object associated
2006 * with the socket they own - Jean II */
2007 if((!capable(CAP_NET_ADMIN)) &&
2008 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
2013 /* Find the attribute (in the object) we target */
2014 ias_attr = irias_find_attrib(ias_obj,
2015 ias_opt->irda_attrib_name);
2016 if(ias_attr == (struct ias_attrib *) NULL) {
2021 /* Check is the user space own the object */
2022 if(ias_attr->value->owner != IAS_USER_ATTR) {
2023 IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __FUNCTION__);
2028 /* Remove the attribute (and maybe the object) */
2029 irias_delete_attrib(ias_obj, ias_attr, 1);
2032 case IRLMP_MAX_SDU_SIZE:
2033 if (optlen < sizeof(int))
2036 if (get_user(opt, (int __user *)optval))
2039 /* Only possible for a seqpacket service (TTP with SAR) */
2040 if (sk->sk_type != SOCK_SEQPACKET) {
2041 IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n",
2043 self->max_sdu_size_rx = opt;
2045 IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n",
2047 return -ENOPROTOOPT;
2050 case IRLMP_HINTS_SET:
2051 if (optlen < sizeof(int))
2054 /* The input is really a (__u8 hints[2]), easier as an int */
2055 if (get_user(opt, (int __user *)optval))
2058 /* Unregister any old registration */
2060 irlmp_unregister_service(self->skey);
2062 self->skey = irlmp_register_service((__u16) opt);
2064 case IRLMP_HINT_MASK_SET:
2065 /* As opposed to the previous case which set the hint bits
2066 * that we advertise, this one set the filter we use when
2067 * making a discovery (nodes which don't match any hint
2068 * bit in the mask are not reported).
2070 if (optlen < sizeof(int))
2073 /* The input is really a (__u8 hints[2]), easier as an int */
2074 if (get_user(opt, (int __user *)optval))
2077 /* Set the new hint mask */
2078 self->mask.word = (__u16) opt;
2079 /* Mask out extension bits */
2080 self->mask.word &= 0x7f7f;
2081 /* Check if no bits */
2082 if(!self->mask.word)
2083 self->mask.word = 0xFFFF;
2087 return -ENOPROTOOPT;
2093 * Function irda_extract_ias_value(ias_opt, ias_value)
2095 * Translate internal IAS value structure to the user space representation
2097 * The external representation of IAS values, as we exchange them with
2098 * user space program is quite different from the internal representation,
2099 * as stored in the IAS database (because we need a flat structure for
2100 * crossing kernel boundary).
2101 * This function transform the former in the latter. We also check
2102 * that the value type is valid.
2104 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2105 struct ias_value *ias_value)
2107 /* Look at the type */
2108 switch (ias_value->type) {
2110 /* Copy the integer */
2111 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2115 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2117 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2118 ias_value->t.oct_seq, ias_value->len);
2122 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2123 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2125 memcpy(ias_opt->attribute.irda_attrib_string.string,
2126 ias_value->t.string, ias_value->len);
2127 /* NULL terminate the string (avoid troubles) */
2128 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2135 /* Copy type over */
2136 ias_opt->irda_attrib_type = ias_value->type;
2142 * Function irda_getsockopt (sock, level, optname, optval, optlen)
2144 static int irda_getsockopt(struct socket *sock, int level, int optname,
2145 char __user *optval, int __user *optlen)
2147 struct sock *sk = sock->sk;
2148 struct irda_sock *self = irda_sk(sk);
2149 struct irda_device_list list;
2150 struct irda_device_info *discoveries;
2151 struct irda_ias_set * ias_opt; /* IAS get/query params */
2152 struct ias_object * ias_obj; /* Object in IAS */
2153 struct ias_attrib * ias_attr; /* Attribute in IAS object */
2154 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
2160 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
2162 if (level != SOL_IRLMP)
2163 return -ENOPROTOOPT;
2165 if (get_user(len, optlen))
2172 case IRLMP_ENUMDEVICES:
2173 /* Ask lmp for the current discovery log */
2174 discoveries = irlmp_get_discoveries(&list.len, self->mask.word,
2176 /* Check if the we got some results */
2177 if (discoveries == NULL)
2178 return -EAGAIN; /* Didn't find any devices */
2181 /* Write total list length back to client */
2182 if (copy_to_user(optval, &list,
2183 sizeof(struct irda_device_list) -
2184 sizeof(struct irda_device_info)))
2187 /* Offset to first device entry */
2188 offset = sizeof(struct irda_device_list) -
2189 sizeof(struct irda_device_info);
2191 /* Copy the list itself - watch for overflow */
2197 total = offset + (list.len * sizeof(struct irda_device_info));
2200 if (copy_to_user(optval+offset, discoveries, total - offset))
2203 /* Write total number of bytes used back to client */
2204 if (put_user(total, optlen))
2207 /* Free up our buffer */
2212 case IRLMP_MAX_SDU_SIZE:
2213 val = self->max_data_size;
2215 if (put_user(len, optlen))
2218 if (copy_to_user(optval, &val, len))
2222 /* The user want an object from our local IAS database.
2223 * We just need to query the IAS and return the value
2226 /* Check that the user has allocated the right space for us */
2227 if (len != sizeof(struct irda_ias_set))
2230 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2231 if (ias_opt == NULL)
2234 /* Copy query to the driver. */
2235 if (copy_from_user(ias_opt, optval, len)) {
2240 /* Find the object we target.
2241 * If the user gives us an empty string, we use the object
2242 * associated with this socket. This will workaround
2243 * duplicated class name - Jean II */
2244 if(ias_opt->irda_class_name[0] == '\0')
2245 ias_obj = self->ias_obj;
2247 ias_obj = irias_find_object(ias_opt->irda_class_name);
2248 if(ias_obj == (struct ias_object *) NULL) {
2253 /* Find the attribute (in the object) we target */
2254 ias_attr = irias_find_attrib(ias_obj,
2255 ias_opt->irda_attrib_name);
2256 if(ias_attr == (struct ias_attrib *) NULL) {
2261 /* Translate from internal to user structure */
2262 err = irda_extract_ias_value(ias_opt, ias_attr->value);
2268 /* Copy reply to the user */
2269 if (copy_to_user(optval, ias_opt,
2270 sizeof(struct irda_ias_set))) {
2274 /* Note : don't need to put optlen, we checked it */
2277 case IRLMP_IAS_QUERY:
2278 /* The user want an object from a remote IAS database.
2279 * We need to use IAP to query the remote database and
2280 * then wait for the answer to come back. */
2282 /* Check that the user has allocated the right space for us */
2283 if (len != sizeof(struct irda_ias_set))
2286 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2287 if (ias_opt == NULL)
2290 /* Copy query to the driver. */
2291 if (copy_from_user(ias_opt, optval, len)) {
2296 /* At this point, there are two cases...
2297 * 1) the socket is connected - that's the easy case, we
2298 * just query the device we are connected to...
2299 * 2) the socket is not connected - the user doesn't want
2300 * to connect and/or may not have a valid service name
2301 * (so can't create a fake connection). In this case,
2302 * we assume that the user pass us a valid destination
2303 * address in the requesting structure...
2305 if(self->daddr != DEV_ADDR_ANY) {
2306 /* We are connected - reuse known daddr */
2307 daddr = self->daddr;
2309 /* We are not connected, we must specify a valid
2310 * destination address */
2311 daddr = ias_opt->daddr;
2312 if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2318 /* Check that we can proceed with IAP */
2320 IRDA_WARNING("%s: busy with a previous query\n",
2326 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2327 irda_getvalue_confirm);
2329 if (self->iriap == NULL) {
2334 /* Treat unexpected wakeup as disconnect */
2335 self->errno = -EHOSTUNREACH;
2337 /* Query remote LM-IAS */
2338 iriap_getvaluebyclass_request(self->iriap,
2340 ias_opt->irda_class_name,
2341 ias_opt->irda_attrib_name);
2343 /* Wait for answer, if not yet finished (or failed) */
2344 if (wait_event_interruptible(self->query_wait,
2345 (self->iriap == NULL))) {
2346 /* pending request uses copy of ias_opt-content
2347 * we can free it regardless! */
2349 /* Treat signals as disconnect */
2350 return -EHOSTUNREACH;
2353 /* Check what happened */
2357 /* Requested object/attribute doesn't exist */
2358 if((self->errno == IAS_CLASS_UNKNOWN) ||
2359 (self->errno == IAS_ATTRIB_UNKNOWN))
2360 return (-EADDRNOTAVAIL);
2362 return (-EHOSTUNREACH);
2365 /* Translate from internal to user structure */
2366 err = irda_extract_ias_value(ias_opt, self->ias_result);
2367 if (self->ias_result)
2368 irias_delete_value(self->ias_result);
2374 /* Copy reply to the user */
2375 if (copy_to_user(optval, ias_opt,
2376 sizeof(struct irda_ias_set))) {
2380 /* Note : don't need to put optlen, we checked it */
2383 case IRLMP_WAITDEVICE:
2384 /* This function is just another way of seeing life ;-)
2385 * IRLMP_ENUMDEVICES assumes that you have a static network,
2386 * and that you just want to pick one of the devices present.
2387 * On the other hand, in here we assume that no device is
2388 * present and that at some point in the future a device will
2389 * come into range. When this device arrive, we just wake
2390 * up the caller, so that he has time to connect to it before
2391 * the device goes away...
2392 * Note : once the node has been discovered for more than a
2393 * few second, it won't trigger this function, unless it
2394 * goes away and come back changes its hint bits (so we
2395 * might call it IRLMP_WAITNEWDEVICE).
2398 /* Check that the user is passing us an int */
2399 if (len != sizeof(int))
2401 /* Get timeout in ms (max time we block the caller) */
2402 if (get_user(val, (int __user *)optval))
2405 /* Tell IrLMP we want to be notified */
2406 irlmp_update_client(self->ckey, self->mask.word,
2407 irda_selective_discovery_indication,
2408 NULL, (void *) self);
2410 /* Do some discovery (and also return cached results) */
2411 irlmp_discovery_request(self->nslots);
2413 /* Wait until a node is discovered */
2414 if (!self->cachedaddr) {
2417 IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __FUNCTION__);
2419 /* Set watchdog timer to expire in <val> ms. */
2421 init_timer(&self->watchdog);
2422 self->watchdog.function = irda_discovery_timeout;
2423 self->watchdog.data = (unsigned long) self;
2424 self->watchdog.expires = jiffies + (val * HZ/1000);
2425 add_timer(&(self->watchdog));
2427 /* Wait for IR-LMP to call us back */
2428 __wait_event_interruptible(self->query_wait,
2429 (self->cachedaddr != 0 || self->errno == -ETIME),
2432 /* If watchdog is still activated, kill it! */
2433 if(timer_pending(&(self->watchdog)))
2434 del_timer(&(self->watchdog));
2436 IRDA_DEBUG(1, "%s(), ...waking up !\n", __FUNCTION__);
2442 IRDA_DEBUG(1, "%s(), found immediately !\n",
2445 /* Tell IrLMP that we have been notified */
2446 irlmp_update_client(self->ckey, self->mask.word,
2449 /* Check if the we got some results */
2450 if (!self->cachedaddr)
2451 return -EAGAIN; /* Didn't find any devices */
2452 daddr = self->cachedaddr;
2454 self->cachedaddr = 0;
2456 /* We return the daddr of the device that trigger the
2457 * wakeup. As irlmp pass us only the new devices, we
2458 * are sure that it's not an old device.
2459 * If the user want more details, he should query
2460 * the whole discovery log and pick one device...
2462 if (put_user(daddr, (int __user *)optval))
2467 return -ENOPROTOOPT;
2473 static struct net_proto_family irda_family_ops = {
2475 .create = irda_create,
2476 .owner = THIS_MODULE,
2479 static const struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2481 .owner = THIS_MODULE,
2482 .release = irda_release,
2484 .connect = irda_connect,
2485 .socketpair = sock_no_socketpair,
2486 .accept = irda_accept,
2487 .getname = irda_getname,
2489 .ioctl = irda_ioctl,
2490 #ifdef CONFIG_COMPAT
2491 .compat_ioctl = irda_compat_ioctl,
2493 .listen = irda_listen,
2494 .shutdown = irda_shutdown,
2495 .setsockopt = irda_setsockopt,
2496 .getsockopt = irda_getsockopt,
2497 .sendmsg = irda_sendmsg,
2498 .recvmsg = irda_recvmsg_stream,
2499 .mmap = sock_no_mmap,
2500 .sendpage = sock_no_sendpage,
2503 static const struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2505 .owner = THIS_MODULE,
2506 .release = irda_release,
2508 .connect = irda_connect,
2509 .socketpair = sock_no_socketpair,
2510 .accept = irda_accept,
2511 .getname = irda_getname,
2512 .poll = datagram_poll,
2513 .ioctl = irda_ioctl,
2514 #ifdef CONFIG_COMPAT
2515 .compat_ioctl = irda_compat_ioctl,
2517 .listen = irda_listen,
2518 .shutdown = irda_shutdown,
2519 .setsockopt = irda_setsockopt,
2520 .getsockopt = irda_getsockopt,
2521 .sendmsg = irda_sendmsg,
2522 .recvmsg = irda_recvmsg_dgram,
2523 .mmap = sock_no_mmap,
2524 .sendpage = sock_no_sendpage,
2527 static const struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2529 .owner = THIS_MODULE,
2530 .release = irda_release,
2532 .connect = irda_connect,
2533 .socketpair = sock_no_socketpair,
2534 .accept = irda_accept,
2535 .getname = irda_getname,
2536 .poll = datagram_poll,
2537 .ioctl = irda_ioctl,
2538 #ifdef CONFIG_COMPAT
2539 .compat_ioctl = irda_compat_ioctl,
2541 .listen = irda_listen,
2542 .shutdown = irda_shutdown,
2543 .setsockopt = irda_setsockopt,
2544 .getsockopt = irda_getsockopt,
2545 .sendmsg = irda_sendmsg_dgram,
2546 .recvmsg = irda_recvmsg_dgram,
2547 .mmap = sock_no_mmap,
2548 .sendpage = sock_no_sendpage,
2551 #ifdef CONFIG_IRDA_ULTRA
2552 static const struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2554 .owner = THIS_MODULE,
2555 .release = irda_release,
2557 .connect = sock_no_connect,
2558 .socketpair = sock_no_socketpair,
2559 .accept = sock_no_accept,
2560 .getname = irda_getname,
2561 .poll = datagram_poll,
2562 .ioctl = irda_ioctl,
2563 #ifdef CONFIG_COMPAT
2564 .compat_ioctl = irda_compat_ioctl,
2566 .listen = sock_no_listen,
2567 .shutdown = irda_shutdown,
2568 .setsockopt = irda_setsockopt,
2569 .getsockopt = irda_getsockopt,
2570 .sendmsg = irda_sendmsg_ultra,
2571 .recvmsg = irda_recvmsg_dgram,
2572 .mmap = sock_no_mmap,
2573 .sendpage = sock_no_sendpage,
2575 #endif /* CONFIG_IRDA_ULTRA */
2577 #include <linux/smp_lock.h>
2578 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2579 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2580 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2581 #ifdef CONFIG_IRDA_ULTRA
2582 SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2583 #endif /* CONFIG_IRDA_ULTRA */
2586 * Function irsock_init (pro)
2588 * Initialize IrDA protocol
2591 int __init irsock_init(void)
2593 int rc = proto_register(&irda_proto, 0);
2596 rc = sock_register(&irda_family_ops);
2602 * Function irsock_cleanup (void)
2604 * Remove IrDA protocol
2607 void __exit irsock_cleanup(void)
2609 sock_unregister(PF_IRDA);
2610 proto_unregister(&irda_proto);