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