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 net *net, 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__);
93 err = sock_queue_rcv_skb(sk, skb);
95 IRDA_DEBUG(1, "%s(), error: no more mem!\n", __FUNCTION__);
96 self->rx_flow = FLOW_STOP;
98 /* When we return error, TTP will need to requeue the skb */
106 * Function irda_disconnect_indication (instance, sap, reason, skb)
108 * Connection has been closed. Check reason to find out why
111 static void irda_disconnect_indication(void *instance, void *sap,
112 LM_REASON reason, struct sk_buff *skb)
114 struct irda_sock *self;
119 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
121 /* Don't care about it, but let's not leak it */
127 IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n",
132 /* Prevent race conditions with irda_release() and irda_shutdown() */
134 if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) {
135 sk->sk_state = TCP_CLOSE;
136 sk->sk_shutdown |= SEND_SHUTDOWN;
138 sk->sk_state_change(sk);
141 * If we leave it open, IrLMP put it back into the list of
142 * unconnected LSAPs. The problem is that any incoming request
143 * can then be matched to this socket (and it will be, because
144 * it is at the head of the list). This would prevent any
145 * listening socket waiting on the same TSAP to get those
146 * requests. Some apps forget to close sockets, or hang to it
147 * a bit too long, so we may stay in this dead state long
148 * enough to be noticed...
149 * Note : all socket function do check sk->sk_state, so we are
154 irttp_close_tsap(self->tsap);
160 /* Note : once we are there, there is not much you want to do
161 * with the socket anymore, apart from closing it.
162 * For example, bind() and connect() won't reset sk->sk_err,
163 * sk->sk_shutdown and sk->sk_flags to valid values...
169 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
171 * Connections has been confirmed by the remote device
174 static void irda_connect_confirm(void *instance, void *sap,
175 struct qos_info *qos,
176 __u32 max_sdu_size, __u8 max_header_size,
179 struct irda_sock *self;
184 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
193 // Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb);
195 /* How much header space do we need to reserve */
196 self->max_header_size = max_header_size;
198 /* IrTTP max SDU size in transmit direction */
199 self->max_sdu_size_tx = max_sdu_size;
201 /* Find out what the largest chunk of data that we can transmit is */
202 switch (sk->sk_type) {
204 if (max_sdu_size != 0) {
205 IRDA_ERROR("%s: max_sdu_size must be 0\n",
209 self->max_data_size = irttp_get_max_seg_size(self->tsap);
212 if (max_sdu_size == 0) {
213 IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
217 self->max_data_size = max_sdu_size;
220 self->max_data_size = irttp_get_max_seg_size(self->tsap);
223 IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
224 self->max_data_size);
226 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
228 /* We are now connected! */
229 sk->sk_state = TCP_ESTABLISHED;
230 sk->sk_state_change(sk);
234 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
236 * Incoming connection
239 static void irda_connect_indication(void *instance, void *sap,
240 struct qos_info *qos, __u32 max_sdu_size,
241 __u8 max_header_size, struct sk_buff *skb)
243 struct irda_sock *self;
248 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
256 /* How much header space do we need to reserve */
257 self->max_header_size = max_header_size;
259 /* IrTTP max SDU size in transmit direction */
260 self->max_sdu_size_tx = max_sdu_size;
262 /* Find out what the largest chunk of data that we can transmit is */
263 switch (sk->sk_type) {
265 if (max_sdu_size != 0) {
266 IRDA_ERROR("%s: max_sdu_size must be 0\n",
271 self->max_data_size = irttp_get_max_seg_size(self->tsap);
274 if (max_sdu_size == 0) {
275 IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
280 self->max_data_size = max_sdu_size;
283 self->max_data_size = irttp_get_max_seg_size(self->tsap);
286 IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __FUNCTION__,
287 self->max_data_size);
289 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
291 skb_queue_tail(&sk->sk_receive_queue, skb);
292 sk->sk_state_change(sk);
296 * Function irda_connect_response (handle)
298 * Accept incoming connection
301 static void irda_connect_response(struct irda_sock *self)
305 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
307 skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
310 IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n",
315 /* Reserve space for MUX_CONTROL and LAP header */
316 skb_reserve(skb, IRDA_MAX_HEADER);
318 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
322 * Function irda_flow_indication (instance, sap, flow)
324 * Used by TinyTP to tell us if it can accept more data or not
327 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
329 struct irda_sock *self;
332 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
340 IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n",
342 self->tx_flow = flow;
345 self->tx_flow = flow;
346 IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n",
348 wake_up_interruptible(sk->sk_sleep);
351 IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __FUNCTION__);
352 /* Unknown flow command, better stop */
353 self->tx_flow = flow;
359 * Function irda_getvalue_confirm (obj_id, value, priv)
361 * Got answer from remote LM-IAS, just pass object to requester...
363 * Note : duplicate from above, but we need our own version that
364 * doesn't touch the dtsap_sel and save the full value structure...
366 static void irda_getvalue_confirm(int result, __u16 obj_id,
367 struct ias_value *value, void *priv)
369 struct irda_sock *self;
371 self = (struct irda_sock *) priv;
373 IRDA_WARNING("%s: lost myself!\n", __FUNCTION__);
377 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
379 /* We probably don't need to make any more queries */
380 iriap_close(self->iriap);
383 /* Check if request succeeded */
384 if (result != IAS_SUCCESS) {
385 IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __FUNCTION__,
388 self->errno = result; /* We really need it later */
390 /* Wake up any processes waiting for result */
391 wake_up_interruptible(&self->query_wait);
396 /* Pass the object to the caller (so the caller must delete it) */
397 self->ias_result = value;
400 /* Wake up any processes waiting for result */
401 wake_up_interruptible(&self->query_wait);
405 * Function irda_selective_discovery_indication (discovery)
407 * Got a selective discovery indication from IrLMP.
409 * IrLMP is telling us that this node is new and matching our hint bit
410 * filter. Wake up any process waiting for answer...
412 static void irda_selective_discovery_indication(discinfo_t *discovery,
416 struct irda_sock *self;
418 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
420 self = (struct irda_sock *) priv;
422 IRDA_WARNING("%s: lost myself!\n", __FUNCTION__);
426 /* Pass parameter to the caller */
427 self->cachedaddr = discovery->daddr;
429 /* Wake up process if its waiting for device to be discovered */
430 wake_up_interruptible(&self->query_wait);
434 * Function irda_discovery_timeout (priv)
436 * Timeout in the selective discovery process
438 * We were waiting for a node to be discovered, but nothing has come up
439 * so far. Wake up the user and tell him that we failed...
441 static void irda_discovery_timeout(u_long priv)
443 struct irda_sock *self;
445 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
447 self = (struct irda_sock *) priv;
448 BUG_ON(self == NULL);
450 /* Nothing for the caller */
451 self->cachelog = NULL;
452 self->cachedaddr = 0;
453 self->errno = -ETIME;
455 /* Wake up process if its still waiting... */
456 wake_up_interruptible(&self->query_wait);
460 * Function irda_open_tsap (self)
462 * Open local Transport Service Access Point (TSAP)
465 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
470 IRDA_WARNING("%s: busy!\n", __FUNCTION__);
474 /* Initialize callbacks to be used by the IrDA stack */
475 irda_notify_init(¬ify);
476 notify.connect_confirm = irda_connect_confirm;
477 notify.connect_indication = irda_connect_indication;
478 notify.disconnect_indication = irda_disconnect_indication;
479 notify.data_indication = irda_data_indication;
480 notify.udata_indication = irda_data_indication;
481 notify.flow_indication = irda_flow_indication;
482 notify.instance = self;
483 strncpy(notify.name, name, NOTIFY_MAX_NAME);
485 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
487 if (self->tsap == NULL) {
488 IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n",
492 /* Remember which TSAP selector we actually got */
493 self->stsap_sel = self->tsap->stsap_sel;
499 * Function irda_open_lsap (self)
501 * Open local Link Service Access Point (LSAP). Used for opening Ultra
504 #ifdef CONFIG_IRDA_ULTRA
505 static int irda_open_lsap(struct irda_sock *self, int pid)
510 IRDA_WARNING("%s(), busy!\n", __FUNCTION__);
514 /* Initialize callbacks to be used by the IrDA stack */
515 irda_notify_init(¬ify);
516 notify.udata_indication = irda_data_indication;
517 notify.instance = self;
518 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
520 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid);
521 if (self->lsap == NULL) {
522 IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __FUNCTION__);
528 #endif /* CONFIG_IRDA_ULTRA */
531 * Function irda_find_lsap_sel (self, name)
533 * Try to lookup LSAP selector in remote LM-IAS
535 * Basically, we start a IAP query, and then go to sleep. When the query
536 * return, irda_getvalue_confirm will wake us up, and we can examine the
537 * result of the query...
538 * Note that in some case, the query fail even before we go to sleep,
539 * creating some races...
541 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
543 IRDA_DEBUG(2, "%s(%p, %s)\n", __FUNCTION__, self, name);
546 IRDA_WARNING("%s(): busy with a previous query\n",
551 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
552 irda_getvalue_confirm);
553 if(self->iriap == NULL)
556 /* Treat unexpected wakeup as disconnect */
557 self->errno = -EHOSTUNREACH;
559 /* Query remote LM-IAS */
560 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
561 name, "IrDA:TinyTP:LsapSel");
563 /* Wait for answer, if not yet finished (or failed) */
564 if (wait_event_interruptible(self->query_wait, (self->iriap==NULL)))
565 /* Treat signals as disconnect */
566 return -EHOSTUNREACH;
568 /* Check what happened */
571 /* Requested object/attribute doesn't exist */
572 if((self->errno == IAS_CLASS_UNKNOWN) ||
573 (self->errno == IAS_ATTRIB_UNKNOWN))
574 return (-EADDRNOTAVAIL);
576 return (-EHOSTUNREACH);
579 /* Get the remote TSAP selector */
580 switch (self->ias_result->type) {
582 IRDA_DEBUG(4, "%s() int=%d\n",
583 __FUNCTION__, self->ias_result->t.integer);
585 if (self->ias_result->t.integer != -1)
586 self->dtsap_sel = self->ias_result->t.integer;
592 IRDA_DEBUG(0, "%s(), bad type!\n", __FUNCTION__);
595 if (self->ias_result)
596 irias_delete_value(self->ias_result);
601 return -EADDRNOTAVAIL;
605 * Function irda_discover_daddr_and_lsap_sel (self, name)
607 * This try to find a device with the requested service.
609 * It basically look into the discovery log. For each address in the list,
610 * it queries the LM-IAS of the device to find if this device offer
611 * the requested service.
612 * If there is more than one node supporting the service, we complain
613 * to the user (it should move devices around).
614 * The, we set both the destination address and the lsap selector to point
615 * on the service on the unique device we have found.
617 * Note : this function fails if there is more than one device in range,
618 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
619 * Moreover, we would need to wait the LAP disconnection...
621 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
623 discinfo_t *discoveries; /* Copy of the discovery log */
624 int number; /* Number of nodes in the log */
626 int err = -ENETUNREACH;
627 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
628 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
630 IRDA_DEBUG(2, "%s(), name=%s\n", __FUNCTION__, name);
632 /* Ask lmp for the current discovery log
633 * Note : we have to use irlmp_get_discoveries(), as opposed
634 * to play with the cachelog directly, because while we are
635 * making our ias query, le log might change... */
636 discoveries = irlmp_get_discoveries(&number, self->mask.word,
638 /* Check if the we got some results */
639 if (discoveries == NULL)
640 return -ENETUNREACH; /* No nodes discovered */
643 * Now, check all discovered devices (if any), and connect
644 * client only about the services that the client is
647 for(i = 0; i < number; i++) {
648 /* Try the address in the log */
649 self->daddr = discoveries[i].daddr;
651 IRDA_DEBUG(1, "%s(), trying daddr = %08x\n",
652 __FUNCTION__, self->daddr);
654 /* Query remote LM-IAS for this service */
655 err = irda_find_lsap_sel(self, name);
658 /* We found the requested service */
659 if(daddr != DEV_ADDR_ANY) {
660 IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
662 self->daddr = DEV_ADDR_ANY;
666 /* First time we found that one, save it ! */
668 dtsap_sel = self->dtsap_sel;
671 /* Requested service simply doesn't exist on this node */
674 /* Something bad did happen :-( */
675 IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __FUNCTION__);
676 self->daddr = DEV_ADDR_ANY;
678 return(-EHOSTUNREACH);
682 /* Cleanup our copy of the discovery log */
685 /* Check out what we found */
686 if(daddr == DEV_ADDR_ANY) {
687 IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
689 self->daddr = DEV_ADDR_ANY;
690 return(-EADDRNOTAVAIL);
693 /* Revert back to discovered device & service */
696 self->dtsap_sel = dtsap_sel;
698 IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
699 __FUNCTION__, name, self->daddr);
705 * Function irda_getname (sock, uaddr, uaddr_len, peer)
707 * Return the our own, or peers socket address (sockaddr_irda)
710 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
711 int *uaddr_len, int peer)
713 struct sockaddr_irda saddr;
714 struct sock *sk = sock->sk;
715 struct irda_sock *self = irda_sk(sk);
718 if (sk->sk_state != TCP_ESTABLISHED)
721 saddr.sir_family = AF_IRDA;
722 saddr.sir_lsap_sel = self->dtsap_sel;
723 saddr.sir_addr = self->daddr;
725 saddr.sir_family = AF_IRDA;
726 saddr.sir_lsap_sel = self->stsap_sel;
727 saddr.sir_addr = self->saddr;
730 IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __FUNCTION__, saddr.sir_lsap_sel);
731 IRDA_DEBUG(1, "%s(), addr = %08x\n", __FUNCTION__, saddr.sir_addr);
733 /* uaddr_len come to us uninitialised */
734 *uaddr_len = sizeof (struct sockaddr_irda);
735 memcpy(uaddr, &saddr, *uaddr_len);
741 * Function irda_listen (sock, backlog)
743 * Just move to the listen state
746 static int irda_listen(struct socket *sock, int backlog)
748 struct sock *sk = sock->sk;
750 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
752 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
753 (sk->sk_type != SOCK_DGRAM))
756 if (sk->sk_state != TCP_LISTEN) {
757 sk->sk_max_ack_backlog = backlog;
758 sk->sk_state = TCP_LISTEN;
767 * Function irda_bind (sock, uaddr, addr_len)
769 * Used by servers to register their well known TSAP
772 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
774 struct sock *sk = sock->sk;
775 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
776 struct irda_sock *self = irda_sk(sk);
779 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
781 if (addr_len != sizeof(struct sockaddr_irda))
784 #ifdef CONFIG_IRDA_ULTRA
785 /* Special care for Ultra sockets */
786 if ((sk->sk_type == SOCK_DGRAM) &&
787 (sk->sk_protocol == IRDAPROTO_ULTRA)) {
788 self->pid = addr->sir_lsap_sel;
789 if (self->pid & 0x80) {
790 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
793 err = irda_open_lsap(self, self->pid);
797 /* Pretend we are connected */
798 sock->state = SS_CONNECTED;
799 sk->sk_state = TCP_ESTABLISHED;
803 #endif /* CONFIG_IRDA_ULTRA */
805 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
806 if (self->ias_obj == NULL)
809 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
811 kfree(self->ias_obj->name);
812 kfree(self->ias_obj);
816 /* Register with LM-IAS */
817 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
818 self->stsap_sel, IAS_KERNEL_ATTR);
819 irias_insert_object(self->ias_obj);
825 * Function irda_accept (sock, newsock, flags)
827 * Wait for incoming connection
830 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
832 struct sock *sk = sock->sk;
833 struct irda_sock *new, *self = irda_sk(sk);
838 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
840 err = irda_create(sk->sk_net, newsock, sk->sk_protocol);
844 if (sock->state != SS_UNCONNECTED)
847 if ((sk = sock->sk) == NULL)
850 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
851 (sk->sk_type != SOCK_DGRAM))
854 if (sk->sk_state != TCP_LISTEN)
858 * The read queue this time is holding sockets ready to use
859 * hooked into the SABM we saved
863 * We can perform the accept only if there is incoming data
864 * on the listening socket.
865 * So, we will block the caller until we receive any data.
866 * If the caller was waiting on select() or poll() before
867 * calling us, the data is waiting for us ;-)
871 skb = skb_dequeue(&sk->sk_receive_queue);
875 /* Non blocking operation */
876 if (flags & O_NONBLOCK)
879 err = wait_event_interruptible(*(sk->sk_sleep),
880 skb_peek(&sk->sk_receive_queue));
889 newsk->sk_state = TCP_ESTABLISHED;
891 new = irda_sk(newsk);
893 /* Now attach up the new socket */
894 new->tsap = irttp_dup(self->tsap, new);
896 IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
901 new->stsap_sel = new->tsap->stsap_sel;
902 new->dtsap_sel = new->tsap->dtsap_sel;
903 new->saddr = irttp_get_saddr(new->tsap);
904 new->daddr = irttp_get_daddr(new->tsap);
906 new->max_sdu_size_tx = self->max_sdu_size_tx;
907 new->max_sdu_size_rx = self->max_sdu_size_rx;
908 new->max_data_size = self->max_data_size;
909 new->max_header_size = self->max_header_size;
911 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
913 /* Clean up the original one to keep it in listen state */
914 irttp_listen(self->tsap);
916 /* Wow ! What is that ? Jean II */
918 skb->destructor = NULL;
920 sk->sk_ack_backlog--;
922 newsock->state = SS_CONNECTED;
924 irda_connect_response(new);
930 * Function irda_connect (sock, uaddr, addr_len, flags)
932 * Connect to a IrDA device
934 * The main difference with a "standard" connect is that with IrDA we need
935 * to resolve the service name into a TSAP selector (in TCP, port number
936 * doesn't have to be resolved).
937 * Because of this service name resoltion, we can offer "auto-connect",
938 * where we connect to a service without specifying a destination address.
940 * Note : by consulting "errno", the user space caller may learn the cause
941 * of the failure. Most of them are visible in the function, others may come
942 * from subroutines called and are listed here :
943 * o EBUSY : already processing a connect
944 * o EHOSTUNREACH : bad addr->sir_addr argument
945 * o EADDRNOTAVAIL : bad addr->sir_name argument
946 * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
947 * o ENETUNREACH : no node found on the network (auto-connect)
949 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
950 int addr_len, int flags)
952 struct sock *sk = sock->sk;
953 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
954 struct irda_sock *self = irda_sk(sk);
957 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
959 /* Don't allow connect for Ultra sockets */
960 if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA))
961 return -ESOCKTNOSUPPORT;
963 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
964 sock->state = SS_CONNECTED;
965 return 0; /* Connect completed during a ERESTARTSYS event */
968 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
969 sock->state = SS_UNCONNECTED;
970 return -ECONNREFUSED;
973 if (sk->sk_state == TCP_ESTABLISHED)
974 return -EISCONN; /* No reconnect on a seqpacket socket */
976 sk->sk_state = TCP_CLOSE;
977 sock->state = SS_UNCONNECTED;
979 if (addr_len != sizeof(struct sockaddr_irda))
982 /* Check if user supplied any destination device address */
983 if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
984 /* Try to find one suitable */
985 err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
987 IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __FUNCTION__);
991 /* Use the one provided by the user */
992 self->daddr = addr->sir_addr;
993 IRDA_DEBUG(1, "%s(), daddr = %08x\n", __FUNCTION__, self->daddr);
995 /* If we don't have a valid service name, we assume the
996 * user want to connect on a specific LSAP. Prevent
997 * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */
998 if((addr->sir_name[0] != '\0') ||
999 (addr->sir_lsap_sel >= 0x70)) {
1000 /* Query remote LM-IAS using service name */
1001 err = irda_find_lsap_sel(self, addr->sir_name);
1003 IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
1007 /* Directly connect to the remote LSAP
1008 * specified by the sir_lsap field.
1009 * Please use with caution, in IrDA LSAPs are
1010 * dynamic and there is no "well-known" LSAP. */
1011 self->dtsap_sel = addr->sir_lsap_sel;
1015 /* Check if we have opened a local TSAP */
1017 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1019 /* Move to connecting socket, start sending Connect Requests */
1020 sock->state = SS_CONNECTING;
1021 sk->sk_state = TCP_SYN_SENT;
1023 /* Connect to remote device */
1024 err = irttp_connect_request(self->tsap, self->dtsap_sel,
1025 self->saddr, self->daddr, NULL,
1026 self->max_sdu_size_rx, NULL);
1028 IRDA_DEBUG(0, "%s(), connect failed!\n", __FUNCTION__);
1033 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1034 return -EINPROGRESS;
1036 if (wait_event_interruptible(*(sk->sk_sleep),
1037 (sk->sk_state != TCP_SYN_SENT)))
1038 return -ERESTARTSYS;
1040 if (sk->sk_state != TCP_ESTABLISHED) {
1041 sock->state = SS_UNCONNECTED;
1042 err = sock_error(sk);
1043 return err? err : -ECONNRESET;
1046 sock->state = SS_CONNECTED;
1048 /* At this point, IrLMP has assigned our source address */
1049 self->saddr = irttp_get_saddr(self->tsap);
1054 static struct proto irda_proto = {
1056 .owner = THIS_MODULE,
1057 .obj_size = sizeof(struct irda_sock),
1061 * Function irda_create (sock, protocol)
1063 * Create IrDA socket
1066 static int irda_create(struct net *net, struct socket *sock, int protocol)
1069 struct irda_sock *self;
1071 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1073 if (net != &init_net)
1074 return -EAFNOSUPPORT;
1076 /* Check for valid socket type */
1077 switch (sock->type) {
1078 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1079 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1080 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1083 return -ESOCKTNOSUPPORT;
1086 /* Allocate networking socket */
1087 sk = sk_alloc(net, PF_IRDA, GFP_ATOMIC, &irda_proto);
1092 IRDA_DEBUG(2, "%s() : self is %p\n", __FUNCTION__, self);
1094 init_waitqueue_head(&self->query_wait);
1096 /* Initialise networking socket struct */
1097 sock_init_data(sock, sk); /* Note : set sk->sk_refcnt to 1 */
1098 sk->sk_family = PF_IRDA;
1099 sk->sk_protocol = protocol;
1101 switch (sock->type) {
1103 sock->ops = &irda_stream_ops;
1104 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1106 case SOCK_SEQPACKET:
1107 sock->ops = &irda_seqpacket_ops;
1108 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1112 #ifdef CONFIG_IRDA_ULTRA
1113 case IRDAPROTO_ULTRA:
1114 sock->ops = &irda_ultra_ops;
1115 /* Initialise now, because we may send on unbound
1116 * sockets. Jean II */
1117 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
1118 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
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 return -ESOCKTNOSUPPORT;
1131 return -ESOCKTNOSUPPORT;
1134 /* Register as a client with IrLMP */
1135 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1136 self->mask.word = 0xffff;
1137 self->rx_flow = self->tx_flow = FLOW_START;
1138 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1139 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1140 self->saddr = 0x0; /* so IrLMP assign us any link */
1145 * Function irda_destroy_socket (self)
1150 static void irda_destroy_socket(struct irda_sock *self)
1152 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
1154 /* Unregister with IrLMP */
1155 irlmp_unregister_client(self->ckey);
1156 irlmp_unregister_service(self->skey);
1158 /* Unregister with LM-IAS */
1159 if (self->ias_obj) {
1160 irias_delete_object(self->ias_obj);
1161 self->ias_obj = NULL;
1165 iriap_close(self->iriap);
1170 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1171 irttp_close_tsap(self->tsap);
1174 #ifdef CONFIG_IRDA_ULTRA
1176 irlmp_close_lsap(self->lsap);
1179 #endif /* CONFIG_IRDA_ULTRA */
1183 * Function irda_release (sock)
1185 static int irda_release(struct socket *sock)
1187 struct sock *sk = sock->sk;
1189 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1195 sk->sk_state = TCP_CLOSE;
1196 sk->sk_shutdown |= SEND_SHUTDOWN;
1197 sk->sk_state_change(sk);
1199 /* Destroy IrDA socket */
1200 irda_destroy_socket(irda_sk(sk));
1206 /* Purge queues (see sock_init_data()) */
1207 skb_queue_purge(&sk->sk_receive_queue);
1209 /* Destroy networking socket if we are the last reference on it,
1210 * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */
1213 /* Notes on socket locking and deallocation... - Jean II
1214 * In theory we should put pairs of sock_hold() / sock_put() to
1215 * prevent the socket to be destroyed whenever there is an
1216 * outstanding request or outstanding incoming packet or event.
1218 * 1) This may include IAS request, both in connect and getsockopt.
1219 * Unfortunately, the situation is a bit more messy than it looks,
1220 * because we close iriap and kfree(self) above.
1222 * 2) This may include selective discovery in getsockopt.
1223 * Same stuff as above, irlmp registration and self are gone.
1225 * Probably 1 and 2 may not matter, because it's all triggered
1226 * by a process and the socket layer already prevent the
1227 * socket to go away while a process is holding it, through
1228 * sockfd_put() and fput()...
1230 * 3) This may include deferred TSAP closure. In particular,
1231 * we may receive a late irda_disconnect_indication()
1232 * Fortunately, (tsap_cb *)->close_pend should protect us
1235 * I did some testing on SMP, and it looks solid. And the socket
1236 * memory leak is now gone... - Jean II
1243 * Function irda_sendmsg (iocb, sock, msg, len)
1245 * Send message down to TinyTP. This function is used for both STREAM and
1246 * SEQPACK services. This is possible since it forces the client to
1247 * fragment the message if necessary
1249 static int irda_sendmsg(struct kiocb *iocb, struct socket *sock,
1250 struct msghdr *msg, size_t len)
1252 struct sock *sk = sock->sk;
1253 struct irda_sock *self;
1254 struct sk_buff *skb;
1257 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1259 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1260 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR | MSG_CMSG_COMPAT |
1264 if (sk->sk_shutdown & SEND_SHUTDOWN)
1267 if (sk->sk_state != TCP_ESTABLISHED)
1272 /* Check if IrTTP is wants us to slow down */
1274 if (wait_event_interruptible(*(sk->sk_sleep),
1275 (self->tx_flow != FLOW_STOP || sk->sk_state != TCP_ESTABLISHED)))
1276 return -ERESTARTSYS;
1278 /* Check if we are still connected */
1279 if (sk->sk_state != TCP_ESTABLISHED)
1282 /* Check that we don't send out too big frames */
1283 if (len > self->max_data_size) {
1284 IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n",
1285 __FUNCTION__, len, self->max_data_size);
1286 len = self->max_data_size;
1289 skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16,
1290 msg->msg_flags & MSG_DONTWAIT, &err);
1294 skb_reserve(skb, self->max_header_size + 16);
1295 skb_reset_transport_header(skb);
1297 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1304 * Just send the message to TinyTP, and let it deal with possible
1305 * errors. No need to duplicate all that here
1307 err = irttp_data_request(self->tsap, skb);
1309 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1312 /* Tell client how much data we actually sent */
1316 return sk_stream_error(sk, msg->msg_flags, err);
1321 * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags)
1323 * Try to receive message and copy it to user. The frame is discarded
1324 * after being read, regardless of how much the user actually read
1326 static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
1327 struct msghdr *msg, size_t size, int flags)
1329 struct sock *sk = sock->sk;
1330 struct irda_sock *self = irda_sk(sk);
1331 struct sk_buff *skb;
1335 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1337 if ((err = sock_error(sk)) < 0)
1340 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1341 flags & MSG_DONTWAIT, &err);
1345 skb_reset_transport_header(skb);
1348 if (copied > size) {
1349 IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n",
1350 __FUNCTION__, copied, size);
1352 msg->msg_flags |= MSG_TRUNC;
1354 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1356 skb_free_datagram(sk, skb);
1359 * Check if we have previously stopped IrTTP and we know
1360 * have more free space in our rx_queue. If so tell IrTTP
1361 * to start delivering frames again before our rx_queue gets
1364 if (self->rx_flow == FLOW_STOP) {
1365 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1366 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
1367 self->rx_flow = FLOW_START;
1368 irttp_flow_request(self->tsap, FLOW_START);
1376 * Function irda_recvmsg_stream (iocb, sock, msg, size, flags)
1378 static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
1379 struct msghdr *msg, size_t size, int flags)
1381 struct sock *sk = sock->sk;
1382 struct irda_sock *self = irda_sk(sk);
1383 int noblock = flags & MSG_DONTWAIT;
1388 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1390 if ((err = sock_error(sk)) < 0)
1393 if (sock->flags & __SO_ACCEPTCON)
1396 if (flags & MSG_OOB)
1399 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
1400 timeo = sock_rcvtimeo(sk, noblock);
1402 msg->msg_namelen = 0;
1406 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1412 if (copied >= target)
1415 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1418 * POSIX 1003.1g mandates this order.
1420 ret = sock_error(sk);
1423 else if (sk->sk_shutdown & RCV_SHUTDOWN)
1427 else if (signal_pending(current))
1428 ret = sock_intr_errno(timeo);
1429 else if (sk->sk_state != TCP_ESTABLISHED)
1431 else if (skb_peek(&sk->sk_receive_queue) == NULL)
1432 /* Wait process until data arrives */
1435 finish_wait(sk->sk_sleep, &wait);
1439 if (sk->sk_shutdown & RCV_SHUTDOWN)
1445 chunk = min_t(unsigned int, skb->len, size);
1446 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1447 skb_queue_head(&sk->sk_receive_queue, skb);
1455 /* Mark read part of skb as used */
1456 if (!(flags & MSG_PEEK)) {
1457 skb_pull(skb, chunk);
1459 /* put the skb back if we didn't use it up.. */
1461 IRDA_DEBUG(1, "%s(), back on q!\n",
1463 skb_queue_head(&sk->sk_receive_queue, skb);
1469 IRDA_DEBUG(0, "%s() questionable!?\n", __FUNCTION__);
1471 /* put message back and return */
1472 skb_queue_head(&sk->sk_receive_queue, skb);
1478 * Check if we have previously stopped IrTTP and we know
1479 * have more free space in our rx_queue. If so tell IrTTP
1480 * to start delivering frames again before our rx_queue gets
1483 if (self->rx_flow == FLOW_STOP) {
1484 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1485 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __FUNCTION__);
1486 self->rx_flow = FLOW_START;
1487 irttp_flow_request(self->tsap, FLOW_START);
1495 * Function irda_sendmsg_dgram (iocb, sock, msg, len)
1497 * Send message down to TinyTP for the unreliable sequenced
1501 static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock,
1502 struct msghdr *msg, size_t len)
1504 struct sock *sk = sock->sk;
1505 struct irda_sock *self;
1506 struct sk_buff *skb;
1509 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1511 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1514 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1515 send_sig(SIGPIPE, current, 0);
1519 if (sk->sk_state != TCP_ESTABLISHED)
1525 * Check that we don't send out too big frames. This is an unreliable
1526 * service, so we have no fragmentation and no coalescence
1528 if (len > self->max_data_size) {
1529 IRDA_DEBUG(0, "%s(), Warning to much data! "
1530 "Chopping frame from %zd to %d bytes!\n",
1531 __FUNCTION__, len, self->max_data_size);
1532 len = self->max_data_size;
1535 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1536 msg->msg_flags & MSG_DONTWAIT, &err);
1540 skb_reserve(skb, self->max_header_size);
1541 skb_reset_transport_header(skb);
1543 IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
1545 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1552 * Just send the message to TinyTP, and let it deal with possible
1553 * errors. No need to duplicate all that here
1555 err = irttp_udata_request(self->tsap, skb);
1557 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1564 * Function irda_sendmsg_ultra (iocb, sock, msg, len)
1566 * Send message down to IrLMP for the unreliable Ultra
1569 #ifdef CONFIG_IRDA_ULTRA
1570 static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock,
1571 struct msghdr *msg, size_t len)
1573 struct sock *sk = sock->sk;
1574 struct irda_sock *self;
1577 struct sk_buff *skb;
1580 IRDA_DEBUG(4, "%s(), len=%zd\n", __FUNCTION__, len);
1582 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1585 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1586 send_sig(SIGPIPE, current, 0);
1592 /* Check if an address was specified with sendto. Jean II */
1593 if (msg->msg_name) {
1594 struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name;
1595 /* Check address, extract pid. Jean II */
1596 if (msg->msg_namelen < sizeof(*addr))
1598 if (addr->sir_family != AF_IRDA)
1601 pid = addr->sir_lsap_sel;
1603 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __FUNCTION__);
1607 /* Check that the socket is properly bound to an Ultra
1609 if ((self->lsap == NULL) ||
1610 (sk->sk_state != TCP_ESTABLISHED)) {
1611 IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n",
1615 /* Use PID from socket */
1620 * Check that we don't send out too big frames. This is an unreliable
1621 * service, so we have no fragmentation and no coalescence
1623 if (len > self->max_data_size) {
1624 IRDA_DEBUG(0, "%s(), Warning to much data! "
1625 "Chopping frame from %zd to %d bytes!\n",
1626 __FUNCTION__, len, self->max_data_size);
1627 len = self->max_data_size;
1630 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1631 msg->msg_flags & MSG_DONTWAIT, &err);
1635 skb_reserve(skb, self->max_header_size);
1636 skb_reset_transport_header(skb);
1638 IRDA_DEBUG(4, "%s(), appending user data\n", __FUNCTION__);
1640 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1646 err = irlmp_connless_data_request((bound ? self->lsap : NULL),
1649 IRDA_DEBUG(0, "%s(), err=%d\n", __FUNCTION__, err);
1654 #endif /* CONFIG_IRDA_ULTRA */
1657 * Function irda_shutdown (sk, how)
1659 static int irda_shutdown(struct socket *sock, int how)
1661 struct sock *sk = sock->sk;
1662 struct irda_sock *self = irda_sk(sk);
1664 IRDA_DEBUG(1, "%s(%p)\n", __FUNCTION__, self);
1666 sk->sk_state = TCP_CLOSE;
1667 sk->sk_shutdown |= SEND_SHUTDOWN;
1668 sk->sk_state_change(sk);
1671 iriap_close(self->iriap);
1676 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1677 irttp_close_tsap(self->tsap);
1681 /* A few cleanup so the socket look as good as new... */
1682 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1683 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1684 self->saddr = 0x0; /* so IrLMP assign us any link */
1690 * Function irda_poll (file, sock, wait)
1692 static unsigned int irda_poll(struct file * file, struct socket *sock,
1695 struct sock *sk = sock->sk;
1696 struct irda_sock *self = irda_sk(sk);
1699 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1701 poll_wait(file, sk->sk_sleep, wait);
1704 /* Exceptional events? */
1707 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1708 IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
1713 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1714 IRDA_DEBUG(4, "Socket is readable\n");
1715 mask |= POLLIN | POLLRDNORM;
1718 /* Connection-based need to check for termination and startup */
1719 switch (sk->sk_type) {
1721 if (sk->sk_state == TCP_CLOSE) {
1722 IRDA_DEBUG(0, "%s(), POLLHUP\n", __FUNCTION__);
1726 if (sk->sk_state == TCP_ESTABLISHED) {
1727 if ((self->tx_flow == FLOW_START) &&
1730 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1734 case SOCK_SEQPACKET:
1735 if ((self->tx_flow == FLOW_START) &&
1738 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1742 if (sock_writeable(sk))
1743 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1752 * Function irda_ioctl (sock, cmd, arg)
1754 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1756 struct sock *sk = sock->sk;
1758 IRDA_DEBUG(4, "%s(), cmd=%#x\n", __FUNCTION__, cmd);
1763 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1766 if (put_user(amount, (unsigned int __user *)arg))
1772 struct sk_buff *skb;
1774 /* These two are safe on a single CPU system as only user tasks fiddle here */
1775 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1777 if (put_user(amount, (unsigned int __user *)arg))
1784 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1789 case SIOCGIFDSTADDR:
1790 case SIOCSIFDSTADDR:
1791 case SIOCGIFBRDADDR:
1792 case SIOCSIFBRDADDR:
1793 case SIOCGIFNETMASK:
1794 case SIOCSIFNETMASK:
1799 IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __FUNCTION__);
1800 return -ENOIOCTLCMD;
1807 #ifdef CONFIG_COMPAT
1809 * Function irda_ioctl (sock, cmd, arg)
1811 static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1814 * All IRDA's ioctl are standard ones.
1816 return -ENOIOCTLCMD;
1821 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1823 * Set some options for the socket
1826 static int irda_setsockopt(struct socket *sock, int level, int optname,
1827 char __user *optval, int optlen)
1829 struct sock *sk = sock->sk;
1830 struct irda_sock *self = irda_sk(sk);
1831 struct irda_ias_set *ias_opt;
1832 struct ias_object *ias_obj;
1833 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1834 int opt, free_ias = 0;
1836 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
1838 if (level != SOL_IRLMP)
1839 return -ENOPROTOOPT;
1843 /* The user want to add an attribute to an existing IAS object
1844 * (in the IAS database) or to create a new object with this
1846 * We first query IAS to know if the object exist, and then
1847 * create the right attribute...
1850 if (optlen != sizeof(struct irda_ias_set))
1853 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1854 if (ias_opt == NULL)
1857 /* Copy query to the driver. */
1858 if (copy_from_user(ias_opt, optval, optlen)) {
1863 /* Find the object we target.
1864 * If the user gives us an empty string, we use the object
1865 * associated with this socket. This will workaround
1866 * duplicated class name - Jean II */
1867 if(ias_opt->irda_class_name[0] == '\0') {
1868 if(self->ias_obj == NULL) {
1872 ias_obj = self->ias_obj;
1874 ias_obj = irias_find_object(ias_opt->irda_class_name);
1876 /* Only ROOT can mess with the global IAS database.
1877 * Users can only add attributes to the object associated
1878 * with the socket they own - Jean II */
1879 if((!capable(CAP_NET_ADMIN)) &&
1880 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
1885 /* If the object doesn't exist, create it */
1886 if(ias_obj == (struct ias_object *) NULL) {
1887 /* Create a new object */
1888 ias_obj = irias_new_object(ias_opt->irda_class_name,
1890 if (ias_obj == NULL) {
1897 /* Do we have the attribute already ? */
1898 if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1901 kfree(ias_obj->name);
1907 /* Look at the type */
1908 switch(ias_opt->irda_attrib_type) {
1910 /* Add an integer attribute */
1911 irias_add_integer_attrib(
1913 ias_opt->irda_attrib_name,
1914 ias_opt->attribute.irda_attrib_int,
1919 if(ias_opt->attribute.irda_attrib_octet_seq.len >
1920 IAS_MAX_OCTET_STRING) {
1923 kfree(ias_obj->name);
1929 /* Add an octet sequence attribute */
1930 irias_add_octseq_attrib(
1932 ias_opt->irda_attrib_name,
1933 ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1934 ias_opt->attribute.irda_attrib_octet_seq.len,
1938 /* Should check charset & co */
1940 /* The length is encoded in a __u8, and
1941 * IAS_MAX_STRING == 256, so there is no way
1942 * userspace can pass us a string too large.
1944 /* NULL terminate the string (avoid troubles) */
1945 ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1946 /* Add a string attribute */
1947 irias_add_string_attrib(
1949 ias_opt->irda_attrib_name,
1950 ias_opt->attribute.irda_attrib_string.string,
1956 kfree(ias_obj->name);
1961 irias_insert_object(ias_obj);
1965 /* The user want to delete an object from our local IAS
1966 * database. We just need to query the IAS, check is the
1967 * object is not owned by the kernel and delete it.
1970 if (optlen != sizeof(struct irda_ias_set))
1973 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1974 if (ias_opt == NULL)
1977 /* Copy query to the driver. */
1978 if (copy_from_user(ias_opt, optval, optlen)) {
1983 /* Find the object we target.
1984 * If the user gives us an empty string, we use the object
1985 * associated with this socket. This will workaround
1986 * duplicated class name - Jean II */
1987 if(ias_opt->irda_class_name[0] == '\0')
1988 ias_obj = self->ias_obj;
1990 ias_obj = irias_find_object(ias_opt->irda_class_name);
1991 if(ias_obj == (struct ias_object *) NULL) {
1996 /* Only ROOT can mess with the global IAS database.
1997 * Users can only del attributes from the object associated
1998 * with the socket they own - Jean II */
1999 if((!capable(CAP_NET_ADMIN)) &&
2000 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
2005 /* Find the attribute (in the object) we target */
2006 ias_attr = irias_find_attrib(ias_obj,
2007 ias_opt->irda_attrib_name);
2008 if(ias_attr == (struct ias_attrib *) NULL) {
2013 /* Check is the user space own the object */
2014 if(ias_attr->value->owner != IAS_USER_ATTR) {
2015 IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __FUNCTION__);
2020 /* Remove the attribute (and maybe the object) */
2021 irias_delete_attrib(ias_obj, ias_attr, 1);
2024 case IRLMP_MAX_SDU_SIZE:
2025 if (optlen < sizeof(int))
2028 if (get_user(opt, (int __user *)optval))
2031 /* Only possible for a seqpacket service (TTP with SAR) */
2032 if (sk->sk_type != SOCK_SEQPACKET) {
2033 IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n",
2035 self->max_sdu_size_rx = opt;
2037 IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n",
2039 return -ENOPROTOOPT;
2042 case IRLMP_HINTS_SET:
2043 if (optlen < sizeof(int))
2046 /* The input is really a (__u8 hints[2]), easier as an int */
2047 if (get_user(opt, (int __user *)optval))
2050 /* Unregister any old registration */
2052 irlmp_unregister_service(self->skey);
2054 self->skey = irlmp_register_service((__u16) opt);
2056 case IRLMP_HINT_MASK_SET:
2057 /* As opposed to the previous case which set the hint bits
2058 * that we advertise, this one set the filter we use when
2059 * making a discovery (nodes which don't match any hint
2060 * bit in the mask are not reported).
2062 if (optlen < sizeof(int))
2065 /* The input is really a (__u8 hints[2]), easier as an int */
2066 if (get_user(opt, (int __user *)optval))
2069 /* Set the new hint mask */
2070 self->mask.word = (__u16) opt;
2071 /* Mask out extension bits */
2072 self->mask.word &= 0x7f7f;
2073 /* Check if no bits */
2074 if(!self->mask.word)
2075 self->mask.word = 0xFFFF;
2079 return -ENOPROTOOPT;
2085 * Function irda_extract_ias_value(ias_opt, ias_value)
2087 * Translate internal IAS value structure to the user space representation
2089 * The external representation of IAS values, as we exchange them with
2090 * user space program is quite different from the internal representation,
2091 * as stored in the IAS database (because we need a flat structure for
2092 * crossing kernel boundary).
2093 * This function transform the former in the latter. We also check
2094 * that the value type is valid.
2096 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2097 struct ias_value *ias_value)
2099 /* Look at the type */
2100 switch (ias_value->type) {
2102 /* Copy the integer */
2103 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2107 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2109 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2110 ias_value->t.oct_seq, ias_value->len);
2114 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2115 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2117 memcpy(ias_opt->attribute.irda_attrib_string.string,
2118 ias_value->t.string, ias_value->len);
2119 /* NULL terminate the string (avoid troubles) */
2120 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2127 /* Copy type over */
2128 ias_opt->irda_attrib_type = ias_value->type;
2134 * Function irda_getsockopt (sock, level, optname, optval, optlen)
2136 static int irda_getsockopt(struct socket *sock, int level, int optname,
2137 char __user *optval, int __user *optlen)
2139 struct sock *sk = sock->sk;
2140 struct irda_sock *self = irda_sk(sk);
2141 struct irda_device_list list;
2142 struct irda_device_info *discoveries;
2143 struct irda_ias_set * ias_opt; /* IAS get/query params */
2144 struct ias_object * ias_obj; /* Object in IAS */
2145 struct ias_attrib * ias_attr; /* Attribute in IAS object */
2146 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
2152 IRDA_DEBUG(2, "%s(%p)\n", __FUNCTION__, self);
2154 if (level != SOL_IRLMP)
2155 return -ENOPROTOOPT;
2157 if (get_user(len, optlen))
2164 case IRLMP_ENUMDEVICES:
2165 /* Ask lmp for the current discovery log */
2166 discoveries = irlmp_get_discoveries(&list.len, self->mask.word,
2168 /* Check if the we got some results */
2169 if (discoveries == NULL)
2170 return -EAGAIN; /* Didn't find any devices */
2173 /* Write total list length back to client */
2174 if (copy_to_user(optval, &list,
2175 sizeof(struct irda_device_list) -
2176 sizeof(struct irda_device_info)))
2179 /* Offset to first device entry */
2180 offset = sizeof(struct irda_device_list) -
2181 sizeof(struct irda_device_info);
2183 /* Copy the list itself - watch for overflow */
2189 total = offset + (list.len * sizeof(struct irda_device_info));
2192 if (copy_to_user(optval+offset, discoveries, total - offset))
2195 /* Write total number of bytes used back to client */
2196 if (put_user(total, optlen))
2199 /* Free up our buffer */
2204 case IRLMP_MAX_SDU_SIZE:
2205 val = self->max_data_size;
2207 if (put_user(len, optlen))
2210 if (copy_to_user(optval, &val, len))
2214 /* The user want an object from our local IAS database.
2215 * We just need to query the IAS and return the value
2218 /* Check that the user has allocated the right space for us */
2219 if (len != sizeof(struct irda_ias_set))
2222 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2223 if (ias_opt == NULL)
2226 /* Copy query to the driver. */
2227 if (copy_from_user(ias_opt, optval, len)) {
2232 /* Find the object we target.
2233 * If the user gives us an empty string, we use the object
2234 * associated with this socket. This will workaround
2235 * duplicated class name - Jean II */
2236 if(ias_opt->irda_class_name[0] == '\0')
2237 ias_obj = self->ias_obj;
2239 ias_obj = irias_find_object(ias_opt->irda_class_name);
2240 if(ias_obj == (struct ias_object *) NULL) {
2245 /* Find the attribute (in the object) we target */
2246 ias_attr = irias_find_attrib(ias_obj,
2247 ias_opt->irda_attrib_name);
2248 if(ias_attr == (struct ias_attrib *) NULL) {
2253 /* Translate from internal to user structure */
2254 err = irda_extract_ias_value(ias_opt, ias_attr->value);
2260 /* Copy reply to the user */
2261 if (copy_to_user(optval, ias_opt,
2262 sizeof(struct irda_ias_set))) {
2266 /* Note : don't need to put optlen, we checked it */
2269 case IRLMP_IAS_QUERY:
2270 /* The user want an object from a remote IAS database.
2271 * We need to use IAP to query the remote database and
2272 * then wait for the answer to come back. */
2274 /* Check that the user has allocated the right space for us */
2275 if (len != sizeof(struct irda_ias_set))
2278 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2279 if (ias_opt == NULL)
2282 /* Copy query to the driver. */
2283 if (copy_from_user(ias_opt, optval, len)) {
2288 /* At this point, there are two cases...
2289 * 1) the socket is connected - that's the easy case, we
2290 * just query the device we are connected to...
2291 * 2) the socket is not connected - the user doesn't want
2292 * to connect and/or may not have a valid service name
2293 * (so can't create a fake connection). In this case,
2294 * we assume that the user pass us a valid destination
2295 * address in the requesting structure...
2297 if(self->daddr != DEV_ADDR_ANY) {
2298 /* We are connected - reuse known daddr */
2299 daddr = self->daddr;
2301 /* We are not connected, we must specify a valid
2302 * destination address */
2303 daddr = ias_opt->daddr;
2304 if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2310 /* Check that we can proceed with IAP */
2312 IRDA_WARNING("%s: busy with a previous query\n",
2318 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2319 irda_getvalue_confirm);
2321 if (self->iriap == NULL) {
2326 /* Treat unexpected wakeup as disconnect */
2327 self->errno = -EHOSTUNREACH;
2329 /* Query remote LM-IAS */
2330 iriap_getvaluebyclass_request(self->iriap,
2332 ias_opt->irda_class_name,
2333 ias_opt->irda_attrib_name);
2335 /* Wait for answer, if not yet finished (or failed) */
2336 if (wait_event_interruptible(self->query_wait,
2337 (self->iriap == NULL))) {
2338 /* pending request uses copy of ias_opt-content
2339 * we can free it regardless! */
2341 /* Treat signals as disconnect */
2342 return -EHOSTUNREACH;
2345 /* Check what happened */
2349 /* Requested object/attribute doesn't exist */
2350 if((self->errno == IAS_CLASS_UNKNOWN) ||
2351 (self->errno == IAS_ATTRIB_UNKNOWN))
2352 return (-EADDRNOTAVAIL);
2354 return (-EHOSTUNREACH);
2357 /* Translate from internal to user structure */
2358 err = irda_extract_ias_value(ias_opt, self->ias_result);
2359 if (self->ias_result)
2360 irias_delete_value(self->ias_result);
2366 /* Copy reply to the user */
2367 if (copy_to_user(optval, ias_opt,
2368 sizeof(struct irda_ias_set))) {
2372 /* Note : don't need to put optlen, we checked it */
2375 case IRLMP_WAITDEVICE:
2376 /* This function is just another way of seeing life ;-)
2377 * IRLMP_ENUMDEVICES assumes that you have a static network,
2378 * and that you just want to pick one of the devices present.
2379 * On the other hand, in here we assume that no device is
2380 * present and that at some point in the future a device will
2381 * come into range. When this device arrive, we just wake
2382 * up the caller, so that he has time to connect to it before
2383 * the device goes away...
2384 * Note : once the node has been discovered for more than a
2385 * few second, it won't trigger this function, unless it
2386 * goes away and come back changes its hint bits (so we
2387 * might call it IRLMP_WAITNEWDEVICE).
2390 /* Check that the user is passing us an int */
2391 if (len != sizeof(int))
2393 /* Get timeout in ms (max time we block the caller) */
2394 if (get_user(val, (int __user *)optval))
2397 /* Tell IrLMP we want to be notified */
2398 irlmp_update_client(self->ckey, self->mask.word,
2399 irda_selective_discovery_indication,
2400 NULL, (void *) self);
2402 /* Do some discovery (and also return cached results) */
2403 irlmp_discovery_request(self->nslots);
2405 /* Wait until a node is discovered */
2406 if (!self->cachedaddr) {
2409 IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __FUNCTION__);
2411 /* Set watchdog timer to expire in <val> ms. */
2413 init_timer(&self->watchdog);
2414 self->watchdog.function = irda_discovery_timeout;
2415 self->watchdog.data = (unsigned long) self;
2416 self->watchdog.expires = jiffies + (val * HZ/1000);
2417 add_timer(&(self->watchdog));
2419 /* Wait for IR-LMP to call us back */
2420 __wait_event_interruptible(self->query_wait,
2421 (self->cachedaddr != 0 || self->errno == -ETIME),
2424 /* If watchdog is still activated, kill it! */
2425 if(timer_pending(&(self->watchdog)))
2426 del_timer(&(self->watchdog));
2428 IRDA_DEBUG(1, "%s(), ...waking up !\n", __FUNCTION__);
2434 IRDA_DEBUG(1, "%s(), found immediately !\n",
2437 /* Tell IrLMP that we have been notified */
2438 irlmp_update_client(self->ckey, self->mask.word,
2441 /* Check if the we got some results */
2442 if (!self->cachedaddr)
2443 return -EAGAIN; /* Didn't find any devices */
2444 daddr = self->cachedaddr;
2446 self->cachedaddr = 0;
2448 /* We return the daddr of the device that trigger the
2449 * wakeup. As irlmp pass us only the new devices, we
2450 * are sure that it's not an old device.
2451 * If the user want more details, he should query
2452 * the whole discovery log and pick one device...
2454 if (put_user(daddr, (int __user *)optval))
2459 return -ENOPROTOOPT;
2465 static struct net_proto_family irda_family_ops = {
2467 .create = irda_create,
2468 .owner = THIS_MODULE,
2471 static const struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2473 .owner = THIS_MODULE,
2474 .release = irda_release,
2476 .connect = irda_connect,
2477 .socketpair = sock_no_socketpair,
2478 .accept = irda_accept,
2479 .getname = irda_getname,
2481 .ioctl = irda_ioctl,
2482 #ifdef CONFIG_COMPAT
2483 .compat_ioctl = irda_compat_ioctl,
2485 .listen = irda_listen,
2486 .shutdown = irda_shutdown,
2487 .setsockopt = irda_setsockopt,
2488 .getsockopt = irda_getsockopt,
2489 .sendmsg = irda_sendmsg,
2490 .recvmsg = irda_recvmsg_stream,
2491 .mmap = sock_no_mmap,
2492 .sendpage = sock_no_sendpage,
2495 static const struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2497 .owner = THIS_MODULE,
2498 .release = irda_release,
2500 .connect = irda_connect,
2501 .socketpair = sock_no_socketpair,
2502 .accept = irda_accept,
2503 .getname = irda_getname,
2504 .poll = datagram_poll,
2505 .ioctl = irda_ioctl,
2506 #ifdef CONFIG_COMPAT
2507 .compat_ioctl = irda_compat_ioctl,
2509 .listen = irda_listen,
2510 .shutdown = irda_shutdown,
2511 .setsockopt = irda_setsockopt,
2512 .getsockopt = irda_getsockopt,
2513 .sendmsg = irda_sendmsg,
2514 .recvmsg = irda_recvmsg_dgram,
2515 .mmap = sock_no_mmap,
2516 .sendpage = sock_no_sendpage,
2519 static const struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2521 .owner = THIS_MODULE,
2522 .release = irda_release,
2524 .connect = irda_connect,
2525 .socketpair = sock_no_socketpair,
2526 .accept = irda_accept,
2527 .getname = irda_getname,
2528 .poll = datagram_poll,
2529 .ioctl = irda_ioctl,
2530 #ifdef CONFIG_COMPAT
2531 .compat_ioctl = irda_compat_ioctl,
2533 .listen = irda_listen,
2534 .shutdown = irda_shutdown,
2535 .setsockopt = irda_setsockopt,
2536 .getsockopt = irda_getsockopt,
2537 .sendmsg = irda_sendmsg_dgram,
2538 .recvmsg = irda_recvmsg_dgram,
2539 .mmap = sock_no_mmap,
2540 .sendpage = sock_no_sendpage,
2543 #ifdef CONFIG_IRDA_ULTRA
2544 static const struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2546 .owner = THIS_MODULE,
2547 .release = irda_release,
2549 .connect = sock_no_connect,
2550 .socketpair = sock_no_socketpair,
2551 .accept = sock_no_accept,
2552 .getname = irda_getname,
2553 .poll = datagram_poll,
2554 .ioctl = irda_ioctl,
2555 #ifdef CONFIG_COMPAT
2556 .compat_ioctl = irda_compat_ioctl,
2558 .listen = sock_no_listen,
2559 .shutdown = irda_shutdown,
2560 .setsockopt = irda_setsockopt,
2561 .getsockopt = irda_getsockopt,
2562 .sendmsg = irda_sendmsg_ultra,
2563 .recvmsg = irda_recvmsg_dgram,
2564 .mmap = sock_no_mmap,
2565 .sendpage = sock_no_sendpage,
2567 #endif /* CONFIG_IRDA_ULTRA */
2569 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2570 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2571 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2572 #ifdef CONFIG_IRDA_ULTRA
2573 SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2574 #endif /* CONFIG_IRDA_ULTRA */
2577 * Function irsock_init (pro)
2579 * Initialize IrDA protocol
2582 int __init irsock_init(void)
2584 int rc = proto_register(&irda_proto, 0);
2587 rc = sock_register(&irda_family_ops);
2593 * Function irsock_cleanup (void)
2595 * Remove IrDA protocol
2598 void irsock_cleanup(void)
2600 sock_unregister(PF_IRDA);
2601 proto_unregister(&irda_proto);