File: /usr/src/linux/net/irda/af_irda.c

1     /*********************************************************************
2      *                
3      * Filename:      af_irda.c
4      * Version:       0.9
5      * Description:   IrDA sockets implementation
6      * Status:        Stable
7      * Author:        Dag Brattli <dagb@cs.uit.no>
8      * Created at:    Sun May 31 10:12:43 1998
9      * Modified at:   Sat Dec 25 21:10:23 1999
10      * Modified by:   Dag Brattli <dag@brattli.net>
11      * Sources:       af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
12      * 
13      *     Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
14      *     Copyright (c) 1999 Jean Tourrilhes <jt@hpl.hp.com>
15      *     All Rights Reserved.
16      *
17      *     This program is free software; you can redistribute it and/or 
18      *     modify it under the terms of the GNU General Public License as 
19      *     published by the Free Software Foundation; either version 2 of 
20      *     the License, or (at your option) any later version.
21      * 
22      *     This program is distributed in the hope that it will be useful,
23      *     but WITHOUT ANY WARRANTY; without even the implied warranty of
24      *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25      *     GNU General Public License for more details.
26      * 
27      *     You should have received a copy of the GNU General Public License 
28      *     along with this program; if not, write to the Free Software 
29      *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
30      *     MA 02111-1307 USA
31      *
32      *     Linux-IrDA now supports four different types of IrDA sockets:
33      *
34      *     o SOCK_STREAM:    TinyTP connections with SAR disabled. The
35      *                       max SDU size is 0 for conn. of this type
36      *     o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may 
37      *                       fragment the messages, but will preserve
38      *                       the message boundaries
39      *     o SOCK_DGRAM:     IRDAPROTO_UNITDATA: TinyTP connections with Unitdata 
40      *                       (unreliable) transfers
41      *                       IRDAPROTO_ULTRA: Connectionless and unreliable data
42      *     
43      ********************************************************************/
44     
45     #include <linux/config.h>
46     #include <linux/module.h>
47     #include <linux/types.h>
48     #include <linux/socket.h>
49     #include <linux/sockios.h>
50     #include <linux/init.h>
51     #include <linux/if_arp.h>
52     #include <linux/net.h>
53     #include <linux/irda.h>
54     #include <linux/poll.h>
55     
56     #include <asm/uaccess.h>
57     
58     #include <net/sock.h>
59     
60     #include <net/irda/irda.h>
61     #include <net/irda/iriap.h>
62     #include <net/irda/irias_object.h>
63     #include <net/irda/irlmp.h>
64     #include <net/irda/irttp.h>
65     #include <net/irda/discovery.h>
66     
67     extern int  irda_init(void);
68     extern void irda_cleanup(void);
69     extern int  irlap_driver_rcv(struct sk_buff *, struct net_device *, 
70     			     struct packet_type *);
71     
72     static int irda_create(struct socket *sock, int protocol);
73     
74     static struct proto_ops irda_stream_ops;
75     static struct proto_ops irda_seqpacket_ops;
76     static struct proto_ops irda_dgram_ops;
77     
78     #ifdef CONFIG_IRDA_ULTRA
79     static struct proto_ops irda_ultra_ops;
80     #define ULTRA_MAX_DATA 382
81     #endif /* CONFIG_IRDA_ULTRA */
82     
83     #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
84     
85     #ifdef CONFIG_IRDA_DEBUG
86     __u32 irda_debug = IRDA_DEBUG_LEVEL;
87     #endif
88     
89     /*
90      * Function irda_data_indication (instance, sap, skb)
91      *
92      *    Received some data from TinyTP. Just queue it on the receive queue
93      *
94      */
95     static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
96     {
97     	struct irda_sock *self;
98     	struct sock *sk;
99     	int err;
100     
101     	IRDA_DEBUG(3, __FUNCTION__ "()\n");
102     
103     	self = (struct irda_sock *) instance;
104     	ASSERT(self != NULL, return -1;);
105     
106     	sk = self->sk;
107     	ASSERT(sk != NULL, return -1;);
108     
109     	err = sock_queue_rcv_skb(sk, skb);
110     	if (err) {
111     		IRDA_DEBUG(1, __FUNCTION__ "(), error: no more mem!\n");
112     		self->rx_flow = FLOW_STOP;
113     
114     		/* When we return error, TTP will need to requeue the skb */
115     		return err;
116     	}
117     
118     	return 0;
119     }
120     
121     /*
122      * Function irda_disconnect_indication (instance, sap, reason, skb)
123      *
124      *    Connection has been closed. Check reason to find out why
125      *
126      */
127     static void irda_disconnect_indication(void *instance, void *sap, 
128     				       LM_REASON reason, struct sk_buff *skb)
129     {
130     	struct irda_sock *self;
131     	struct sock *sk;
132     
133     	self = (struct irda_sock *) instance;
134     
135     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
136     
137     	sk = self->sk;
138     	if (sk == NULL)
139     		return;
140     
141     	sk->state     = TCP_CLOSE;
142             sk->err       = ECONNRESET;
143             sk->shutdown |= SEND_SHUTDOWN;
144     	if (!sk->dead) {
145     		sk->state_change(sk);
146                     sk->dead = 1;
147             }
148     
149     	/* Close our TSAP.
150     	 * If we leave it open, IrLMP put it back into the list of
151     	 * unconnected LSAPs. The problem is that any incoming request
152     	 * can then be matched to this socket (and it will be, because
153     	 * it is at the head of the list). This would prevent any
154     	 * listening socket waiting on the same TSAP to get those requests.
155     	 * Some apps forget to close sockets, or hang to it a bit too long,
156     	 * so we may stay in this dead state long enough to be noticed...
157     	 * Note : all socket function do check sk->state, so we are safe...
158     	 * Jean II
159     	 */
160     	if (self->tsap) {
161     		irttp_close_tsap(self->tsap);
162     		self->tsap = NULL;
163     	}
164     
165     	/* Note : once we are there, there is not much you want to do
166     	 * with the socket anymore, apart from closing it.
167     	 * For example, bind() and connect() won't reset sk->err,
168     	 * sk->shutdown and sk->dead to valid values...
169     	 * Jean II
170     	 */
171     }
172     
173     /*
174      * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
175      *
176      *    Connections has been confirmed by the remote device
177      *
178      */
179     static void irda_connect_confirm(void *instance, void *sap, 
180     				 struct qos_info *qos,
181     				 __u32 max_sdu_size, __u8 max_header_size, 
182     				 struct sk_buff *skb)
183     {
184     	struct irda_sock *self;
185     	struct sock *sk;
186     
187     	self = (struct irda_sock *) instance;
188     
189     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
190     
191     	sk = self->sk;
192     	if (sk == NULL)
193     		return;
194     
195     	/* How much header space do we need to reserve */
196     	self->max_header_size = max_header_size;
197     
198     	/* IrTTP max SDU size in transmit direction */
199     	self->max_sdu_size_tx = max_sdu_size;
200     
201     	/* Find out what the largest chunk of data that we can transmit is */
202     	switch (sk->type) {
203     	case SOCK_STREAM:
204     		if (max_sdu_size != 0) {
205     			ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
206     			return;
207     		}
208     		self->max_data_size = irttp_get_max_seg_size(self->tsap);
209     		break;
210     	case SOCK_SEQPACKET:
211     		if (max_sdu_size == 0) {
212     			ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
213     			return;
214     		}
215     		self->max_data_size = max_sdu_size;
216     		break;
217     	default:
218     		self->max_data_size = irttp_get_max_seg_size(self->tsap);
219     	};
220     
221     	IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n", 
222     		   self->max_data_size);
223     
224     	memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
225     	kfree_skb(skb);
226     
227     	/* We are now connected! */
228     	sk->state = TCP_ESTABLISHED;
229     	sk->state_change(sk);
230     }
231     
232     /*
233      * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
234      *
235      *    Incoming connection
236      *
237      */
238     static void irda_connect_indication(void *instance, void *sap, 
239     				    struct qos_info *qos, __u32 max_sdu_size,
240     				    __u8 max_header_size, struct sk_buff *skb)
241     {
242     	struct irda_sock *self;
243     	struct sock *sk;
244     
245      	self = (struct irda_sock *) instance;
246     
247     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
248     
249     	sk = self->sk;
250     	if (sk == NULL)
251     		return;
252     
253     	/* How much header space do we need to reserve */
254     	self->max_header_size = max_header_size;
255     
256     	/* IrTTP max SDU size in transmit direction */
257     	self->max_sdu_size_tx = max_sdu_size;	
258     
259     	/* Find out what the largest chunk of data that we can transmit is */
260     	switch (sk->type) {
261     	case SOCK_STREAM:
262     		if (max_sdu_size != 0) {
263     			ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
264     			return;
265     		}
266     		self->max_data_size = irttp_get_max_seg_size(self->tsap);
267     		break;
268     	case SOCK_SEQPACKET:
269     		if (max_sdu_size == 0) {
270     			ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
271     			return;
272     		}
273     		self->max_data_size = max_sdu_size;
274     		break;
275     	default:
276     		self->max_data_size = irttp_get_max_seg_size(self->tsap);
277     	};
278     
279     	IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n", 
280     		   self->max_data_size);
281     
282     	memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
283     	
284     	skb_queue_tail(&sk->receive_queue, skb);
285     	sk->state_change(sk);
286     }
287     
288     /*
289      * Function irda_connect_response (handle)
290      *
291      *    Accept incoming connection
292      *
293      */
294     void irda_connect_response(struct irda_sock *self)
295     {
296     	struct sk_buff *skb;
297     
298     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
299     
300     	ASSERT(self != NULL, return;);
301     
302     	skb = dev_alloc_skb(64);
303     	if (skb == NULL) {
304     		IRDA_DEBUG(0, __FUNCTION__ "() Unable to allocate sk_buff!\n");
305     		return;
306     	}
307     
308     	/* Reserve space for MUX_CONTROL and LAP header */
309     	skb_reserve(skb, IRDA_MAX_HEADER);
310     
311     	irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
312     }
313     
314     /*
315      * Function irda_flow_indication (instance, sap, flow)
316      *
317      *    Used by TinyTP to tell us if it can accept more data or not
318      *
319      */
320     static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow) 
321     {
322     	struct irda_sock *self;
323     	struct sock *sk;
324     
325     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
326     	
327     	self = (struct irda_sock *) instance;
328     	ASSERT(self != NULL, return;);
329     
330     	sk = self->sk;
331     	ASSERT(sk != NULL, return;);
332     	
333     	switch (flow) {
334     	case FLOW_STOP:
335     		IRDA_DEBUG(1, __FUNCTION__ "(), IrTTP wants us to slow down\n");
336     		self->tx_flow = flow;
337     		break;
338     	case FLOW_START:
339     		self->tx_flow = flow;
340     		IRDA_DEBUG(1, __FUNCTION__ 
341     			   "(), IrTTP wants us to start again\n");
342     		wake_up_interruptible(sk->sleep);
343     		break;
344     	default:
345     		IRDA_DEBUG( 0, __FUNCTION__ "(), Unknown flow command!\n");
346     		/* Unknown flow command, better stop */
347     		self->tx_flow = flow;
348     		break;
349     	}
350     }
351     
352     /*
353      * Function irda_getvalue_confirm (obj_id, value, priv)
354      *
355      *    Got answer from remote LM-IAS, just pass object to requester...
356      *
357      * Note : duplicate from above, but we need our own version that
358      * doesn't touch the dtsap_sel and save the full value structure...
359      */
360     static void irda_getvalue_confirm(int result, __u16 obj_id, 
361     					  struct ias_value *value, void *priv)
362     {
363     	struct irda_sock *self;
364     	
365     	self = (struct irda_sock *) priv;
366     	if (!self) {
367     		WARNING(__FUNCTION__ "(), lost myself!\n");
368     		return;
369     	}
370     
371     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
372     
373     	/* We probably don't need to make any more queries */
374     	iriap_close(self->iriap);
375     	self->iriap = NULL;
376     
377     	/* Check if request succeeded */
378     	if (result != IAS_SUCCESS) {
379     		IRDA_DEBUG(1, __FUNCTION__ "(), IAS query failed! (%d)\n",
380     			   result);
381     
382     		self->errno = result;	/* We really need it later */
383     
384     		/* Wake up any processes waiting for result */
385     		wake_up_interruptible(&self->query_wait);
386     
387     		return;
388     	}
389     
390     	/* Pass the object to the caller (so the caller must delete it) */
391     	self->ias_result = value;
392     	self->errno = 0;
393     
394     	/* Wake up any processes waiting for result */
395     	wake_up_interruptible(&self->query_wait);
396     }
397     
398     /*
399      * Function irda_selective_discovery_indication (discovery)
400      *
401      *    Got a selective discovery indication from IrLMP.
402      *
403      * IrLMP is telling us that this node is matching our hint bit
404      * filter. Check if it's a newly discovered node (or if node changed its
405      * hint bits), and then wake up any process waiting for answer...
406      */
407     static void irda_selective_discovery_indication(discovery_t *discovery,
408     						void *priv)
409     {
410     	struct irda_sock *self;
411     	
412     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
413     
414     	self = (struct irda_sock *) priv;
415     	if (!self) {
416     		WARNING(__FUNCTION__ "(), lost myself!\n");
417     		return;
418     	}
419     
420     	/* Check if node is discovered is a new one or an old one.
421     	 * We check when how long ago this node was discovered, with a
422     	 * coarse timeout (we may miss some discovery events or be delayed).
423     	 * Note : by doing this test here, we avoid waking up a process ;-)
424     	 */
425     	if((jiffies - discovery->first_timestamp) >
426     	   (sysctl_discovery_timeout * HZ)) {
427     		return;		/* Too old, not interesting -> goodbye */
428     	}
429     
430     	/* Pass parameter to the caller */
431     	self->cachediscovery = discovery;
432     
433     	/* Wake up process if its waiting for device to be discovered */
434     	wake_up_interruptible(&self->query_wait);
435     }
436     
437     /*
438      * Function irda_discovery_timeout (priv)
439      *
440      *    Timeout in the selective discovery process
441      *
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...
444      */
445     static void irda_discovery_timeout(u_long priv)
446     {
447     	struct irda_sock *self;
448     	
449     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
450     
451     	self = (struct irda_sock *) priv;
452     	ASSERT(self != NULL, return;);
453     
454     	/* Nothing for the caller */
455     	self->cachelog = NULL;
456     	self->cachediscovery = NULL;
457     	self->errno = -ETIME;
458     
459     	/* Wake up process if its still waiting... */
460     	wake_up_interruptible(&self->query_wait);
461     }
462     
463     /*
464      * Function irda_open_tsap (self)
465      *
466      *    Open local Transport Service Access Point (TSAP)
467      *
468      */
469     static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
470     {
471     	notify_t notify;
472     
473     	if (self->tsap) {
474     		WARNING(__FUNCTION__ "(), busy!\n");
475     		return -EBUSY;
476     	}
477     	
478     	/* Initialize callbacks to be used by the IrDA stack */
479     	irda_notify_init(&notify);
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);
488     
489     	self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
490     				     &notify);	
491     	if (self->tsap == NULL) {
492     		IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate TSAP!\n");
493     		return -ENOMEM;
494     	}
495     	/* Remember which TSAP selector we actually got */
496     	self->stsap_sel = self->tsap->stsap_sel;
497     
498     	return 0;
499     }
500     
501     /*
502      * Function irda_open_lsap (self)
503      *
504      *    Open local Link Service Access Point (LSAP). Used for opening Ultra
505      *    sockets
506      */
507     #ifdef CONFIG_IRDA_ULTRA
508     static int irda_open_lsap(struct irda_sock *self, int pid)
509     {
510     	notify_t notify;
511     
512     	if (self->lsap) {
513     		WARNING(__FUNCTION__ "(), busy!\n");
514     		return -EBUSY;
515     	}
516     	
517     	/* Initialize callbacks to be used by the IrDA stack */
518     	irda_notify_init(&notify);
519     	notify.udata_indication	= irda_data_indication;
520     	notify.instance = self;
521     	strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
522     
523     	self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);	
524     	if (self->lsap == NULL) {
525     		IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate LSAP!\n");
526     		return -ENOMEM;
527     	}
528     
529     	return 0;
530     }
531     #endif /* CONFIG_IRDA_ULTRA */
532     
533     /*
534      * Function irda_find_lsap_sel (self, name)
535      *
536      *    Try to lookup LSAP selector in remote LM-IAS
537      *
538      * Basically, we start a IAP query, and then go to sleep. When the query
539      * return, irda_getvalue_confirm will wake us up, and we can examine the
540      * result of the query...
541      * Note that in some case, the query fail even before we go to sleep,
542      * creating some races...
543      */
544     static int irda_find_lsap_sel(struct irda_sock *self, char *name)
545     {
546     	IRDA_DEBUG(2, __FUNCTION__ "(%p, %s)\n", self, name);
547     
548     	ASSERT(self != NULL, return -1;);
549     
550     	if (self->iriap) {
551     		WARNING(__FUNCTION__ "(), busy with a previous query\n");
552     		return -EBUSY;
553     	}
554     
555     	self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
556     				 irda_getvalue_confirm);
557     	if(self->iriap == NULL)
558     		return -ENOMEM;
559     
560     	/* Treat unexpected signals as disconnect */
561     	self->errno = -EHOSTUNREACH;
562     
563     	/* Query remote LM-IAS */
564     	iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
565     				      name, "IrDA:TinyTP:LsapSel");
566     	/* Wait for answer (if not already failed) */
567     	if(self->iriap != NULL)
568     		interruptible_sleep_on(&self->query_wait);
569     
570     	/* Check what happened */
571     	if (self->errno)
572     	{
573     		/* Requested object/attribute doesn't exist */
574     		if((self->errno == IAS_CLASS_UNKNOWN) ||
575     		   (self->errno == IAS_ATTRIB_UNKNOWN))
576     			return (-EADDRNOTAVAIL);
577     		else
578     			return (-EHOSTUNREACH);
579     	}
580     
581     	/* Get the remote TSAP selector */
582     	switch (self->ias_result->type) {
583     	case IAS_INTEGER:
584     		IRDA_DEBUG(4, __FUNCTION__ "() int=%d\n",
585     			   self->ias_result->t.integer);
586     		
587     		if (self->ias_result->t.integer != -1)
588     			self->dtsap_sel = self->ias_result->t.integer;
589     		else 
590     			self->dtsap_sel = 0;
591     		break;
592     	default:
593     		self->dtsap_sel = 0;
594     		IRDA_DEBUG(0, __FUNCTION__ "(), bad type!\n");
595     		break;
596     	}
597     	if (self->ias_result)
598     		irias_delete_value(self->ias_result);
599     
600     	if (self->dtsap_sel)
601     		return 0;
602     
603     	return -EADDRNOTAVAIL;
604     }
605     
606     /*
607      * Function irda_discover_daddr_and_lsap_sel (self, name)
608      *
609      *    This try to find a device with the requested service.
610      *
611      * It basically look into the discovery log. For each address in the list,
612      * it queries the LM-IAS of the device to find if this device offer
613      * the requested service.
614      * If there is more than one node supporting the service, we complain
615      * to the user (it should move devices around).
616      * The, we set both the destination address and the lsap selector to point
617      * on the service on the unique device we have found.
618      *
619      * Note : this function fails if there is more than one device in range,
620      * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
621      * Moreover, we would need to wait the LAP disconnection...
622      */
623     static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
624     {
625     	struct irda_device_info *discoveries;	/* Copy of the discovery log */
626     	int	number;			/* Number of nodes in the log */
627     	int	i;
628     	int	err = -ENETUNREACH;
629     	__u32	daddr = DEV_ADDR_ANY;	/* Address we found the service on */
630     	__u8	dtsap_sel = 0x0;	/* TSAP associated with it */
631     
632     	IRDA_DEBUG(2, __FUNCTION__ "(), name=%s\n", name);
633     
634     	ASSERT(self != NULL, return -1;);
635     
636     	/* Ask lmp for the current discovery log
637     	 * Note : we have to use irlmp_get_discoveries(), as opposed
638     	 * to play with the cachelog directly, because while we are
639     	 * making our ias query, le log might change... */
640     	discoveries = irlmp_get_discoveries(&number, self->mask);
641     	/* Check if the we got some results */
642     	if (discoveries == NULL)
643     		return -ENETUNREACH;	/* No nodes discovered */
644     
645     	/* 
646     	 * Now, check all discovered devices (if any), and connect
647     	 * client only about the services that the client is
648     	 * interested in...
649     	 */
650     	for(i = 0; i < number; i++) {
651     		/* Try the address in the log */
652     		self->daddr = discoveries[i].daddr;
653     		self->saddr = 0x0;
654     		IRDA_DEBUG(1, __FUNCTION__ "(), trying daddr = %08x\n",
655     			   self->daddr);
656     
657     		/* Query remote LM-IAS for this service */
658     		err = irda_find_lsap_sel(self, name);
659     		switch (err) {
660     		case 0:
661     			/* We found the requested service */
662     			if(daddr != DEV_ADDR_ANY) {
663     				IRDA_DEBUG(1, __FUNCTION__
664     					   "(), discovered service ''%s'' in two different devices !!!\n",
665     					   name);
666     				self->daddr = DEV_ADDR_ANY;
667     				kfree(discoveries);
668     				return(-ENOTUNIQ);
669     			}
670     			/* First time we found that one, save it ! */
671     			daddr = self->daddr;
672     			dtsap_sel = self->dtsap_sel;
673     			break;
674     		case -EADDRNOTAVAIL:
675     			/* Requested service simply doesn't exist on this node */
676     			break;
677     		default:
678     			/* Something bad did happen :-( */
679     			IRDA_DEBUG(0, __FUNCTION__
680     				   "(), unexpected IAS query failure\n");
681     			self->daddr = DEV_ADDR_ANY;
682     			kfree(discoveries);
683     			return(-EHOSTUNREACH);
684     			break;
685     		}
686     	}
687     	/* Cleanup our copy of the discovery log */
688     	kfree(discoveries);
689     
690     	/* Check out what we found */
691     	if(daddr == DEV_ADDR_ANY) {
692     		IRDA_DEBUG(1, __FUNCTION__
693     			   "(), cannot discover service ''%s'' in any device !!!\n",
694     			   name);
695     		self->daddr = DEV_ADDR_ANY;
696     		return(-EADDRNOTAVAIL);
697     	}
698     
699     	/* Revert back to discovered device & service */
700     	self->daddr = daddr;
701     	self->saddr = 0x0;
702     	self->dtsap_sel = dtsap_sel;
703     
704     	IRDA_DEBUG(1, __FUNCTION__ 
705     		   "(), discovered requested service ''%s'' at address %08x\n",
706     		   name, self->daddr);
707     
708     	return 0;
709     }
710     
711     /*
712      * Function irda_getname (sock, uaddr, uaddr_len, peer)
713      *
714      *    Return the our own, or peers socket address (sockaddr_irda)
715      *
716      */
717     static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
718     			int *uaddr_len, int peer)
719     {
720     	struct sockaddr_irda saddr;
721     	struct sock *sk = sock->sk;
722     	struct irda_sock *self = sk->protinfo.irda;
723     
724     	if (peer) {
725     		if (sk->state != TCP_ESTABLISHED)
726     			return -ENOTCONN;
727     		
728     		saddr.sir_family = AF_IRDA;
729     		saddr.sir_lsap_sel = self->dtsap_sel;
730     		saddr.sir_addr = self->daddr;
731     	} else {
732     		saddr.sir_family = AF_IRDA;
733     		saddr.sir_lsap_sel = self->stsap_sel;
734     		saddr.sir_addr = self->saddr;
735     	}
736     	
737     	IRDA_DEBUG(1, __FUNCTION__ "(), tsap_sel = %#x\n", saddr.sir_lsap_sel);
738     	IRDA_DEBUG(1, __FUNCTION__ "(), addr = %08x\n", saddr.sir_addr);
739     
740     	/* uaddr_len come to us uninitialised */
741     	*uaddr_len = sizeof (struct sockaddr_irda);
742     	memcpy(uaddr, &saddr, *uaddr_len);
743     
744     	return 0;
745     }
746     
747     /*
748      * Function irda_listen (sock, backlog)
749      *
750      *    Just move to the listen state
751      *
752      */
753     static int irda_listen(struct socket *sock, int backlog)
754     {
755     	struct sock *sk = sock->sk;
756     
757     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
758     
759     	if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
760     	    (sk->type != SOCK_DGRAM))
761     		return -EOPNOTSUPP;
762     
763     	if (sk->state != TCP_LISTEN) {
764     		sk->max_ack_backlog = backlog;
765     		sk->state           = TCP_LISTEN;
766     		
767     		return 0;
768     	}
769     	
770     	return -EOPNOTSUPP;
771     }
772     
773     /*
774      * Function irda_bind (sock, uaddr, addr_len)
775      *
776      *    Used by servers to register their well known TSAP
777      *
778      */
779     static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
780     {
781     	struct sock *sk = sock->sk;
782     	struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
783     	struct irda_sock *self;
784     	int err;
785     
786     	self = sk->protinfo.irda;
787     	ASSERT(self != NULL, return -1;);
788     
789     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
790     
791     	if (addr_len != sizeof(struct sockaddr_irda))
792     		return -EINVAL;
793     
794     #ifdef CONFIG_IRDA_ULTRA
795     	/* Special care for Ultra sockets */
796     	if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
797     		self->pid = addr->sir_lsap_sel;
798     		if (self->pid & 0x80) {
799     			IRDA_DEBUG(0, __FUNCTION__ 
800     				   "(), extension in PID not supp!\n");
801     			return -EOPNOTSUPP;
802     		}
803     		err = irda_open_lsap(self, self->pid);
804     		if (err < 0)
805     			return err;
806     		
807     		self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
808     		self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
809     
810     		/* Pretend we are connected */
811     		sock->state = SS_CONNECTED;
812     		sk->state   = TCP_ESTABLISHED;
813     
814     		return 0;
815     	}
816     #endif /* CONFIG_IRDA_ULTRA */
817     
818     	err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
819     	if (err < 0)
820     		return err;
821     	
822     	/*  Register with LM-IAS */
823     	self->ias_obj = irias_new_object(addr->sir_name, jiffies);
824     	irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel", 
825     				 self->stsap_sel, IAS_KERNEL_ATTR);
826     	irias_insert_object(self->ias_obj);
827     	
828     	return 0;
829     }
830     
831     /*
832      * Function irda_accept (sock, newsock, flags)
833      *
834      *    Wait for incoming connection
835      *
836      */
837     static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
838     {
839     	struct irda_sock *self, *new;
840     	struct sock *sk = sock->sk;
841     	struct sock *newsk;
842     	struct sk_buff *skb;
843     	int err;
844     
845     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
846     
847     	self = sk->protinfo.irda;
848     	ASSERT(self != NULL, return -1;);
849     
850     	err = irda_create(newsock, sk->protocol);
851     	if (err)
852     		return err;
853     
854     	if (sock->state != SS_UNCONNECTED)
855     		return -EINVAL;
856     
857     	if ((sk = sock->sk) == NULL)
858     		return -EINVAL;
859     
860     	if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
861     	    (sk->type != SOCK_DGRAM))
862     		return -EOPNOTSUPP;
863     
864     	if (sk->state != TCP_LISTEN) 
865     		return -EINVAL;
866     
867     	/*
868     	 *	The read queue this time is holding sockets ready to use
869     	 *	hooked into the SABM we saved
870     	 */
871     	do {
872     		if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
873     			if (flags & O_NONBLOCK)
874     				return -EWOULDBLOCK;
875     
876     			interruptible_sleep_on(sk->sleep);
877     			if (signal_pending(current)) 
878     				return -ERESTARTSYS;
879     		}
880     	} while (skb == NULL);
881     
882      	newsk = newsock->sk;
883     	newsk->state = TCP_ESTABLISHED;
884     
885     	new = newsk->protinfo.irda;
886     	ASSERT(new != NULL, return -1;);
887     
888     	/* Now attach up the new socket */
889     	new->tsap = irttp_dup(self->tsap, new);
890     	if (!new->tsap) {
891     		IRDA_DEBUG(0, __FUNCTION__ "(), dup failed!\n");
892     		return -1;
893     	}
894     		
895     	new->stsap_sel = new->tsap->stsap_sel;
896     	new->dtsap_sel = new->tsap->dtsap_sel;
897     	new->saddr = irttp_get_saddr(new->tsap);
898     	new->daddr = irttp_get_daddr(new->tsap);
899     
900     	new->max_sdu_size_tx = self->max_sdu_size_tx;
901     	new->max_sdu_size_rx = self->max_sdu_size_rx;
902     	new->max_data_size   = self->max_data_size;
903     	new->max_header_size = self->max_header_size;
904     
905     	memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
906     
907     	/* Clean up the original one to keep it in listen state */
908     	self->tsap->dtsap_sel = self->tsap->lsap->dlsap_sel = LSAP_ANY;
909     	self->tsap->lsap->lsap_state = LSAP_DISCONNECTED;
910     
911     	skb->sk = NULL;
912     	skb->destructor = NULL;
913     	kfree_skb(skb);
914     	sk->ack_backlog--;
915     
916     	newsock->state = SS_CONNECTED;
917     
918     	irda_connect_response(new);
919     
920     	return 0;
921     }
922     
923     /*
924      * Function irda_connect (sock, uaddr, addr_len, flags)
925      *
926      *    Connect to a IrDA device
927      *
928      * The main difference with a "standard" connect is that with IrDA we need
929      * to resolve the service name into a TSAP selector (in TCP, port number
930      * doesn't have to be resolved).
931      * Because of this service name resoltion, we can offer "auto-connect",
932      * where we connect to a service without specifying a destination address.
933      *
934      * Note : by consulting "errno", the user space caller may learn the cause
935      * of the failure. Most of them are visible in the function, others may come
936      * from subroutines called and are listed here :
937      *	o EBUSY : already processing a connect
938      *	o EHOSTUNREACH : bad addr->sir_addr argument
939      *	o EADDRNOTAVAIL : bad addr->sir_name argument
940      *	o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
941      *	o ENETUNREACH : no node found on the network (auto-connect)
942      */
943     static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
944     			int addr_len, int flags)
945     {
946     	struct sock *sk = sock->sk;
947     	struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
948     	struct irda_sock *self;
949     	int err;
950     
951     	self = sk->protinfo.irda;
952     	
953     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
954     
955     	/* Don't allow connect for Ultra sockets */
956     	if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA))
957     		return -ESOCKTNOSUPPORT;
958     
959     	if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
960     		sock->state = SS_CONNECTED;
961     		return 0;   /* Connect completed during a ERESTARTSYS event */
962     	}
963     	
964     	if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
965     		sock->state = SS_UNCONNECTED;
966     		return -ECONNREFUSED;
967     	}
968     	
969     	if (sk->state == TCP_ESTABLISHED)
970     		return -EISCONN;      /* No reconnect on a seqpacket socket */
971     	
972     	sk->state   = TCP_CLOSE;	
973     	sock->state = SS_UNCONNECTED;
974     
975     	if (addr_len != sizeof(struct sockaddr_irda))
976     		return -EINVAL;
977     
978     	/* Check if user supplied any destination device address */
979     	if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
980     		/* Try to find one suitable */
981     		err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
982     		if (err) {
983     			IRDA_DEBUG(0, __FUNCTION__ 
984     				   "(), auto-connect failed!\n");
985     			return err;
986     		}
987     	} else {
988     		/* Use the one provided by the user */
989     		self->daddr = addr->sir_addr;
990     		IRDA_DEBUG(1, __FUNCTION__ "(), daddr = %08x\n", self->daddr);
991     		
992     		/* Query remote LM-IAS */
993     		err = irda_find_lsap_sel(self, addr->sir_name);
994     		if (err) {
995     			IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
996     			return err;
997     		}
998     	}
999     
1000     	/* Check if we have opened a local TSAP */
1001     	if (!self->tsap)
1002     		irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1003     	
1004     	/* Move to connecting socket, start sending Connect Requests */
1005     	sock->state = SS_CONNECTING;
1006     	sk->state   = TCP_SYN_SENT;
1007     
1008     	/* Connect to remote device */
1009     	err = irttp_connect_request(self->tsap, self->dtsap_sel, 
1010     				    self->saddr, self->daddr, NULL, 
1011     				    self->max_sdu_size_rx, NULL);
1012     	if (err) {
1013     		IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
1014     		return err;
1015     	}
1016     
1017     	/* Now the loop */
1018     	if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1019     		return -EINPROGRESS;
1020     		
1021     	cli();	/* To avoid races on the sleep */
1022     	
1023     	/* A Connect Ack with Choke or timeout or failed routing will go to
1024     	 * closed.  */
1025     	while (sk->state == TCP_SYN_SENT) {
1026     		interruptible_sleep_on(sk->sleep);
1027     		if (signal_pending(current)) {
1028     			sti();
1029     			return -ERESTARTSYS;
1030     		}
1031     	}
1032     	
1033     	if (sk->state != TCP_ESTABLISHED) {
1034     		sti();
1035     		sock->state = SS_UNCONNECTED;
1036     		return sock_error(sk);	/* Always set at this point */
1037     	}
1038     	
1039     	sock->state = SS_CONNECTED;
1040     	
1041     	sti();
1042     	
1043     	/* At this point, IrLMP has assigned our source address */
1044     	self->saddr = irttp_get_saddr(self->tsap);
1045     
1046     	return 0;
1047     }
1048     
1049     /*
1050      * Function irda_create (sock, protocol)
1051      *
1052      *    Create IrDA socket
1053      *
1054      */
1055     static int irda_create(struct socket *sock, int protocol)
1056     {
1057     	struct sock *sk;
1058     	struct irda_sock *self;
1059     
1060     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
1061     	
1062     	/* Check for valid socket type */
1063     	switch (sock->type) {
1064     	case SOCK_STREAM:     /* For TTP connections with SAR disabled */
1065     	case SOCK_SEQPACKET:  /* For TTP connections with SAR enabled */
1066     	case SOCK_DGRAM:      /* For TTP Unitdata or LMP Ultra transfers */
1067     		break;
1068     	default:
1069     		return -ESOCKTNOSUPPORT;
1070     	}
1071     
1072     	/* Allocate networking socket */
1073     	if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
1074     		return -ENOMEM;
1075     
1076     	/* Allocate IrDA socket */
1077     	self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
1078     	if (self == NULL) {
1079     		sk_free(sk);
1080     		return -ENOMEM;
1081     	}
1082     	memset(self, 0, sizeof(struct irda_sock));
1083     
1084     	IRDA_DEBUG(2, __FUNCTION__ "() : self is %p\n", self);
1085     
1086     	init_waitqueue_head(&self->query_wait);
1087     
1088     	/* Initialise networking socket struct */ 
1089     	sock_init_data(sock, sk);	/* Note : set sk->refcnt to 1 */
1090     	sk->family = PF_IRDA;
1091     	sk->protocol = protocol;
1092     	/* Link networking socket and IrDA socket structs together */
1093     	sk->protinfo.irda = self;
1094     	self->sk = sk;
1095     
1096     	switch (sock->type) {
1097     	case SOCK_STREAM:
1098     		sock->ops = &irda_stream_ops;
1099     		self->max_sdu_size_rx = TTP_SAR_DISABLE;
1100     		break;
1101     	case SOCK_SEQPACKET:
1102     		sock->ops = &irda_seqpacket_ops;
1103     		self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1104     		break;
1105     	case SOCK_DGRAM:
1106     		switch (protocol) {
1107     #ifdef CONFIG_IRDA_ULTRA
1108     		case IRDAPROTO_ULTRA:
1109     			sock->ops = &irda_ultra_ops;
1110     			break;
1111     #endif /* CONFIG_IRDA_ULTRA */
1112     		case IRDAPROTO_UNITDATA:
1113     			sock->ops = &irda_dgram_ops;
1114     			/* We let Unitdata conn. be like seqpack conn. */
1115     			self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1116     			break;
1117     		default:
1118     			ERROR(__FUNCTION__ "(), protocol not supported!\n");
1119     			return -ESOCKTNOSUPPORT;
1120     		}
1121     		break;
1122     	default:
1123     		return -ESOCKTNOSUPPORT;
1124     	}		
1125     
1126     	/* Register as a client with IrLMP */
1127     	self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1128     	self->mask = 0xffff;
1129     	self->rx_flow = self->tx_flow = FLOW_START;
1130     	self->nslots = DISCOVERY_DEFAULT_SLOTS;
1131     	self->daddr = DEV_ADDR_ANY;	/* Until we get connected */
1132     	self->saddr = 0x0;		/* so IrLMP assign us any link */
1133     
1134     	MOD_INC_USE_COUNT;
1135     
1136     	return 0;
1137     }
1138     
1139     /*
1140      * Function irda_destroy_socket (self)
1141      *
1142      *    Destroy socket
1143      *
1144      */
1145     void irda_destroy_socket(struct irda_sock *self)
1146     {
1147     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1148     
1149     	ASSERT(self != NULL, return;);
1150     
1151     	/* Unregister with IrLMP */
1152     	irlmp_unregister_client(self->ckey);
1153     	irlmp_unregister_service(self->skey);
1154     
1155     	/* Unregister with LM-IAS */
1156     	if (self->ias_obj) {
1157     		irias_delete_object(self->ias_obj);
1158     		self->ias_obj = NULL;
1159     	}
1160     
1161     	if (self->iriap) {
1162     		iriap_close(self->iriap);
1163     		self->iriap = NULL;
1164     	}
1165     
1166     	if (self->tsap) {
1167     		irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1168     		irttp_close_tsap(self->tsap);
1169     		self->tsap = NULL;
1170     	}
1171     #ifdef CONFIG_IRDA_ULTRA
1172     	if (self->lsap) {
1173     		irlmp_close_lsap(self->lsap);
1174     		self->lsap = NULL;
1175     	}
1176     #endif /* CONFIG_IRDA_ULTRA */
1177     	kfree(self);
1178     	MOD_DEC_USE_COUNT;
1179     	
1180     	return;
1181     }
1182     
1183     /*
1184      * Function irda_release (sock)
1185      *
1186      *    
1187      *
1188      */
1189     static int irda_release(struct socket *sock)
1190     {
1191     	struct sock *sk = sock->sk;
1192     	
1193     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
1194     
1195             if (sk == NULL) 
1196     		return 0;
1197     	
1198     	sk->state       = TCP_CLOSE;
1199     	sk->shutdown   |= SEND_SHUTDOWN;
1200     	sk->state_change(sk);
1201     
1202     	/* Destroy IrDA socket */
1203     	irda_destroy_socket(sk->protinfo.irda);
1204     	/* Prevent sock_def_destruct() to create havoc */
1205     	sk->protinfo.irda = NULL;
1206     
1207     	sock_orphan(sk);
1208             sock->sk   = NULL;      
1209     
1210     	/* Purge queues (see sock_init_data()) */
1211     	skb_queue_purge(&sk->receive_queue);
1212     
1213     	/* Destroy networking socket if we are the last reference on it,
1214     	 * i.e. if(sk->refcnt == 0) -> sk_free(sk) */
1215     	sock_put(sk);
1216     
1217     	/* Notes on socket locking and deallocation... - Jean II
1218     	 * In theory we should put pairs of sock_hold() / sock_put() to
1219     	 * prevent the socket to be destroyed whenever there is an
1220     	 * outstanding request or outstanding incomming packet or event.
1221     	 *
1222     	 * 1) This may include IAS request, both in connect and getsockopt.
1223     	 * Unfortunately, the situation is a bit more messy than it looks,
1224     	 * because we close iriap and kfree(self) above.
1225     	 * 
1226     	 * 2) This may include selective discovery in getsockopt.
1227     	 * Same stuff as above, irlmp registration and self are gone.
1228     	 *
1229     	 * Probably 1 and 2 may not matter, because it's all triggered
1230     	 * by a process and the socket layer already prevent the
1231     	 * socket to go away while a process is holding it, through
1232     	 * sockfd_put() and fput()...
1233     	 *
1234     	 * 3) This may include deferred TSAP closure. In particular,
1235     	 * we may receive a late irda_disconnect_indication()
1236     	 * Fortunately, (tsap_cb *)->close_pend should protect us
1237     	 * from that.
1238     	 *
1239     	 * I did some testing on SMP, and it looks solid. And the socket
1240     	 * memory leak is now gone... - Jean II
1241     	 */
1242     
1243             return 0;
1244     }
1245     
1246     /*
1247      * Function irda_sendmsg (sock, msg, len, scm)
1248      *
1249      *    Send message down to TinyTP. This function is used for both STREAM and
1250      *    SEQPACK services. This is possible since it forces the client to 
1251      *    fragment the message if necessary
1252      */
1253     static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len, 
1254     			struct scm_cookie *scm)
1255     {
1256     	struct sock *sk = sock->sk;
1257     	struct irda_sock *self;
1258     	struct sk_buff *skb;
1259     	unsigned char *asmptr;
1260     	int err;
1261     
1262     	IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1263     
1264     	/* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1265     	if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
1266     		return -EINVAL;
1267     
1268     	if (sk->shutdown & SEND_SHUTDOWN) {
1269     		send_sig(SIGPIPE, current, 0);
1270     		return -EPIPE;
1271     	}
1272     
1273     	if (sk->state != TCP_ESTABLISHED)
1274     		return -ENOTCONN;
1275     
1276     	self = sk->protinfo.irda;
1277     	ASSERT(self != NULL, return -1;);
1278     
1279     	/* Check if IrTTP is wants us to slow down */
1280     	while (self->tx_flow == FLOW_STOP) {
1281     		IRDA_DEBUG(2, __FUNCTION__ "(), IrTTP is busy, going to sleep!\n");
1282     		interruptible_sleep_on(sk->sleep);
1283     		
1284     		/* Check if we are still connected */
1285     		if (sk->state != TCP_ESTABLISHED)
1286     			return -ENOTCONN;
1287     	}
1288     
1289     	/* Check that we don't send out to big frames */
1290     	if (len > self->max_data_size) {
1291     		IRDA_DEBUG(2, __FUNCTION__ 
1292     			   "(), Chopping frame from %d to %d bytes!\n", len, 
1293     			   self->max_data_size);
1294     		len = self->max_data_size;
1295     	}
1296     
1297     	skb = sock_alloc_send_skb(sk, len + self->max_header_size, 
1298     				  msg->msg_flags & MSG_DONTWAIT, &err);
1299     	if (!skb)
1300     		return -ENOBUFS;
1301     
1302     	skb_reserve(skb, self->max_header_size);
1303     	
1304     	asmptr = skb->h.raw = skb_put(skb, len);
1305     	memcpy_fromiovec(asmptr, msg->msg_iov, len);
1306     
1307     	/* 
1308     	 * Just send the message to TinyTP, and let it deal with possible 
1309     	 * errors. No need to duplicate all that here
1310     	 */
1311     	err = irttp_data_request(self->tsap, skb);
1312     	if (err) {
1313     		IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1314     		return err;
1315     	}
1316     	/* Tell client how much data we actually sent */
1317     	return len;
1318     }
1319     
1320     /*
1321      * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
1322      *
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
1325      */
1326     static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg, 
1327     			      int size, int flags, struct scm_cookie *scm)
1328     {
1329     	struct irda_sock *self;
1330     	struct sock *sk = sock->sk;
1331     	struct sk_buff *skb;
1332     	int copied, err;
1333     
1334     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
1335     
1336     	self = sk->protinfo.irda;
1337     	ASSERT(self != NULL, return -1;);
1338     
1339     	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 
1340     				flags & MSG_DONTWAIT, &err);
1341     	if (!skb)
1342     		return err;
1343     
1344     	skb->h.raw = skb->data;
1345     	copied     = skb->len;
1346     	
1347     	if (copied > size) {
1348     		IRDA_DEBUG(2, __FUNCTION__ 
1349     			   "(), Received truncated frame (%d < %d)!\n",
1350     			   copied, size);
1351     		copied = size;
1352     		msg->msg_flags |= MSG_TRUNC;
1353     	}
1354     	skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1355     
1356     	skb_free_datagram(sk, skb);
1357     
1358     	/*
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
1362     	 *  empty
1363     	 */
1364     	if (self->rx_flow == FLOW_STOP) {
1365     		if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1366     			IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1367     			self->rx_flow = FLOW_START;
1368     			irttp_flow_request(self->tsap, FLOW_START);
1369     		}
1370     	}
1371     
1372     	return copied;
1373     }
1374     
1375     /*
1376      * Function irda_data_wait (sk)
1377      *
1378      *    Sleep until data has arrive. But check for races..
1379      *
1380      */
1381     static void irda_data_wait(struct sock *sk)
1382     {
1383     	if (!skb_peek(&sk->receive_queue)) {
1384     		set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1385     		interruptible_sleep_on(sk->sleep);
1386     		clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1387     	}
1388     }
1389     
1390     /*
1391      * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
1392      *
1393      *    
1394      *
1395      */
1396     static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg, 
1397     			       int size, int flags, struct scm_cookie *scm)
1398     {
1399     	struct irda_sock *self;
1400     	struct sock *sk = sock->sk;
1401     	int noblock = flags & MSG_DONTWAIT;
1402     	int copied = 0;
1403     	int target = 1;
1404     
1405     	IRDA_DEBUG(3, __FUNCTION__ "()\n");
1406     
1407     	self = sk->protinfo.irda;
1408     	ASSERT(self != NULL, return -1;);
1409     
1410     	if (sock->flags & __SO_ACCEPTCON) 
1411     		return(-EINVAL);
1412     
1413     	if (flags & MSG_OOB)
1414     		return -EOPNOTSUPP;
1415     
1416     	if (flags & MSG_WAITALL)
1417     		target = size;
1418     		
1419     	msg->msg_namelen = 0;
1420     
1421     	do {
1422     		int chunk;
1423     		struct sk_buff *skb;
1424     
1425     		skb=skb_dequeue(&sk->receive_queue);
1426     		if (skb==NULL) {
1427     			if (copied >= target)
1428     				break;
1429     			
1430     			/*
1431     			 *	POSIX 1003.1g mandates this order.
1432     			 */
1433     			
1434     			if (sk->err) {
1435     				return sock_error(sk);
1436     			}
1437     
1438     			if (sk->shutdown & RCV_SHUTDOWN)
1439     				break;
1440     
1441     			if (noblock)
1442     				return -EAGAIN;
1443     			irda_data_wait(sk);
1444     			if (signal_pending(current))
1445     				return -ERESTARTSYS;
1446     			continue;
1447     		}
1448     
1449     		chunk = min_t(unsigned int, skb->len, size);
1450     		if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1451     			skb_queue_head(&sk->receive_queue, skb);
1452     			if (copied == 0)
1453     				copied = -EFAULT;
1454     			break;
1455     		}
1456     		copied += chunk;
1457     		size -= chunk;
1458     
1459     		/* Mark read part of skb as used */
1460     		if (!(flags & MSG_PEEK)) {
1461     			skb_pull(skb, chunk);
1462     
1463     			/* put the skb back if we didn't use it up.. */
1464     			if (skb->len) {
1465     				IRDA_DEBUG(1, __FUNCTION__ "(), back on q!\n");
1466     				skb_queue_head(&sk->receive_queue, skb);
1467     				break;
1468     			}
1469     
1470     			kfree_skb(skb);			
1471     		} else {
1472     			IRDA_DEBUG(0, __FUNCTION__ "() questionable!?\n");
1473     
1474     			/* put message back and return */
1475     			skb_queue_head(&sk->receive_queue, skb);
1476     			break;
1477     		}
1478     	} while (size);
1479     
1480     	/*
1481     	 *  Check if we have previously stopped IrTTP and we know
1482     	 *  have more free space in our rx_queue. If so tell IrTTP
1483     	 *  to start delivering frames again before our rx_queue gets
1484     	 *  empty
1485     	 */
1486     	if (self->rx_flow == FLOW_STOP) {
1487     		if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1488     			IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1489     			self->rx_flow = FLOW_START;
1490     			irttp_flow_request(self->tsap, FLOW_START);
1491     		}
1492     	}
1493     
1494     	return copied;
1495     }
1496     
1497     /*
1498      * Function irda_sendmsg_dgram (sock, msg, len, scm)
1499      *
1500      *    Send message down to TinyTP for the unreliable sequenced
1501      *    packet service...
1502      *
1503      */
1504     static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
1505     			      int len, struct scm_cookie *scm)
1506     {
1507     	struct sock *sk = sock->sk;
1508     	struct irda_sock *self;
1509     	struct sk_buff *skb;
1510     	unsigned char *asmptr;
1511     	int err;
1512     	
1513     	IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1514     	
1515     	if (msg->msg_flags & ~MSG_DONTWAIT)
1516     		return -EINVAL;
1517     
1518     	if (sk->shutdown & SEND_SHUTDOWN) {
1519     		send_sig(SIGPIPE, current, 0);
1520     		return -EPIPE;
1521     	}
1522     
1523     	if (sk->state != TCP_ESTABLISHED)
1524     		return -ENOTCONN;
1525     
1526     	self = sk->protinfo.irda;
1527     	ASSERT(self != NULL, return -1;);
1528     
1529     	/*  
1530     	 * Check that we don't send out to big frames. This is an unreliable 
1531     	 * service, so we have no fragmentation and no coalescence 
1532     	 */
1533     	if (len > self->max_data_size) {
1534     		IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1535     			   "Chopping frame from %d to %d bytes!\n", len, 
1536     			   self->max_data_size);
1537     		len = self->max_data_size;
1538     	}
1539     
1540     	skb = sock_alloc_send_skb(sk, len + self->max_header_size, 
1541     				  msg->msg_flags & MSG_DONTWAIT, &err);
1542     	if (!skb)
1543     		return -ENOBUFS;
1544     
1545     	skb_reserve(skb, self->max_header_size);
1546     	
1547     	IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1548     	asmptr = skb->h.raw = skb_put(skb, len);
1549     	memcpy_fromiovec(asmptr, msg->msg_iov, len);
1550     
1551     	/* 
1552     	 * Just send the message to TinyTP, and let it deal with possible 
1553     	 * errors. No need to duplicate all that here
1554     	 */
1555     	err = irttp_udata_request(self->tsap, skb);
1556     	if (err) {
1557     		IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1558     		return err;
1559     	}
1560     	return len;
1561     }
1562     
1563     /*
1564      * Function irda_sendmsg_ultra (sock, msg, len, scm)
1565      *
1566      *    Send message down to IrLMP for the unreliable Ultra
1567      *    packet service...
1568      */
1569     #ifdef CONFIG_IRDA_ULTRA
1570     static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
1571     			      int len, struct scm_cookie *scm)
1572     {
1573     	struct sock *sk = sock->sk;
1574     	struct irda_sock *self;
1575     	struct sk_buff *skb;
1576     	unsigned char *asmptr;
1577     	int err;
1578     	
1579     	IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1580     	
1581     	if (msg->msg_flags & ~MSG_DONTWAIT)
1582     		return -EINVAL;
1583     
1584     	if (sk->shutdown & SEND_SHUTDOWN) {
1585     		send_sig(SIGPIPE, current, 0);
1586     		return -EPIPE;
1587     	}
1588     
1589     	self = sk->protinfo.irda;
1590     	ASSERT(self != NULL, return -1;);
1591     
1592     	/*  
1593     	 * Check that we don't send out to big frames. This is an unreliable 
1594     	 * service, so we have no fragmentation and no coalescence 
1595     	 */
1596     	if (len > self->max_data_size) {
1597     		IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1598     			   "Chopping frame from %d to %d bytes!\n", len, 
1599     			   self->max_data_size);
1600     		len = self->max_data_size;
1601     	}
1602     
1603     	skb = sock_alloc_send_skb(sk, len + self->max_header_size, 
1604     				  msg->msg_flags & MSG_DONTWAIT, &err);
1605     	if (!skb)
1606     		return -ENOBUFS;
1607     
1608     	skb_reserve(skb, self->max_header_size);
1609     	
1610     	IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1611     	asmptr = skb->h.raw = skb_put(skb, len);
1612     	memcpy_fromiovec(asmptr, msg->msg_iov, len);
1613     
1614     	err = irlmp_connless_data_request(self->lsap, skb);
1615     	if (err) {
1616     		IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1617     		return err;
1618     	}
1619     	return len;
1620     }
1621     #endif /* CONFIG_IRDA_ULTRA */
1622     
1623     /*
1624      * Function irda_shutdown (sk, how)
1625      *
1626      *    
1627      *
1628      */
1629     static int irda_shutdown(struct socket *sock, int how)
1630     {
1631     	struct irda_sock *self;
1632     	struct sock *sk = sock->sk;
1633     
1634     	self = sk->protinfo.irda;
1635     	ASSERT(self != NULL, return -1;);
1636     
1637     	IRDA_DEBUG(1, __FUNCTION__ "(%p)\n", self);
1638     
1639     	sk->state       = TCP_CLOSE;
1640     	sk->shutdown   |= SEND_SHUTDOWN;
1641     	sk->state_change(sk);
1642     
1643     	if (self->iriap) {
1644     		iriap_close(self->iriap);
1645     		self->iriap = NULL;
1646     	}
1647     
1648     	if (self->tsap) {
1649     		irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1650     		irttp_close_tsap(self->tsap);
1651     		self->tsap = NULL;
1652     	}
1653     
1654     	/* A few cleanup so the socket look as good as new... */
1655     	self->rx_flow = self->tx_flow = FLOW_START;	/* needed ??? */
1656     	self->daddr = DEV_ADDR_ANY;	/* Until we get re-connected */
1657     	self->saddr = 0x0;		/* so IrLMP assign us any link */
1658     
1659             return 0;
1660     }
1661     
1662     /*
1663      * Function irda_poll (file, sock, wait)
1664      *
1665      *    
1666      *
1667      */
1668     static unsigned int irda_poll(struct file * file, struct socket *sock, 
1669     			      poll_table *wait)
1670     {
1671     	struct sock *sk = sock->sk;
1672     	unsigned int mask;
1673     	struct irda_sock *self;
1674     
1675     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
1676     
1677     	self = sk->protinfo.irda;
1678     	poll_wait(file, sk->sleep, wait);
1679     	mask = 0;
1680     
1681     	/* Exceptional events? */
1682     	if (sk->err)
1683     		mask |= POLLERR;
1684     	if (sk->shutdown & RCV_SHUTDOWN) {
1685     		IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1686     		mask |= POLLHUP;
1687     	}
1688     
1689     	/* Readable? */
1690     	if (!skb_queue_empty(&sk->receive_queue)) {
1691     		IRDA_DEBUG(4, "Socket is readable\n");
1692     		mask |= POLLIN | POLLRDNORM;
1693     	}
1694     
1695     	/* Connection-based need to check for termination and startup */
1696     	switch (sk->type) {
1697     	case SOCK_STREAM:
1698     		if (sk->state == TCP_CLOSE) {
1699     			IRDA_DEBUG(0, __FUNCTION__ "(), POLLHUP\n");
1700     			mask |= POLLHUP;
1701     		}
1702     
1703     		if (sk->state == TCP_ESTABLISHED) {
1704     			if ((self->tx_flow == FLOW_START) && 
1705     			    (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE))
1706     			{
1707     				mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1708     			}
1709     		}
1710     		break;
1711     	case SOCK_SEQPACKET:
1712     		if ((self->tx_flow == FLOW_START) && 
1713     		    (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE))
1714     		{	
1715     			mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1716     		}
1717     		break;
1718     	case SOCK_DGRAM:
1719     		if (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE)
1720     			mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1721     		break;
1722     	default:
1723     		break;
1724     	}		
1725     	return mask;
1726     }
1727     
1728     /*
1729      * Function irda_ioctl (sock, cmd, arg)
1730      *
1731      *    
1732      *
1733      */
1734     static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1735     {
1736     	struct sock *sk = sock->sk;
1737     
1738     	IRDA_DEBUG(4, __FUNCTION__ "(), cmd=%#x\n", cmd);
1739     	
1740     	switch (cmd) {
1741     	case TIOCOUTQ: {
1742     		long amount;
1743     		amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1744     		if (amount < 0)
1745     			amount = 0;
1746     		if (put_user(amount, (unsigned int *)arg))
1747     			return -EFAULT;
1748     		return 0;
1749     	}
1750     	
1751     	case TIOCINQ: {
1752     		struct sk_buff *skb;
1753     		long amount = 0L;
1754     		/* These two are safe on a single CPU system as only user tasks fiddle here */
1755     		if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1756     			amount = skb->len;
1757     		if (put_user(amount, (unsigned int *)arg))
1758     			return -EFAULT;
1759     		return 0;
1760     	}
1761     	
1762     	case SIOCGSTAMP:
1763     		if (sk != NULL) {
1764     			if (sk->stamp.tv_sec == 0)
1765     				return -ENOENT;
1766     			if (copy_to_user((void *)arg, &sk->stamp, 
1767     					 sizeof(struct timeval)))
1768     				return -EFAULT;
1769     			return 0;
1770     		}
1771     		return -EINVAL;
1772     		
1773     	case SIOCGIFADDR:
1774     	case SIOCSIFADDR:
1775     	case SIOCGIFDSTADDR:
1776     	case SIOCSIFDSTADDR:
1777     	case SIOCGIFBRDADDR:
1778     	case SIOCSIFBRDADDR:
1779     	case SIOCGIFNETMASK:
1780     	case SIOCSIFNETMASK:
1781     	case SIOCGIFMETRIC:
1782     	case SIOCSIFMETRIC:
1783     		return -EINVAL;		
1784     	default:
1785     		IRDA_DEBUG(1, __FUNCTION__ "(), doing device ioctl!\n");
1786     		return dev_ioctl(cmd, (void *) arg);
1787     	}
1788     
1789     	/*NOTREACHED*/
1790     	return 0;
1791     }
1792     
1793     /*
1794      * Function irda_setsockopt (sock, level, optname, optval, optlen)
1795      *
1796      *    Set some options for the socket
1797      *
1798      */
1799     static int irda_setsockopt(struct socket *sock, int level, int optname, 
1800     			   char *optval, int optlen)
1801     {
1802      	struct sock *sk = sock->sk;
1803     	struct irda_sock *self;
1804     	struct irda_ias_set    *ias_opt;
1805     	struct ias_object      *ias_obj;
1806     	struct ias_attrib *	ias_attr;	/* Attribute in IAS object */
1807     	int opt;
1808     	
1809     	self = sk->protinfo.irda;
1810     	ASSERT(self != NULL, return -1;);
1811     
1812     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
1813     
1814     	if (level != SOL_IRLMP)
1815     		return -ENOPROTOOPT;
1816     		
1817     	switch (optname) {
1818     	case IRLMP_IAS_SET:
1819     		/* The user want to add an attribute to an existing IAS object
1820     		 * (in the IAS database) or to create a new object with this
1821     		 * attribute.
1822     		 * We first query IAS to know if the object exist, and then
1823     		 * create the right attribute...
1824     		 */
1825     
1826     		if (optlen != sizeof(struct irda_ias_set))
1827     			return -EINVAL;
1828     	
1829     		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1830     		if (ias_opt == NULL)
1831     			return -ENOMEM;
1832     
1833     		/* Copy query to the driver. */
1834     		if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1835     			kfree(ias_opt);
1836     		  	return -EFAULT;
1837     		}
1838     
1839     		/* Find the object we target */
1840     		ias_obj = irias_find_object(ias_opt->irda_class_name);
1841     		if(ias_obj == (struct ias_object *) NULL) {
1842     			/* Create a new object */
1843     			ias_obj = irias_new_object(ias_opt->irda_class_name,
1844     						   jiffies);
1845     		}
1846     
1847     		/* Do we have it already ? */
1848     		if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1849     			kfree(ias_opt);
1850     			return -EINVAL;
1851     		}
1852     
1853     		/* Look at the type */
1854     		switch(ias_opt->irda_attrib_type) {
1855     		case IAS_INTEGER:
1856     			/* Add an integer attribute */
1857     			irias_add_integer_attrib(
1858     				ias_obj,
1859     				ias_opt->irda_attrib_name, 
1860     				ias_opt->attribute.irda_attrib_int,
1861     				IAS_USER_ATTR);
1862     			break;
1863     		case IAS_OCT_SEQ:
1864     			/* Check length */
1865     			if(ias_opt->attribute.irda_attrib_octet_seq.len >
1866     			   IAS_MAX_OCTET_STRING) {
1867     				kfree(ias_opt);
1868     				return -EINVAL;
1869     			}
1870     			/* Add an octet sequence attribute */
1871     			irias_add_octseq_attrib(
1872     			      ias_obj,
1873     			      ias_opt->irda_attrib_name, 
1874     			      ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1875     			      ias_opt->attribute.irda_attrib_octet_seq.len,
1876     			      IAS_USER_ATTR);
1877     			break;
1878     		case IAS_STRING:
1879     			/* Should check charset & co */
1880     			/* Check length */
1881     			if(ias_opt->attribute.irda_attrib_string.len >
1882     			   IAS_MAX_STRING) {
1883     				kfree(ias_opt);
1884     				return -EINVAL;
1885     			}
1886     			/* NULL terminate the string (avoid troubles) */
1887     			ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1888     			/* Add a string attribute */
1889     			irias_add_string_attrib(
1890     				ias_obj,
1891     				ias_opt->irda_attrib_name, 
1892     				ias_opt->attribute.irda_attrib_string.string,
1893     				IAS_USER_ATTR);
1894     			break;
1895     		default :
1896     			kfree(ias_opt);
1897     			return -EINVAL;
1898     		}
1899     		irias_insert_object(ias_obj);
1900     		kfree(ias_opt);
1901     		break;
1902     	case IRLMP_IAS_DEL:
1903     		/* The user want to delete an object from our local IAS
1904     		 * database. We just need to query the IAS, check is the
1905     		 * object is not owned by the kernel and delete it.
1906     		 */
1907     
1908     		if (optlen != sizeof(struct irda_ias_set))
1909     			return -EINVAL;
1910     	
1911     		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1912     		if (ias_opt == NULL)
1913     			return -ENOMEM;
1914     	
1915     		/* Copy query to the driver. */
1916     		if (copy_from_user(ias_opt, (char *)optval, optlen)) {
1917     			kfree(ias_opt);
1918     		  	return -EFAULT;
1919     		}
1920     
1921     		/* Find the object we target */
1922     		ias_obj = irias_find_object(ias_opt->irda_class_name);
1923     		if(ias_obj == (struct ias_object *) NULL) {
1924     			kfree(ias_opt);
1925     			return -EINVAL;
1926     		}
1927     
1928     		/* Find the attribute (in the object) we target */
1929     		ias_attr = irias_find_attrib(ias_obj,
1930     					     ias_opt->irda_attrib_name); 
1931     		if(ias_attr == (struct ias_attrib *) NULL) {
1932     			kfree(ias_opt);
1933     			return -EINVAL;
1934     		}
1935     
1936     		/* Check is the user space own the object */
1937     		if(ias_attr->value->owner != IAS_USER_ATTR) {
1938     			IRDA_DEBUG(1, __FUNCTION__ 
1939     				   "(), attempting to delete a kernel attribute\n");
1940     			kfree(ias_opt);
1941     			return -EPERM;
1942     		}
1943     
1944     		/* Remove the attribute (and maybe the object) */
1945     		irias_delete_attrib(ias_obj, ias_attr);
1946     		kfree(ias_opt);
1947     		break;
1948     	case IRLMP_MAX_SDU_SIZE:
1949     		if (optlen < sizeof(int))
1950     			return -EINVAL;
1951     	
1952     		if (get_user(opt, (int *)optval))
1953     			return -EFAULT;
1954     		
1955     		/* Only possible for a seqpacket service (TTP with SAR) */
1956     		if (sk->type != SOCK_SEQPACKET) {
1957     			IRDA_DEBUG(2, __FUNCTION__ 
1958     				   "(), setting max_sdu_size = %d\n", opt);
1959     			self->max_sdu_size_rx = opt;
1960     		} else {
1961     			WARNING(__FUNCTION__ 
1962     				"(), not allowed to set MAXSDUSIZE for this "
1963     				"socket type!\n");
1964     			return -ENOPROTOOPT;
1965     		}
1966     		break;
1967     	case IRLMP_HINTS_SET:
1968     		if (optlen < sizeof(int))
1969     			return -EINVAL;
1970     	
1971     		if (get_user(opt, (int *)optval))
1972     			return -EFAULT;
1973     
1974     		/* Unregister any old registration */
1975     		if (self->skey)
1976     			irlmp_unregister_service(self->skey);
1977     
1978     		self->skey = irlmp_register_service((__u16) opt);
1979     		break;
1980     	case IRLMP_HINT_MASK_SET:
1981     		/* As opposed to the previous case which set the hint bits
1982     		 * that we advertise, this one set the filter we use when
1983     		 * making a discovery (nodes which don't match any hint
1984     		 * bit in the mask are not reported).
1985     		 */
1986     		if (optlen < sizeof(int))
1987     			return -EINVAL;
1988     	
1989     		if (get_user(opt, (int *)optval))
1990     			return -EFAULT;
1991     
1992     		/* Set the new hint mask */
1993     		self->mask = (__u16) opt;
1994     		/* Mask out extension bits */
1995     		self->mask &= 0x7f7f;
1996     		/* Check if no bits */
1997     		if(!self->mask)
1998     			self->mask = 0xFFFF;
1999     
2000     		break;
2001     	default:
2002     		return -ENOPROTOOPT;
2003     	}
2004     	return 0;
2005     }
2006     
2007     /*
2008      * Function irda_extract_ias_value(ias_opt, ias_value)
2009      *
2010      *    Translate internal IAS value structure to the user space representation
2011      *
2012      * The external representation of IAS values, as we exchange them with
2013      * user space program is quite different from the internal representation,
2014      * as stored in the IAS database (because we need a flat structure for
2015      * crossing kernel boundary).
2016      * This function transform the former in the latter. We also check
2017      * that the value type is valid.
2018      */
2019     static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2020     				  struct ias_value *ias_value)
2021     {
2022     	/* Look at the type */
2023     	switch (ias_value->type) {
2024     	case IAS_INTEGER:
2025     		/* Copy the integer */
2026     		ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2027     		break;
2028     	case IAS_OCT_SEQ:
2029     		/* Set length */
2030     		ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2031     		/* Copy over */
2032     		memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2033     		       ias_value->t.oct_seq, ias_value->len);
2034     		break;
2035     	case IAS_STRING:
2036     		/* Set length */
2037     		ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2038     		ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2039     		/* Copy over */
2040     		memcpy(ias_opt->attribute.irda_attrib_string.string,
2041     		       ias_value->t.string, ias_value->len);
2042     		/* NULL terminate the string (avoid troubles) */
2043     		ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2044     		break;
2045     	case IAS_MISSING:
2046     	default :
2047     		return -EINVAL;
2048     	}
2049     	
2050     	/* Copy type over */
2051     	ias_opt->irda_attrib_type = ias_value->type;
2052     	
2053     	return 0;
2054     }
2055     
2056     /*
2057      * Function irda_getsockopt (sock, level, optname, optval, optlen)
2058      *
2059      *    
2060      *
2061      */
2062     static int irda_getsockopt(struct socket *sock, int level, int optname, 
2063     			   char *optval, int *optlen)
2064     {
2065     	struct sock *sk = sock->sk;
2066     	struct irda_sock *self;
2067     	struct irda_device_list list;
2068     	struct irda_device_info *discoveries;
2069     	struct irda_ias_set *	ias_opt;	/* IAS get/query params */
2070     	struct ias_object *	ias_obj;	/* Object in IAS */
2071     	struct ias_attrib *	ias_attr;	/* Attribute in IAS object */
2072     	int daddr = DEV_ADDR_ANY;	/* Dest address for IAS queries */
2073     	int val = 0;
2074     	int len = 0;
2075     	int err;
2076     	int offset, total;
2077     
2078     	self = sk->protinfo.irda;
2079     
2080     	IRDA_DEBUG(2, __FUNCTION__ "(%p)\n", self);
2081     
2082     	if (level != SOL_IRLMP)
2083     		return -ENOPROTOOPT;
2084     
2085     	if (get_user(len, optlen))
2086     		return -EFAULT;
2087     
2088     	if(len < 0)
2089     		return -EINVAL;
2090     		
2091     	switch (optname) {
2092     	case IRLMP_ENUMDEVICES:
2093     		/* Ask lmp for the current discovery log */
2094     		discoveries = irlmp_get_discoveries(&list.len, self->mask);
2095     		/* Check if the we got some results */
2096     		if (discoveries == NULL)
2097     			return -EAGAIN;		/* Didn't find any devices */
2098     		err = 0;
2099     
2100     		/* Write total list length back to client */
2101     		if (copy_to_user(optval, &list, 
2102     				 sizeof(struct irda_device_list) -
2103     				 sizeof(struct irda_device_info)))
2104     			err = -EFAULT;
2105     
2106     		/* Offset to first device entry */
2107     		offset = sizeof(struct irda_device_list) - 
2108     			sizeof(struct irda_device_info);
2109     
2110     		/* Copy the list itself - watch for overflow */
2111     		if(list.len > 2048)
2112     		{
2113     			err = -EINVAL;
2114     			goto bed;
2115     		}
2116     		total = offset + (list.len * sizeof(struct irda_device_info));
2117     		if (total > len)
2118     			total = len;
2119     		if (copy_to_user(optval+offset, discoveries, total - offset))
2120     			err = -EFAULT;
2121     
2122     		/* Write total number of bytes used back to client */
2123     		if (put_user(total, optlen))
2124     			err = -EFAULT;
2125     bed:
2126     		/* Free up our buffer */
2127     		kfree(discoveries);
2128     		if (err)
2129     			return err;
2130     		break;
2131     	case IRLMP_MAX_SDU_SIZE:
2132     		val = self->max_data_size;
2133     		len = sizeof(int);
2134     		if (put_user(len, optlen))
2135     			return -EFAULT;
2136     		
2137     		if (copy_to_user(optval, &val, len))
2138     			return -EFAULT;
2139     		break;
2140     	case IRLMP_IAS_GET:
2141     		/* The user want an object from our local IAS database.
2142     		 * We just need to query the IAS and return the value
2143     		 * that we found */
2144     
2145     		/* Check that the user has allocated the right space for us */
2146     		if (len != sizeof(struct irda_ias_set))
2147     			return -EINVAL;
2148     
2149     		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2150     		if (ias_opt == NULL)
2151     			return -ENOMEM;
2152     
2153     		/* Copy query to the driver. */
2154     		if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2155     			kfree(ias_opt);
2156     		  	return -EFAULT;
2157     		}
2158     
2159     		/* Find the object we target */
2160     		ias_obj = irias_find_object(ias_opt->irda_class_name);
2161     		if(ias_obj == (struct ias_object *) NULL) {
2162     			kfree(ias_opt);
2163     			return -EINVAL;
2164     		}
2165     
2166     		/* Find the attribute (in the object) we target */
2167     		ias_attr = irias_find_attrib(ias_obj,
2168     					     ias_opt->irda_attrib_name); 
2169     		if(ias_attr == (struct ias_attrib *) NULL) {
2170     			kfree(ias_opt);
2171     			return -EINVAL;
2172     		}
2173     
2174     		/* Translate from internal to user structure */
2175     		err = irda_extract_ias_value(ias_opt, ias_attr->value);
2176     		if(err) {
2177     			kfree(ias_opt);
2178     			return err;
2179     		}
2180     
2181     		/* Copy reply to the user */
2182     		if (copy_to_user((char *)optval, (char *) ias_opt,
2183     				 sizeof(struct irda_ias_set))) {
2184     			kfree(ias_opt);
2185     		  	return -EFAULT;
2186     		}
2187     		/* Note : don't need to put optlen, we checked it */
2188     		kfree(ias_opt);
2189     		break;
2190     	case IRLMP_IAS_QUERY:
2191     		/* The user want an object from a remote IAS database.
2192     		 * We need to use IAP to query the remote database and
2193     		 * then wait for the answer to come back. */
2194     
2195     		/* Check that the user has allocated the right space for us */
2196     		if (len != sizeof(struct irda_ias_set))
2197     			return -EINVAL;
2198     
2199     		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2200     		if (ias_opt == NULL)
2201     			return -ENOMEM;
2202     
2203     		/* Copy query to the driver. */
2204     		if (copy_from_user((char *) ias_opt, (char *)optval, len)) {
2205     			kfree(ias_opt);
2206     		  	return -EFAULT;
2207     		}
2208     
2209     		/* At this point, there are two cases...
2210     		 * 1) the socket is connected - that's the easy case, we
2211     		 *	just query the device we are connected to...
2212     		 * 2) the socket is not connected - the user doesn't want
2213     		 *	to connect and/or may not have a valid service name
2214     		 *	(so can't create a fake connection). In this case,
2215     		 *	we assume that the user pass us a valid destination
2216     		 *	address in the requesting structure...
2217     		 */
2218     		if(self->daddr != DEV_ADDR_ANY) {
2219     			/* We are connected - reuse known daddr */
2220     			daddr = self->daddr;
2221     		} else {
2222     			/* We are not connected, we must specify a valid
2223     			 * destination address */
2224     			daddr = ias_opt->daddr;
2225     			if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2226     				kfree(ias_opt);
2227     				return -EINVAL;
2228     			}
2229     		}
2230     
2231     		/* Check that we can proceed with IAP */
2232     		if (self->iriap) {
2233     			WARNING(__FUNCTION__
2234     				"(), busy with a previous query\n");
2235     			kfree(ias_opt);
2236     			return -EBUSY;
2237     		}
2238     
2239     		self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2240     					 irda_getvalue_confirm);
2241     
2242     		/* Treat unexpected signals as disconnect */
2243     		self->errno = -EHOSTUNREACH;
2244     
2245     		/* Query remote LM-IAS */
2246     		iriap_getvaluebyclass_request(self->iriap,
2247     					      self->saddr, daddr,
2248     					      ias_opt->irda_class_name,
2249     					      ias_opt->irda_attrib_name);
2250     		/* Wait for answer (if not already failed) */
2251     		if(self->iriap != NULL)
2252     			interruptible_sleep_on(&self->query_wait);
2253     		/* Check what happened */
2254     		if (self->errno)
2255     		{
2256     			kfree(ias_opt);
2257     			/* Requested object/attribute doesn't exist */
2258     			if((self->errno == IAS_CLASS_UNKNOWN) ||
2259     			   (self->errno == IAS_ATTRIB_UNKNOWN))
2260     				return (-EADDRNOTAVAIL);
2261     			else
2262     				return (-EHOSTUNREACH);
2263     		}
2264     
2265     		/* Translate from internal to user structure */
2266     		err = irda_extract_ias_value(ias_opt, self->ias_result);
2267     		if (self->ias_result)
2268     			irias_delete_value(self->ias_result);
2269     		if (err) {
2270     			kfree(ias_opt);
2271     			return err;
2272     		}
2273     
2274     		/* Copy reply to the user */
2275     		if (copy_to_user((char *)optval, (char *) ias_opt,
2276     				 sizeof(struct irda_ias_set))) {
2277     			kfree(ias_opt);
2278     		  	return -EFAULT;
2279     		}
2280     		/* Note : don't need to put optlen, we checked it */
2281     		kfree(ias_opt);
2282     		break;
2283     	case IRLMP_WAITDEVICE:
2284     		/* This function is just another way of seeing life ;-)
2285     		 * IRLMP_ENUMDEVICES assumes that you have a static network,
2286     		 * and that you just want to pick one of the devices present.
2287     		 * On the other hand, in here we assume that no device is
2288     		 * present and that at some point in the future a device will
2289     		 * come into range. When this device arrive, we just wake
2290     		 * up the caller, so that he has time to connect to it before
2291     		 * the device goes away...
2292     		 * Note : once the node has been discovered for more than a
2293     		 * few second, it won't trigger this function, unless it
2294     		 * goes away and come back changes its hint bits (so we
2295     		 * might call it IRLMP_WAITNEWDEVICE).
2296     		 */
2297     
2298     		/* Check that the user is passing us an int */
2299     		if (len != sizeof(int))
2300     			return -EINVAL;
2301     		/* Get timeout in ms (max time we block the caller) */
2302     		if (get_user(val, (int *)optval))
2303     			return -EFAULT;
2304     
2305     		/* Tell IrLMP we want to be notified */
2306     		irlmp_update_client(self->ckey, self->mask,
2307     				    irda_selective_discovery_indication,
2308     				    NULL, (void *) self);
2309     		
2310     		/* Do some discovery (and also return cached results) */
2311     		irlmp_discovery_request(self->nslots);
2312     		
2313     		/* Wait until a node is discovered */
2314     		if (!self->cachediscovery) {
2315     			IRDA_DEBUG(1, __FUNCTION__ 
2316     				   "(), nothing discovered yet, going to sleep...\n");
2317     
2318     			/* Set watchdog timer to expire in <val> ms. */
2319     			self->watchdog.function = irda_discovery_timeout;
2320     			self->watchdog.data = (unsigned long) self;
2321     			self->watchdog.expires = jiffies + (val * HZ/1000);
2322     			add_timer(&(self->watchdog));
2323     
2324     			/* Wait for IR-LMP to call us back */
2325     			interruptible_sleep_on(&self->query_wait);
2326     
2327     			/* If watchdog is still activated, kill it! */
2328     			if(timer_pending(&(self->watchdog)))
2329     				del_timer(&(self->watchdog));
2330     
2331     			IRDA_DEBUG(1, __FUNCTION__ 
2332     				   "(), ...waking up !\n");
2333     		}
2334     		else
2335     			IRDA_DEBUG(1, __FUNCTION__ 
2336     				   "(), found immediately !\n");
2337     
2338     		/* Tell IrLMP that we have been notified */
2339     		irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
2340     
2341     		/* Check if the we got some results */
2342     		if (!self->cachediscovery)
2343     			return -EAGAIN;		/* Didn't find any devices */
2344     		/* Cleanup */
2345     		self->cachediscovery = NULL;
2346     
2347     		/* Note : We don't return anything to the user.
2348     		 * We could return the device that triggered the wake up,
2349     		 * but it's probably better to force the user to query
2350     		 * the whole discovery log and let him pick one device...
2351     		 */
2352     		break;
2353     	default:
2354     		return -ENOPROTOOPT;
2355     	}
2356     	
2357     	return 0;
2358     }
2359     
2360     static struct net_proto_family irda_family_ops =
2361     {
2362     	PF_IRDA,
2363     	irda_create
2364     };
2365     
2366     static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2367     	family:		PF_IRDA,
2368     	
2369     	release:	irda_release,
2370     	bind:		irda_bind,
2371     	connect:	irda_connect,
2372     	socketpair:	sock_no_socketpair,
2373     	accept:		irda_accept,
2374     	getname:	irda_getname,
2375     	poll:		irda_poll,
2376     	ioctl:		irda_ioctl,
2377     	listen:		irda_listen,
2378     	shutdown:	irda_shutdown,
2379     	setsockopt:	irda_setsockopt,
2380     	getsockopt:	irda_getsockopt,
2381     	sendmsg:	irda_sendmsg,
2382     	recvmsg:	irda_recvmsg_stream,
2383     	mmap:		sock_no_mmap,
2384     	sendpage:	sock_no_sendpage,
2385     };
2386     
2387     static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2388     	family:		PF_IRDA,
2389     	
2390     	release:	irda_release,
2391     	bind:		irda_bind,
2392     	connect:	irda_connect,
2393     	socketpair:	sock_no_socketpair,
2394     	accept:		irda_accept,
2395     	getname:	irda_getname,
2396     	poll:		datagram_poll,
2397     	ioctl:		irda_ioctl,
2398     	listen:		irda_listen,
2399     	shutdown:	irda_shutdown,
2400     	setsockopt:	irda_setsockopt,
2401     	getsockopt:	irda_getsockopt,
2402     	sendmsg:	irda_sendmsg,
2403     	recvmsg:	irda_recvmsg_dgram,
2404     	mmap:		sock_no_mmap,
2405     	sendpage:	sock_no_sendpage,
2406     };
2407     
2408     static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2409     	family:		PF_IRDA,
2410            
2411     	release:	irda_release,
2412     	bind:		irda_bind,
2413     	connect:	irda_connect,
2414     	socketpair:	sock_no_socketpair,
2415     	accept:		irda_accept,
2416     	getname:	irda_getname,
2417     	poll:		datagram_poll,
2418     	ioctl:		irda_ioctl,
2419     	listen:		irda_listen,
2420     	shutdown:	irda_shutdown,
2421     	setsockopt:	irda_setsockopt,
2422     	getsockopt:	irda_getsockopt,
2423     	sendmsg:	irda_sendmsg_dgram,
2424     	recvmsg:	irda_recvmsg_dgram,
2425     	mmap:		sock_no_mmap,
2426     	sendpage:	sock_no_sendpage,
2427     };
2428     
2429     #ifdef CONFIG_IRDA_ULTRA
2430     static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2431     	family:		PF_IRDA,
2432            
2433     	release:	irda_release,
2434     	bind:		irda_bind,
2435     	connect:	sock_no_connect,
2436     	socketpair:	sock_no_socketpair,
2437     	accept:		sock_no_accept,
2438     	getname:	irda_getname,
2439     	poll:		datagram_poll,
2440     	ioctl:		irda_ioctl,
2441     	listen:		sock_no_listen,
2442     	shutdown:	irda_shutdown,
2443     	setsockopt:	irda_setsockopt,
2444     	getsockopt:	irda_getsockopt,
2445     	sendmsg:	irda_sendmsg_ultra,
2446     	recvmsg:	irda_recvmsg_dgram,
2447     	mmap:		sock_no_mmap,
2448     	sendpage:	sock_no_sendpage,
2449     };
2450     #endif /* CONFIG_IRDA_ULTRA */
2451     
2452     #include <linux/smp_lock.h>
2453     SOCKOPS_WRAP(irda_stream, PF_IRDA);
2454     SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2455     SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2456     #ifdef CONFIG_IRDA_ULTRA
2457     SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2458     #endif /* CONFIG_IRDA_ULTRA */
2459     
2460     /*
2461      * Function irda_device_event (this, event, ptr)
2462      *
2463      *    Called when a device is taken up or down
2464      *
2465      */
2466     static int irda_device_event(struct notifier_block *this, unsigned long event,
2467     			     void *ptr)
2468     {
2469     	struct net_device *dev = (struct net_device *) ptr;
2470     	
2471             /* Reject non IrDA devices */
2472     	if (dev->type != ARPHRD_IRDA) 
2473     		return NOTIFY_DONE;
2474     	
2475             switch (event) {
2476     	case NETDEV_UP:
2477     		IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_UP\n");
2478     		/* irda_dev_device_up(dev); */
2479     		break;
2480     	case NETDEV_DOWN:
2481     		IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_DOWN\n");
2482     		/* irda_kill_by_device(dev); */
2483     		/* irda_rt_device_down(dev); */
2484     		/* irda_dev_device_down(dev); */
2485     		break;
2486     	default:
2487     		break;
2488             }
2489     
2490             return NOTIFY_DONE;
2491     }
2492     
2493     static struct packet_type irda_packet_type = 
2494     {
2495     	0,	/* MUTTER ntohs(ETH_P_IRDA),*/
2496     	NULL,
2497     	irlap_driver_rcv,
2498     	NULL,
2499     	NULL,
2500     };
2501     
2502     static struct notifier_block irda_dev_notifier = {
2503     	irda_device_event,
2504     	NULL,
2505     	0
2506     };
2507     
2508     /*
2509      * Function irda_proc_modcount (inode, fill)
2510      *
2511      *    Use by the proc file system functions to prevent the irda module
2512      *    being removed while the use is standing in the net/irda directory
2513      */
2514     void irda_proc_modcount(struct inode *inode, int fill)
2515     {
2516     #ifdef MODULE
2517     #ifdef CONFIG_PROC_FS
2518     	if (fill)
2519     		MOD_INC_USE_COUNT;
2520     	else
2521     		MOD_DEC_USE_COUNT;
2522     #endif /* CONFIG_PROC_FS */
2523     #endif /* MODULE */
2524     }
2525     
2526     /*
2527      * Function irda_proto_init (pro)
2528      *
2529      *    Initialize IrDA protocol layer
2530      *
2531      */
2532     int __init irda_proto_init(void)
2533     {
2534     	sock_register(&irda_family_ops);
2535     
2536     	irda_packet_type.type = htons(ETH_P_IRDA);
2537             dev_add_pack(&irda_packet_type);
2538     
2539     	register_netdevice_notifier(&irda_dev_notifier);
2540     
2541     	irda_init();
2542     #ifdef MODULE
2543      	irda_device_init();  /* Called by init/main.c when non-modular */
2544     #endif
2545     	return 0;
2546     }
2547     #ifdef MODULE
2548     module_init(irda_proto_init);	/* If non-module, called from init/main.c */
2549     #endif
2550     
2551     /*
2552      * Function irda_proto_cleanup (void)
2553      *
2554      *    Remove IrDA protocol layer
2555      *
2556      */
2557     #ifdef MODULE
2558     void irda_proto_cleanup(void)
2559     {
2560     	irda_packet_type.type = htons(ETH_P_IRDA);
2561             dev_remove_pack(&irda_packet_type);
2562     
2563             unregister_netdevice_notifier(&irda_dev_notifier);
2564     	
2565     	sock_unregister(PF_IRDA);
2566     	irda_cleanup();
2567     	
2568             return;
2569     }
2570     module_exit(irda_proto_cleanup);
2571      
2572     MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2573     MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem"); 
2574     #ifdef CONFIG_IRDA_DEBUG
2575     MODULE_PARM(irda_debug, "1l");
2576     #endif
2577     #endif /* MODULE */
2578