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