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