af_irda.c 67.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*********************************************************************
 *
 * Filename:      af_irda.c
 * Version:       0.9
 * Description:   IrDA sockets implementation
 * Status:        Stable
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sun May 31 10:12:43 1998
 * Modified at:   Sat Dec 25 21:10:23 1999
 * Modified by:   Dag Brattli <dag@brattli.net>
 * Sources:       af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
 *
 *     Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
 *     Copyright (c) 1999-2003 Jean Tourrilhes <jt@hpl.hp.com>
 *     All Rights Reserved.
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *     MA 02111-1307 USA
 *
 *     Linux-IrDA now supports four different types of IrDA sockets:
 *
 *     o SOCK_STREAM:    TinyTP connections with SAR disabled. The
 *                       max SDU size is 0 for conn. of this type
 *     o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may
 *                       fragment the messages, but will preserve
 *                       the message boundaries
 *     o SOCK_DGRAM:     IRDAPROTO_UNITDATA: TinyTP connections with Unitdata
 *                       (unreliable) transfers
 *                       IRDAPROTO_ULTRA: Connectionless and unreliable data
 *
 ********************************************************************/

45
#include <linux/capability.h>
L
Linus Torvalds 已提交
46 47 48 49
#include <linux/module.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/sockios.h>
50
#include <linux/slab.h>
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59
#include <linux/init.h>
#include <linux/net.h>
#include <linux/irda.h>
#include <linux/poll.h>

#include <asm/ioctls.h>		/* TIOCOUTQ, TIOCINQ */
#include <asm/uaccess.h>

#include <net/sock.h>
60
#include <net/tcp_states.h>
L
Linus Torvalds 已提交
61 62 63

#include <net/irda/af_irda.h>

64
static int irda_create(struct net *net, struct socket *sock, int protocol, int kern);
L
Linus Torvalds 已提交
65

66 67 68
static const struct proto_ops irda_stream_ops;
static const struct proto_ops irda_seqpacket_ops;
static const struct proto_ops irda_dgram_ops;
L
Linus Torvalds 已提交
69 70

#ifdef CONFIG_IRDA_ULTRA
71
static const struct proto_ops irda_ultra_ops;
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#define ULTRA_MAX_DATA 382
#endif /* CONFIG_IRDA_ULTRA */

#define IRDA_MAX_HEADER (TTP_MAX_HEADER)

/*
 * Function irda_data_indication (instance, sap, skb)
 *
 *    Received some data from TinyTP. Just queue it on the receive queue
 *
 */
static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
{
	struct irda_sock *self;
	struct sock *sk;
	int err;

89
	IRDA_DEBUG(3, "%s()\n", __func__);
L
Linus Torvalds 已提交
90 91 92 93 94 95

	self = instance;
	sk = instance;

	err = sock_queue_rcv_skb(sk, skb);
	if (err) {
96
		IRDA_DEBUG(1, "%s(), error: no more mem!\n", __func__);
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		self->rx_flow = FLOW_STOP;

		/* When we return error, TTP will need to requeue the skb */
		return err;
	}

	return 0;
}

/*
 * Function irda_disconnect_indication (instance, sap, reason, skb)
 *
 *    Connection has been closed. Check reason to find out why
 *
 */
static void irda_disconnect_indication(void *instance, void *sap,
				       LM_REASON reason, struct sk_buff *skb)
{
	struct irda_sock *self;
	struct sock *sk;

	self = instance;

120
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128

	/* Don't care about it, but let's not leak it */
	if(skb)
		dev_kfree_skb(skb);

	sk = instance;
	if (sk == NULL) {
		IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n",
129
			   __func__, self);
L
Linus Torvalds 已提交
130 131 132 133
		return;
	}

	/* Prevent race conditions with irda_release() and irda_shutdown() */
134
	bh_lock_sock(sk);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) {
		sk->sk_state     = TCP_CLOSE;
		sk->sk_shutdown |= SEND_SHUTDOWN;

		sk->sk_state_change(sk);

		/* Close our TSAP.
		 * If we leave it open, IrLMP put it back into the list of
		 * unconnected LSAPs. The problem is that any incoming request
		 * can then be matched to this socket (and it will be, because
		 * it is at the head of the list). This would prevent any
		 * listening socket waiting on the same TSAP to get those
		 * requests. Some apps forget to close sockets, or hang to it
		 * a bit too long, so we may stay in this dead state long
		 * enough to be noticed...
		 * Note : all socket function do check sk->sk_state, so we are
		 * safe...
		 * Jean II
		 */
		if (self->tsap) {
			irttp_close_tsap(self->tsap);
			self->tsap = NULL;
		}
158
	}
159
	bh_unlock_sock(sk);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

	/* Note : once we are there, there is not much you want to do
	 * with the socket anymore, apart from closing it.
	 * For example, bind() and connect() won't reset sk->sk_err,
	 * sk->sk_shutdown and sk->sk_flags to valid values...
	 * Jean II
	 */
}

/*
 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
 *
 *    Connections has been confirmed by the remote device
 *
 */
static void irda_connect_confirm(void *instance, void *sap,
				 struct qos_info *qos,
				 __u32 max_sdu_size, __u8 max_header_size,
				 struct sk_buff *skb)
{
	struct irda_sock *self;
	struct sock *sk;

	self = instance;

185
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

	sk = instance;
	if (sk == NULL) {
		dev_kfree_skb(skb);
		return;
	}

	dev_kfree_skb(skb);
	// Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb);

	/* How much header space do we need to reserve */
	self->max_header_size = max_header_size;

	/* IrTTP max SDU size in transmit direction */
	self->max_sdu_size_tx = max_sdu_size;

	/* Find out what the largest chunk of data that we can transmit is */
	switch (sk->sk_type) {
	case SOCK_STREAM:
		if (max_sdu_size != 0) {
			IRDA_ERROR("%s: max_sdu_size must be 0\n",
207
				   __func__);
L
Linus Torvalds 已提交
208 209 210 211 212 213 214
			return;
		}
		self->max_data_size = irttp_get_max_seg_size(self->tsap);
		break;
	case SOCK_SEQPACKET:
		if (max_sdu_size == 0) {
			IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
215
				   __func__);
L
Linus Torvalds 已提交
216 217 218 219 220 221
			return;
		}
		self->max_data_size = max_sdu_size;
		break;
	default:
		self->max_data_size = irttp_get_max_seg_size(self->tsap);
222
	}
L
Linus Torvalds 已提交
223

224
	IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		   self->max_data_size);

	memcpy(&self->qos_tx, qos, sizeof(struct qos_info));

	/* We are now connected! */
	sk->sk_state = TCP_ESTABLISHED;
	sk->sk_state_change(sk);
}

/*
 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
 *
 *    Incoming connection
 *
 */
static void irda_connect_indication(void *instance, void *sap,
				    struct qos_info *qos, __u32 max_sdu_size,
				    __u8 max_header_size, struct sk_buff *skb)
{
	struct irda_sock *self;
	struct sock *sk;

	self = instance;

249
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

	sk = instance;
	if (sk == NULL) {
		dev_kfree_skb(skb);
		return;
	}

	/* How much header space do we need to reserve */
	self->max_header_size = max_header_size;

	/* IrTTP max SDU size in transmit direction */
	self->max_sdu_size_tx = max_sdu_size;

	/* Find out what the largest chunk of data that we can transmit is */
	switch (sk->sk_type) {
	case SOCK_STREAM:
		if (max_sdu_size != 0) {
			IRDA_ERROR("%s: max_sdu_size must be 0\n",
268
				   __func__);
L
Linus Torvalds 已提交
269 270 271 272 273 274 275 276
			kfree_skb(skb);
			return;
		}
		self->max_data_size = irttp_get_max_seg_size(self->tsap);
		break;
	case SOCK_SEQPACKET:
		if (max_sdu_size == 0) {
			IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
277
				   __func__);
L
Linus Torvalds 已提交
278 279 280 281 282 283 284
			kfree_skb(skb);
			return;
		}
		self->max_data_size = max_sdu_size;
		break;
	default:
		self->max_data_size = irttp_get_max_seg_size(self->tsap);
285
	}
L
Linus Torvalds 已提交
286

287
	IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		   self->max_data_size);

	memcpy(&self->qos_tx, qos, sizeof(struct qos_info));

	skb_queue_tail(&sk->sk_receive_queue, skb);
	sk->sk_state_change(sk);
}

/*
 * Function irda_connect_response (handle)
 *
 *    Accept incoming connection
 *
 */
static void irda_connect_response(struct irda_sock *self)
{
	struct sk_buff *skb;

306
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
307

308
	skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER, GFP_KERNEL);
L
Linus Torvalds 已提交
309 310
	if (skb == NULL) {
		IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n",
311
			   __func__);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		return;
	}

	/* Reserve space for MUX_CONTROL and LAP header */
	skb_reserve(skb, IRDA_MAX_HEADER);

	irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
}

/*
 * Function irda_flow_indication (instance, sap, flow)
 *
 *    Used by TinyTP to tell us if it can accept more data or not
 *
 */
static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
{
	struct irda_sock *self;
	struct sock *sk;

332
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
333 334 335

	self = instance;
	sk = instance;
336
	BUG_ON(sk == NULL);
L
Linus Torvalds 已提交
337 338 339 340

	switch (flow) {
	case FLOW_STOP:
		IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n",
341
			   __func__);
L
Linus Torvalds 已提交
342 343 344 345 346
		self->tx_flow = flow;
		break;
	case FLOW_START:
		self->tx_flow = flow;
		IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n",
347
			   __func__);
E
Eric Dumazet 已提交
348
		wake_up_interruptible(sk_sleep(sk));
L
Linus Torvalds 已提交
349 350
		break;
	default:
351
		IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __func__);
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
		/* Unknown flow command, better stop */
		self->tx_flow = flow;
		break;
	}
}

/*
 * Function irda_getvalue_confirm (obj_id, value, priv)
 *
 *    Got answer from remote LM-IAS, just pass object to requester...
 *
 * Note : duplicate from above, but we need our own version that
 * doesn't touch the dtsap_sel and save the full value structure...
 */
static void irda_getvalue_confirm(int result, __u16 obj_id,
				  struct ias_value *value, void *priv)
{
	struct irda_sock *self;

J
Joe Perches 已提交
371
	self = priv;
L
Linus Torvalds 已提交
372
	if (!self) {
373
		IRDA_WARNING("%s: lost myself!\n", __func__);
L
Linus Torvalds 已提交
374 375 376
		return;
	}

377
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384

	/* We probably don't need to make any more queries */
	iriap_close(self->iriap);
	self->iriap = NULL;

	/* Check if request succeeded */
	if (result != IAS_SUCCESS) {
385
		IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __func__,
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
			   result);

		self->errno = result;	/* We really need it later */

		/* Wake up any processes waiting for result */
		wake_up_interruptible(&self->query_wait);

		return;
	}

	/* Pass the object to the caller (so the caller must delete it) */
	self->ias_result = value;
	self->errno = 0;

	/* Wake up any processes waiting for result */
	wake_up_interruptible(&self->query_wait);
}

/*
 * Function irda_selective_discovery_indication (discovery)
 *
 *    Got a selective discovery indication from IrLMP.
 *
 * IrLMP is telling us that this node is new and matching our hint bit
 * filter. Wake up any process waiting for answer...
 */
static void irda_selective_discovery_indication(discinfo_t *discovery,
						DISCOVERY_MODE mode,
						void *priv)
{
	struct irda_sock *self;

418
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
419

J
Joe Perches 已提交
420
	self = priv;
L
Linus Torvalds 已提交
421
	if (!self) {
422
		IRDA_WARNING("%s: lost myself!\n", __func__);
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		return;
	}

	/* Pass parameter to the caller */
	self->cachedaddr = discovery->daddr;

	/* Wake up process if its waiting for device to be discovered */
	wake_up_interruptible(&self->query_wait);
}

/*
 * Function irda_discovery_timeout (priv)
 *
 *    Timeout in the selective discovery process
 *
 * We were waiting for a node to be discovered, but nothing has come up
 * so far. Wake up the user and tell him that we failed...
 */
static void irda_discovery_timeout(u_long priv)
{
	struct irda_sock *self;

445
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
446 447

	self = (struct irda_sock *) priv;
448
	BUG_ON(self == NULL);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	/* Nothing for the caller */
	self->cachelog = NULL;
	self->cachedaddr = 0;
	self->errno = -ETIME;

	/* Wake up process if its still waiting... */
	wake_up_interruptible(&self->query_wait);
}

/*
 * Function irda_open_tsap (self)
 *
 *    Open local Transport Service Access Point (TSAP)
 *
 */
static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
{
	notify_t notify;

	if (self->tsap) {
D
Dave Jones 已提交
470
		IRDA_DEBUG(0, "%s: busy!\n", __func__);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
		return -EBUSY;
	}

	/* Initialize callbacks to be used by the IrDA stack */
	irda_notify_init(&notify);
	notify.connect_confirm       = irda_connect_confirm;
	notify.connect_indication    = irda_connect_indication;
	notify.disconnect_indication = irda_disconnect_indication;
	notify.data_indication       = irda_data_indication;
	notify.udata_indication	     = irda_data_indication;
	notify.flow_indication       = irda_flow_indication;
	notify.instance = self;
	strncpy(notify.name, name, NOTIFY_MAX_NAME);

	self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
				     &notify);
	if (self->tsap == NULL) {
		IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n",
489
			   __func__);
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
		return -ENOMEM;
	}
	/* Remember which TSAP selector we actually got */
	self->stsap_sel = self->tsap->stsap_sel;

	return 0;
}

/*
 * Function irda_open_lsap (self)
 *
 *    Open local Link Service Access Point (LSAP). Used for opening Ultra
 *    sockets
 */
#ifdef CONFIG_IRDA_ULTRA
static int irda_open_lsap(struct irda_sock *self, int pid)
{
	notify_t notify;

	if (self->lsap) {
510
		IRDA_WARNING("%s(), busy!\n", __func__);
L
Linus Torvalds 已提交
511 512 513 514 515 516 517 518 519 520 521
		return -EBUSY;
	}

	/* Initialize callbacks to be used by the IrDA stack */
	irda_notify_init(&notify);
	notify.udata_indication	= irda_data_indication;
	notify.instance = self;
	strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);

	self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);
	if (self->lsap == NULL) {
522
		IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __func__);
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
		return -ENOMEM;
	}

	return 0;
}
#endif /* CONFIG_IRDA_ULTRA */

/*
 * Function irda_find_lsap_sel (self, name)
 *
 *    Try to lookup LSAP selector in remote LM-IAS
 *
 * Basically, we start a IAP query, and then go to sleep. When the query
 * return, irda_getvalue_confirm will wake us up, and we can examine the
 * result of the query...
 * Note that in some case, the query fail even before we go to sleep,
 * creating some races...
 */
static int irda_find_lsap_sel(struct irda_sock *self, char *name)
{
543
	IRDA_DEBUG(2, "%s(%p, %s)\n", __func__, self, name);
L
Linus Torvalds 已提交
544 545 546

	if (self->iriap) {
		IRDA_WARNING("%s(): busy with a previous query\n",
547
			     __func__);
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		return -EBUSY;
	}

	self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
				 irda_getvalue_confirm);
	if(self->iriap == NULL)
		return -ENOMEM;

	/* Treat unexpected wakeup as disconnect */
	self->errno = -EHOSTUNREACH;

	/* Query remote LM-IAS */
	iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
				      name, "IrDA:TinyTP:LsapSel");

	/* Wait for answer, if not yet finished (or failed) */
	if (wait_event_interruptible(self->query_wait, (self->iriap==NULL)))
		/* Treat signals as disconnect */
		return -EHOSTUNREACH;

	/* Check what happened */
	if (self->errno)
	{
		/* Requested object/attribute doesn't exist */
		if((self->errno == IAS_CLASS_UNKNOWN) ||
		   (self->errno == IAS_ATTRIB_UNKNOWN))
E
Eric Dumazet 已提交
574
			return -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
575
		else
E
Eric Dumazet 已提交
576
			return -EHOSTUNREACH;
L
Linus Torvalds 已提交
577 578 579 580 581 582
	}

	/* Get the remote TSAP selector */
	switch (self->ias_result->type) {
	case IAS_INTEGER:
		IRDA_DEBUG(4, "%s() int=%d\n",
583
			   __func__, self->ias_result->t.integer);
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591

		if (self->ias_result->t.integer != -1)
			self->dtsap_sel = self->ias_result->t.integer;
		else
			self->dtsap_sel = 0;
		break;
	default:
		self->dtsap_sel = 0;
592
		IRDA_DEBUG(0, "%s(), bad type!\n", __func__);
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
		break;
	}
	if (self->ias_result)
		irias_delete_value(self->ias_result);

	if (self->dtsap_sel)
		return 0;

	return -EADDRNOTAVAIL;
}

/*
 * Function irda_discover_daddr_and_lsap_sel (self, name)
 *
 *    This try to find a device with the requested service.
 *
 * It basically look into the discovery log. For each address in the list,
 * it queries the LM-IAS of the device to find if this device offer
 * the requested service.
 * If there is more than one node supporting the service, we complain
 * to the user (it should move devices around).
 * The, we set both the destination address and the lsap selector to point
 * on the service on the unique device we have found.
 *
 * Note : this function fails if there is more than one device in range,
 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
 * Moreover, we would need to wait the LAP disconnection...
 */
static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
{
	discinfo_t *discoveries;	/* Copy of the discovery log */
	int	number;			/* Number of nodes in the log */
	int	i;
	int	err = -ENETUNREACH;
	__u32	daddr = DEV_ADDR_ANY;	/* Address we found the service on */
	__u8	dtsap_sel = 0x0;	/* TSAP associated with it */

630
	IRDA_DEBUG(2, "%s(), name=%s\n", __func__, name);
L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

	/* Ask lmp for the current discovery log
	 * Note : we have to use irlmp_get_discoveries(), as opposed
	 * to play with the cachelog directly, because while we are
	 * making our ias query, le log might change... */
	discoveries = irlmp_get_discoveries(&number, self->mask.word,
					    self->nslots);
	/* Check if the we got some results */
	if (discoveries == NULL)
		return -ENETUNREACH;	/* No nodes discovered */

	/*
	 * Now, check all discovered devices (if any), and connect
	 * client only about the services that the client is
	 * interested in...
	 */
	for(i = 0; i < number; i++) {
		/* Try the address in the log */
		self->daddr = discoveries[i].daddr;
		self->saddr = 0x0;
		IRDA_DEBUG(1, "%s(), trying daddr = %08x\n",
652
			   __func__, self->daddr);
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660

		/* Query remote LM-IAS for this service */
		err = irda_find_lsap_sel(self, name);
		switch (err) {
		case 0:
			/* We found the requested service */
			if(daddr != DEV_ADDR_ANY) {
				IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
661
					   __func__, name);
L
Linus Torvalds 已提交
662 663
				self->daddr = DEV_ADDR_ANY;
				kfree(discoveries);
E
Eric Dumazet 已提交
664
				return -ENOTUNIQ;
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672 673 674
			}
			/* First time we found that one, save it ! */
			daddr = self->daddr;
			dtsap_sel = self->dtsap_sel;
			break;
		case -EADDRNOTAVAIL:
			/* Requested service simply doesn't exist on this node */
			break;
		default:
			/* Something bad did happen :-( */
675
			IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __func__);
L
Linus Torvalds 已提交
676 677
			self->daddr = DEV_ADDR_ANY;
			kfree(discoveries);
E
Eric Dumazet 已提交
678
			return -EHOSTUNREACH;
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687
			break;
		}
	}
	/* Cleanup our copy of the discovery log */
	kfree(discoveries);

	/* Check out what we found */
	if(daddr == DEV_ADDR_ANY) {
		IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
688
			   __func__, name);
L
Linus Torvalds 已提交
689
		self->daddr = DEV_ADDR_ANY;
E
Eric Dumazet 已提交
690
		return -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
691 692 693 694 695 696 697 698
	}

	/* Revert back to discovered device & service */
	self->daddr = daddr;
	self->saddr = 0x0;
	self->dtsap_sel = dtsap_sel;

	IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
699
		   __func__, name, self->daddr);
L
Linus Torvalds 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

	return 0;
}

/*
 * Function irda_getname (sock, uaddr, uaddr_len, peer)
 *
 *    Return the our own, or peers socket address (sockaddr_irda)
 *
 */
static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
			int *uaddr_len, int peer)
{
	struct sockaddr_irda saddr;
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);

E
Eric Dumazet 已提交
717
	memset(&saddr, 0, sizeof(saddr));
L
Linus Torvalds 已提交
718 719
	if (peer) {
		if (sk->sk_state != TCP_ESTABLISHED)
720
			return -ENOTCONN;
L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728 729 730

		saddr.sir_family = AF_IRDA;
		saddr.sir_lsap_sel = self->dtsap_sel;
		saddr.sir_addr = self->daddr;
	} else {
		saddr.sir_family = AF_IRDA;
		saddr.sir_lsap_sel = self->stsap_sel;
		saddr.sir_addr = self->saddr;
	}

731 732
	IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __func__, saddr.sir_lsap_sel);
	IRDA_DEBUG(1, "%s(), addr = %08x\n", __func__, saddr.sir_addr);
L
Linus Torvalds 已提交
733 734 735 736

	/* uaddr_len come to us uninitialised */
	*uaddr_len = sizeof (struct sockaddr_irda);
	memcpy(uaddr, &saddr, *uaddr_len);
737 738

	return 0;
L
Linus Torvalds 已提交
739 740 741 742 743 744 745 746 747 748 749
}

/*
 * Function irda_listen (sock, backlog)
 *
 *    Just move to the listen state
 *
 */
static int irda_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
A
Arnd Bergmann 已提交
750
	int err = -EOPNOTSUPP;
L
Linus Torvalds 已提交
751

752
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
753

754 755
	lock_sock(sk);

L
Linus Torvalds 已提交
756 757
	if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
	    (sk->sk_type != SOCK_DGRAM))
A
Arnd Bergmann 已提交
758
		goto out;
L
Linus Torvalds 已提交
759 760 761 762 763

	if (sk->sk_state != TCP_LISTEN) {
		sk->sk_max_ack_backlog = backlog;
		sk->sk_state           = TCP_LISTEN;

A
Arnd Bergmann 已提交
764
		err = 0;
L
Linus Torvalds 已提交
765
	}
A
Arnd Bergmann 已提交
766
out:
767
	release_sock(sk);
L
Linus Torvalds 已提交
768

A
Arnd Bergmann 已提交
769
	return err;
L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
}

/*
 * Function irda_bind (sock, uaddr, addr_len)
 *
 *    Used by servers to register their well known TSAP
 *
 */
static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
	struct sock *sk = sock->sk;
	struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
	struct irda_sock *self = irda_sk(sk);
	int err;

785
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
786 787 788 789

	if (addr_len != sizeof(struct sockaddr_irda))
		return -EINVAL;

790
	lock_sock(sk);
L
Linus Torvalds 已提交
791 792 793 794 795
#ifdef CONFIG_IRDA_ULTRA
	/* Special care for Ultra sockets */
	if ((sk->sk_type == SOCK_DGRAM) &&
	    (sk->sk_protocol == IRDAPROTO_ULTRA)) {
		self->pid = addr->sir_lsap_sel;
A
Arnd Bergmann 已提交
796
		err = -EOPNOTSUPP;
L
Linus Torvalds 已提交
797
		if (self->pid & 0x80) {
798
			IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
A
Arnd Bergmann 已提交
799
			goto out;
L
Linus Torvalds 已提交
800 801 802
		}
		err = irda_open_lsap(self, self->pid);
		if (err < 0)
A
Arnd Bergmann 已提交
803
			goto out;
L
Linus Torvalds 已提交
804 805 806 807

		/* Pretend we are connected */
		sock->state = SS_CONNECTED;
		sk->sk_state   = TCP_ESTABLISHED;
A
Arnd Bergmann 已提交
808
		err = 0;
L
Linus Torvalds 已提交
809

A
Arnd Bergmann 已提交
810
		goto out;
L
Linus Torvalds 已提交
811 812 813
	}
#endif /* CONFIG_IRDA_ULTRA */

J
Jesper Juhl 已提交
814
	self->ias_obj = irias_new_object(addr->sir_name, jiffies);
A
Arnd Bergmann 已提交
815
	err = -ENOMEM;
J
Jesper Juhl 已提交
816
	if (self->ias_obj == NULL)
A
Arnd Bergmann 已提交
817
		goto out;
J
Jesper Juhl 已提交
818

L
Linus Torvalds 已提交
819
	err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
J
Jesper Juhl 已提交
820
	if (err < 0) {
821 822
		irias_delete_object(self->ias_obj);
		self->ias_obj = NULL;
A
Arnd Bergmann 已提交
823
		goto out;
J
Jesper Juhl 已提交
824
	}
L
Linus Torvalds 已提交
825 826 827 828 829 830

	/*  Register with LM-IAS */
	irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
				 self->stsap_sel, IAS_KERNEL_ATTR);
	irias_insert_object(self->ias_obj);

A
Arnd Bergmann 已提交
831 832
	err = 0;
out:
833
	release_sock(sk);
A
Arnd Bergmann 已提交
834
	return err;
L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
}

/*
 * Function irda_accept (sock, newsock, flags)
 *
 *    Wait for incoming connection
 *
 */
static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
{
	struct sock *sk = sock->sk;
	struct irda_sock *new, *self = irda_sk(sk);
	struct sock *newsk;
	struct sk_buff *skb;
	int err;

851
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
852

853
	err = irda_create(sock_net(sk), newsock, sk->sk_protocol, 0);
L
Linus Torvalds 已提交
854
	if (err)
855
		return err;
L
Linus Torvalds 已提交
856

A
Arnd Bergmann 已提交
857
	err = -EINVAL;
858 859

	lock_sock(sk);
L
Linus Torvalds 已提交
860
	if (sock->state != SS_UNCONNECTED)
A
Arnd Bergmann 已提交
861
		goto out;
L
Linus Torvalds 已提交
862 863

	if ((sk = sock->sk) == NULL)
A
Arnd Bergmann 已提交
864
		goto out;
L
Linus Torvalds 已提交
865

A
Arnd Bergmann 已提交
866
	err = -EOPNOTSUPP;
L
Linus Torvalds 已提交
867 868
	if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
	    (sk->sk_type != SOCK_DGRAM))
A
Arnd Bergmann 已提交
869
		goto out;
L
Linus Torvalds 已提交
870

A
Arnd Bergmann 已提交
871
	err = -EINVAL;
L
Linus Torvalds 已提交
872
	if (sk->sk_state != TCP_LISTEN)
A
Arnd Bergmann 已提交
873
		goto out;
L
Linus Torvalds 已提交
874 875 876 877 878 879 880 881 882 883 884 885 886 887

	/*
	 *	The read queue this time is holding sockets ready to use
	 *	hooked into the SABM we saved
	 */

	/*
	 * We can perform the accept only if there is incoming data
	 * on the listening socket.
	 * So, we will block the caller until we receive any data.
	 * If the caller was waiting on select() or poll() before
	 * calling us, the data is waiting for us ;-)
	 * Jean II
	 */
888 889 890 891
	while (1) {
		skb = skb_dequeue(&sk->sk_receive_queue);
		if (skb)
			break;
L
Linus Torvalds 已提交
892 893

		/* Non blocking operation */
A
Arnd Bergmann 已提交
894
		err = -EWOULDBLOCK;
L
Linus Torvalds 已提交
895
		if (flags & O_NONBLOCK)
A
Arnd Bergmann 已提交
896
			goto out;
L
Linus Torvalds 已提交
897

E
Eric Dumazet 已提交
898
		err = wait_event_interruptible(*(sk_sleep(sk)),
899 900
					skb_peek(&sk->sk_receive_queue));
		if (err)
A
Arnd Bergmann 已提交
901
			goto out;
L
Linus Torvalds 已提交
902 903 904
	}

	newsk = newsock->sk;
A
Arnd Bergmann 已提交
905
	err = -EIO;
906
	if (newsk == NULL)
A
Arnd Bergmann 已提交
907
		goto out;
908

L
Linus Torvalds 已提交
909 910 911 912 913 914
	newsk->sk_state = TCP_ESTABLISHED;

	new = irda_sk(newsk);

	/* Now attach up the new socket */
	new->tsap = irttp_dup(self->tsap, new);
A
Arnd Bergmann 已提交
915
	err = -EPERM; /* value does not seem to make sense. -arnd */
L
Linus Torvalds 已提交
916
	if (!new->tsap) {
917
		IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
L
Linus Torvalds 已提交
918
		kfree_skb(skb);
A
Arnd Bergmann 已提交
919
		goto out;
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	}

	new->stsap_sel = new->tsap->stsap_sel;
	new->dtsap_sel = new->tsap->dtsap_sel;
	new->saddr = irttp_get_saddr(new->tsap);
	new->daddr = irttp_get_daddr(new->tsap);

	new->max_sdu_size_tx = self->max_sdu_size_tx;
	new->max_sdu_size_rx = self->max_sdu_size_rx;
	new->max_data_size   = self->max_data_size;
	new->max_header_size = self->max_header_size;

	memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));

	/* Clean up the original one to keep it in listen state */
	irttp_listen(self->tsap);

	kfree_skb(skb);
	sk->sk_ack_backlog--;

	newsock->state = SS_CONNECTED;

	irda_connect_response(new);
A
Arnd Bergmann 已提交
943 944
	err = 0;
out:
945
	release_sock(sk);
A
Arnd Bergmann 已提交
946
	return err;
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954 955 956
}

/*
 * Function irda_connect (sock, uaddr, addr_len, flags)
 *
 *    Connect to a IrDA device
 *
 * The main difference with a "standard" connect is that with IrDA we need
 * to resolve the service name into a TSAP selector (in TCP, port number
 * doesn't have to be resolved).
M
Masanari Iida 已提交
957
 * Because of this service name resolution, we can offer "auto-connect",
L
Linus Torvalds 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
 * where we connect to a service without specifying a destination address.
 *
 * Note : by consulting "errno", the user space caller may learn the cause
 * of the failure. Most of them are visible in the function, others may come
 * from subroutines called and are listed here :
 *	o EBUSY : already processing a connect
 *	o EHOSTUNREACH : bad addr->sir_addr argument
 *	o EADDRNOTAVAIL : bad addr->sir_name argument
 *	o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
 *	o ENETUNREACH : no node found on the network (auto-connect)
 */
static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
			int addr_len, int flags)
{
	struct sock *sk = sock->sk;
	struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
	struct irda_sock *self = irda_sk(sk);
	int err;

977
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
978

979
	lock_sock(sk);
L
Linus Torvalds 已提交
980
	/* Don't allow connect for Ultra sockets */
A
Arnd Bergmann 已提交
981
	err = -ESOCKTNOSUPPORT;
L
Linus Torvalds 已提交
982
	if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA))
A
Arnd Bergmann 已提交
983
		goto out;
L
Linus Torvalds 已提交
984 985 986

	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
		sock->state = SS_CONNECTED;
A
Arnd Bergmann 已提交
987 988
		err = 0;
		goto out;   /* Connect completed during a ERESTARTSYS event */
L
Linus Torvalds 已提交
989 990 991 992
	}

	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
		sock->state = SS_UNCONNECTED;
A
Arnd Bergmann 已提交
993 994
		err = -ECONNREFUSED;
		goto out;
L
Linus Torvalds 已提交
995 996
	}

A
Arnd Bergmann 已提交
997
	err = -EISCONN;      /* No reconnect on a seqpacket socket */
L
Linus Torvalds 已提交
998
	if (sk->sk_state == TCP_ESTABLISHED)
A
Arnd Bergmann 已提交
999
		goto out;
L
Linus Torvalds 已提交
1000 1001 1002 1003

	sk->sk_state   = TCP_CLOSE;
	sock->state = SS_UNCONNECTED;

A
Arnd Bergmann 已提交
1004
	err = -EINVAL;
L
Linus Torvalds 已提交
1005
	if (addr_len != sizeof(struct sockaddr_irda))
A
Arnd Bergmann 已提交
1006
		goto out;
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012

	/* Check if user supplied any destination device address */
	if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
		/* Try to find one suitable */
		err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
		if (err) {
1013
			IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __func__);
A
Arnd Bergmann 已提交
1014
			goto out;
L
Linus Torvalds 已提交
1015 1016 1017 1018
		}
	} else {
		/* Use the one provided by the user */
		self->daddr = addr->sir_addr;
1019
		IRDA_DEBUG(1, "%s(), daddr = %08x\n", __func__, self->daddr);
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028

		/* If we don't have a valid service name, we assume the
		 * user want to connect on a specific LSAP. Prevent
		 * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */
		if((addr->sir_name[0] != '\0') ||
		   (addr->sir_lsap_sel >= 0x70)) {
			/* Query remote LM-IAS using service name */
			err = irda_find_lsap_sel(self, addr->sir_name);
			if (err) {
1029
				IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
A
Arnd Bergmann 已提交
1030
				goto out;
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			}
		} else {
			/* Directly connect to the remote LSAP
			 * specified by the sir_lsap field.
			 * Please use with caution, in IrDA LSAPs are
			 * dynamic and there is no "well-known" LSAP. */
			self->dtsap_sel = addr->sir_lsap_sel;
		}
	}

	/* Check if we have opened a local TSAP */
	if (!self->tsap)
		irda_open_tsap(self, LSAP_ANY, addr->sir_name);

	/* Move to connecting socket, start sending Connect Requests */
	sock->state = SS_CONNECTING;
	sk->sk_state   = TCP_SYN_SENT;

	/* Connect to remote device */
	err = irttp_connect_request(self->tsap, self->dtsap_sel,
				    self->saddr, self->daddr, NULL,
				    self->max_sdu_size_rx, NULL);
	if (err) {
1054
		IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
A
Arnd Bergmann 已提交
1055
		goto out;
L
Linus Torvalds 已提交
1056 1057 1058
	}

	/* Now the loop */
A
Arnd Bergmann 已提交
1059
	err = -EINPROGRESS;
L
Linus Torvalds 已提交
1060
	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
A
Arnd Bergmann 已提交
1061
		goto out;
L
Linus Torvalds 已提交
1062

A
Arnd Bergmann 已提交
1063
	err = -ERESTARTSYS;
E
Eric Dumazet 已提交
1064
	if (wait_event_interruptible(*(sk_sleep(sk)),
L
Linus Torvalds 已提交
1065
				     (sk->sk_state != TCP_SYN_SENT)))
A
Arnd Bergmann 已提交
1066
		goto out;
L
Linus Torvalds 已提交
1067 1068 1069

	if (sk->sk_state != TCP_ESTABLISHED) {
		sock->state = SS_UNCONNECTED;
1070 1071
		if (sk->sk_prot->disconnect(sk, flags))
			sock->state = SS_DISCONNECTING;
1072
		err = sock_error(sk);
A
Arnd Bergmann 已提交
1073 1074 1075
		if (!err)
			err = -ECONNRESET;
		goto out;
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081
	}

	sock->state = SS_CONNECTED;

	/* At this point, IrLMP has assigned our source address */
	self->saddr = irttp_get_saddr(self->tsap);
A
Arnd Bergmann 已提交
1082 1083
	err = 0;
out:
1084
	release_sock(sk);
A
Arnd Bergmann 已提交
1085
	return err;
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

static struct proto irda_proto = {
	.name	  = "IRDA",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct irda_sock),
};

/*
 * Function irda_create (sock, protocol)
 *
 *    Create IrDA socket
 *
 */
1100 1101
static int irda_create(struct net *net, struct socket *sock, int protocol,
		       int kern)
L
Linus Torvalds 已提交
1102 1103 1104 1105
{
	struct sock *sk;
	struct irda_sock *self;

1106
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
1107

1108 1109 1110
	if (net != &init_net)
		return -EAFNOSUPPORT;

L
Linus Torvalds 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	/* Check for valid socket type */
	switch (sock->type) {
	case SOCK_STREAM:     /* For TTP connections with SAR disabled */
	case SOCK_SEQPACKET:  /* For TTP connections with SAR enabled */
	case SOCK_DGRAM:      /* For TTP Unitdata or LMP Ultra transfers */
		break;
	default:
		return -ESOCKTNOSUPPORT;
	}

	/* Allocate networking socket */
1122
	sk = sk_alloc(net, PF_IRDA, GFP_KERNEL, &irda_proto);
L
Linus Torvalds 已提交
1123 1124 1125 1126
	if (sk == NULL)
		return -ENOMEM;

	self = irda_sk(sk);
1127
	IRDA_DEBUG(2, "%s() : self is %p\n", __func__, self);
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

	init_waitqueue_head(&self->query_wait);

	switch (sock->type) {
	case SOCK_STREAM:
		sock->ops = &irda_stream_ops;
		self->max_sdu_size_rx = TTP_SAR_DISABLE;
		break;
	case SOCK_SEQPACKET:
		sock->ops = &irda_seqpacket_ops;
		self->max_sdu_size_rx = TTP_SAR_UNBOUND;
		break;
	case SOCK_DGRAM:
		switch (protocol) {
#ifdef CONFIG_IRDA_ULTRA
		case IRDAPROTO_ULTRA:
			sock->ops = &irda_ultra_ops;
			/* Initialise now, because we may send on unbound
			 * sockets. Jean II */
			self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
			self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
			break;
#endif /* CONFIG_IRDA_ULTRA */
		case IRDAPROTO_UNITDATA:
			sock->ops = &irda_dgram_ops;
			/* We let Unitdata conn. be like seqpack conn. */
			self->max_sdu_size_rx = TTP_SAR_UNBOUND;
			break;
		default:
1157
			sk_free(sk);
L
Linus Torvalds 已提交
1158 1159 1160 1161
			return -ESOCKTNOSUPPORT;
		}
		break;
	default:
1162
		sk_free(sk);
L
Linus Torvalds 已提交
1163 1164 1165
		return -ESOCKTNOSUPPORT;
	}

1166 1167 1168 1169 1170
	/* Initialise networking socket struct */
	sock_init_data(sock, sk);	/* Note : set sk->sk_refcnt to 1 */
	sk->sk_family = PF_IRDA;
	sk->sk_protocol = protocol;

L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	/* Register as a client with IrLMP */
	self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
	self->mask.word = 0xffff;
	self->rx_flow = self->tx_flow = FLOW_START;
	self->nslots = DISCOVERY_DEFAULT_SLOTS;
	self->daddr = DEV_ADDR_ANY;	/* Until we get connected */
	self->saddr = 0x0;		/* so IrLMP assign us any link */
	return 0;
}

/*
 * Function irda_destroy_socket (self)
 *
 *    Destroy socket
 *
 */
static void irda_destroy_socket(struct irda_sock *self)
{
1189
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

	/* Unregister with IrLMP */
	irlmp_unregister_client(self->ckey);
	irlmp_unregister_service(self->skey);

	/* Unregister with LM-IAS */
	if (self->ias_obj) {
		irias_delete_object(self->ias_obj);
		self->ias_obj = NULL;
	}

	if (self->iriap) {
		iriap_close(self->iriap);
		self->iriap = NULL;
	}

	if (self->tsap) {
		irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
		irttp_close_tsap(self->tsap);
		self->tsap = NULL;
	}
#ifdef CONFIG_IRDA_ULTRA
	if (self->lsap) {
		irlmp_close_lsap(self->lsap);
		self->lsap = NULL;
	}
#endif /* CONFIG_IRDA_ULTRA */
}

/*
 * Function irda_release (sock)
 */
static int irda_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

1226
	IRDA_DEBUG(2, "%s()\n", __func__);
L
Linus Torvalds 已提交
1227

1228
	if (sk == NULL)
L
Linus Torvalds 已提交
1229 1230
		return 0;

S
Samuel Ortiz 已提交
1231
	lock_sock(sk);
L
Linus Torvalds 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240
	sk->sk_state       = TCP_CLOSE;
	sk->sk_shutdown   |= SEND_SHUTDOWN;
	sk->sk_state_change(sk);

	/* Destroy IrDA socket */
	irda_destroy_socket(irda_sk(sk));

	sock_orphan(sk);
	sock->sk   = NULL;
S
Samuel Ortiz 已提交
1241
	release_sock(sk);
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

	/* Purge queues (see sock_init_data()) */
	skb_queue_purge(&sk->sk_receive_queue);

	/* Destroy networking socket if we are the last reference on it,
	 * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */
	sock_put(sk);

	/* Notes on socket locking and deallocation... - Jean II
	 * In theory we should put pairs of sock_hold() / sock_put() to
	 * prevent the socket to be destroyed whenever there is an
	 * outstanding request or outstanding incoming packet or event.
	 *
	 * 1) This may include IAS request, both in connect and getsockopt.
	 * Unfortunately, the situation is a bit more messy than it looks,
	 * because we close iriap and kfree(self) above.
	 *
	 * 2) This may include selective discovery in getsockopt.
	 * Same stuff as above, irlmp registration and self are gone.
	 *
	 * Probably 1 and 2 may not matter, because it's all triggered
	 * by a process and the socket layer already prevent the
	 * socket to go away while a process is holding it, through
	 * sockfd_put() and fput()...
	 *
	 * 3) This may include deferred TSAP closure. In particular,
	 * we may receive a late irda_disconnect_indication()
	 * Fortunately, (tsap_cb *)->close_pend should protect us
	 * from that.
	 *
	 * I did some testing on SMP, and it looks solid. And the socket
	 * memory leak is now gone... - Jean II
	 */

1276
	return 0;
L
Linus Torvalds 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
}

/*
 * Function irda_sendmsg (iocb, sock, msg, len)
 *
 *    Send message down to TinyTP. This function is used for both STREAM and
 *    SEQPACK services. This is possible since it forces the client to
 *    fragment the message if necessary
 */
static int irda_sendmsg(struct kiocb *iocb, struct socket *sock,
			struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self;
	struct sk_buff *skb;
1292
	int err = -EPIPE;
L
Linus Torvalds 已提交
1293

1294
	IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
L
Linus Torvalds 已提交
1295 1296

	/* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1297
	if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR | MSG_CMSG_COMPAT |
A
Arnd Bergmann 已提交
1298
			       MSG_NOSIGNAL)) {
1299
		return -EINVAL;
A
Arnd Bergmann 已提交
1300
	}
L
Linus Torvalds 已提交
1301

1302 1303
	lock_sock(sk);

1304 1305
	if (sk->sk_shutdown & SEND_SHUTDOWN)
		goto out_err;
L
Linus Torvalds 已提交
1306

A
Arnd Bergmann 已提交
1307 1308 1309 1310
	if (sk->sk_state != TCP_ESTABLISHED) {
		err = -ENOTCONN;
		goto out;
	}
L
Linus Torvalds 已提交
1311 1312 1313 1314 1315

	self = irda_sk(sk);

	/* Check if IrTTP is wants us to slow down */

E
Eric Dumazet 已提交
1316
	if (wait_event_interruptible(*(sk_sleep(sk)),
A
Arnd Bergmann 已提交
1317 1318 1319 1320
	    (self->tx_flow != FLOW_STOP  ||  sk->sk_state != TCP_ESTABLISHED))) {
		err = -ERESTARTSYS;
		goto out;
	}
L
Linus Torvalds 已提交
1321 1322

	/* Check if we are still connected */
A
Arnd Bergmann 已提交
1323 1324 1325 1326
	if (sk->sk_state != TCP_ESTABLISHED) {
		err = -ENOTCONN;
		goto out;
	}
L
Linus Torvalds 已提交
1327

A
Alexey Dobriyan 已提交
1328
	/* Check that we don't send out too big frames */
L
Linus Torvalds 已提交
1329 1330
	if (len > self->max_data_size) {
		IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n",
1331
			   __func__, len, self->max_data_size);
L
Linus Torvalds 已提交
1332 1333 1334
		len = self->max_data_size;
	}

1335
	skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16,
L
Linus Torvalds 已提交
1336 1337
				  msg->msg_flags & MSG_DONTWAIT, &err);
	if (!skb)
1338
		goto out_err;
L
Linus Torvalds 已提交
1339 1340

	skb_reserve(skb, self->max_header_size + 16);
1341 1342 1343
	skb_reset_transport_header(skb);
	skb_put(skb, len);
	err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
L
Linus Torvalds 已提交
1344 1345
	if (err) {
		kfree_skb(skb);
1346
		goto out_err;
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354
	}

	/*
	 * Just send the message to TinyTP, and let it deal with possible
	 * errors. No need to duplicate all that here
	 */
	err = irttp_data_request(self->tsap, skb);
	if (err) {
1355
		IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
1356
		goto out_err;
L
Linus Torvalds 已提交
1357
	}
A
Arnd Bergmann 已提交
1358

1359
	release_sock(sk);
L
Linus Torvalds 已提交
1360 1361
	/* Tell client how much data we actually sent */
	return len;
1362

A
Arnd Bergmann 已提交
1363 1364 1365
out_err:
	err = sk_stream_error(sk, msg->msg_flags, err);
out:
1366
	release_sock(sk);
A
Arnd Bergmann 已提交
1367
	return err;
1368

L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
}

/*
 * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags)
 *
 *    Try to receive message and copy it to user. The frame is discarded
 *    after being read, regardless of how much the user actually read
 */
static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
			      struct msghdr *msg, size_t size, int flags)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);
	struct sk_buff *skb;
	size_t copied;
	int err;

1386
	IRDA_DEBUG(4, "%s()\n", __func__);
L
Linus Torvalds 已提交
1387

1388 1389
	msg->msg_namelen = 0;

L
Linus Torvalds 已提交
1390 1391 1392
	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
				flags & MSG_DONTWAIT, &err);
	if (!skb)
1393
		return err;
L
Linus Torvalds 已提交
1394

1395 1396
	skb_reset_transport_header(skb);
	copied = skb->len;
L
Linus Torvalds 已提交
1397 1398 1399

	if (copied > size) {
		IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n",
1400
			   __func__, copied, size);
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
		copied = size;
		msg->msg_flags |= MSG_TRUNC;
	}
	skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);

	skb_free_datagram(sk, skb);

	/*
	 *  Check if we have previously stopped IrTTP and we know
	 *  have more free space in our rx_queue. If so tell IrTTP
	 *  to start delivering frames again before our rx_queue gets
	 *  empty
	 */
	if (self->rx_flow == FLOW_STOP) {
		if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1416
			IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
L
Linus Torvalds 已提交
1417 1418 1419 1420
			self->rx_flow = FLOW_START;
			irttp_flow_request(self->tsap, FLOW_START);
		}
	}
A
Arnd Bergmann 已提交
1421

1422
	return copied;
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
}

/*
 * Function irda_recvmsg_stream (iocb, sock, msg, size, flags)
 */
static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
			       struct msghdr *msg, size_t size, int flags)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);
	int noblock = flags & MSG_DONTWAIT;
	size_t copied = 0;
1435
	int target, err;
1436
	long timeo;
L
Linus Torvalds 已提交
1437

1438
	IRDA_DEBUG(3, "%s()\n", __func__);
L
Linus Torvalds 已提交
1439

1440
	if ((err = sock_error(sk)) < 0)
1441
		return err;
L
Linus Torvalds 已提交
1442 1443

	if (sock->flags & __SO_ACCEPTCON)
1444
		return -EINVAL;
L
Linus Torvalds 已提交
1445

A
Arnd Bergmann 已提交
1446
	err =-EOPNOTSUPP;
L
Linus Torvalds 已提交
1447
	if (flags & MSG_OOB)
1448
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1449

A
Arnd Bergmann 已提交
1450
	err = 0;
1451 1452
	target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
	timeo = sock_rcvtimeo(sk, noblock);
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459

	msg->msg_namelen = 0;

	do {
		int chunk;
		struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);

1460 1461
		if (skb == NULL) {
			DEFINE_WAIT(wait);
A
Arnd Bergmann 已提交
1462
			err = 0;
L
Linus Torvalds 已提交
1463 1464 1465 1466

			if (copied >= target)
				break;

E
Eric Dumazet 已提交
1467
			prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
1468 1469 1470 1471

			/*
			 *	POSIX 1003.1g mandates this order.
			 */
A
Arnd Bergmann 已提交
1472 1473
			err = sock_error(sk);
			if (err)
1474
				;
L
Linus Torvalds 已提交
1475 1476 1477
			else if (sk->sk_shutdown & RCV_SHUTDOWN)
				;
			else if (noblock)
A
Arnd Bergmann 已提交
1478
				err = -EAGAIN;
L
Linus Torvalds 已提交
1479
			else if (signal_pending(current))
A
Arnd Bergmann 已提交
1480
				err = sock_intr_errno(timeo);
1481
			else if (sk->sk_state != TCP_ESTABLISHED)
A
Arnd Bergmann 已提交
1482
				err = -ENOTCONN;
L
Linus Torvalds 已提交
1483 1484 1485 1486
			else if (skb_peek(&sk->sk_receive_queue) == NULL)
				/* Wait process until data arrives */
				schedule();

E
Eric Dumazet 已提交
1487
			finish_wait(sk_sleep(sk), &wait);
L
Linus Torvalds 已提交
1488

A
Arnd Bergmann 已提交
1489
			if (err)
1490
				return err;
L
Linus Torvalds 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
			if (sk->sk_shutdown & RCV_SHUTDOWN)
				break;

			continue;
		}

		chunk = min_t(unsigned int, skb->len, size);
		if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
			skb_queue_head(&sk->sk_receive_queue, skb);
			if (copied == 0)
				copied = -EFAULT;
			break;
		}
		copied += chunk;
		size -= chunk;

		/* Mark read part of skb as used */
		if (!(flags & MSG_PEEK)) {
			skb_pull(skb, chunk);

			/* put the skb back if we didn't use it up.. */
			if (skb->len) {
				IRDA_DEBUG(1, "%s(), back on q!\n",
1514
					   __func__);
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519 1520
				skb_queue_head(&sk->sk_receive_queue, skb);
				break;
			}

			kfree_skb(skb);
		} else {
1521
			IRDA_DEBUG(0, "%s() questionable!?\n", __func__);
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

			/* put message back and return */
			skb_queue_head(&sk->sk_receive_queue, skb);
			break;
		}
	} while (size);

	/*
	 *  Check if we have previously stopped IrTTP and we know
	 *  have more free space in our rx_queue. If so tell IrTTP
	 *  to start delivering frames again before our rx_queue gets
	 *  empty
	 */
	if (self->rx_flow == FLOW_STOP) {
		if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1537
			IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
L
Linus Torvalds 已提交
1538 1539 1540 1541 1542
			self->rx_flow = FLOW_START;
			irttp_flow_request(self->tsap, FLOW_START);
		}
	}

1543
	return copied;
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
}

/*
 * Function irda_sendmsg_dgram (iocb, sock, msg, len)
 *
 *    Send message down to TinyTP for the unreliable sequenced
 *    packet service...
 *
 */
static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock,
			      struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self;
	struct sk_buff *skb;
	int err;

1561
	IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
L
Linus Torvalds 已提交
1562 1563

	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1564 1565 1566
		return -EINVAL;

	lock_sock(sk);
L
Linus Torvalds 已提交
1567 1568 1569

	if (sk->sk_shutdown & SEND_SHUTDOWN) {
		send_sig(SIGPIPE, current, 0);
A
Arnd Bergmann 已提交
1570 1571
		err = -EPIPE;
		goto out;
L
Linus Torvalds 已提交
1572 1573
	}

A
Arnd Bergmann 已提交
1574
	err = -ENOTCONN;
L
Linus Torvalds 已提交
1575
	if (sk->sk_state != TCP_ESTABLISHED)
A
Arnd Bergmann 已提交
1576
		goto out;
L
Linus Torvalds 已提交
1577 1578 1579 1580

	self = irda_sk(sk);

	/*
A
Alexey Dobriyan 已提交
1581
	 * Check that we don't send out too big frames. This is an unreliable
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586
	 * service, so we have no fragmentation and no coalescence
	 */
	if (len > self->max_data_size) {
		IRDA_DEBUG(0, "%s(), Warning to much data! "
			   "Chopping frame from %zd to %d bytes!\n",
1587
			   __func__, len, self->max_data_size);
L
Linus Torvalds 已提交
1588 1589 1590 1591 1592
		len = self->max_data_size;
	}

	skb = sock_alloc_send_skb(sk, len + self->max_header_size,
				  msg->msg_flags & MSG_DONTWAIT, &err);
A
Arnd Bergmann 已提交
1593
	err = -ENOBUFS;
L
Linus Torvalds 已提交
1594
	if (!skb)
A
Arnd Bergmann 已提交
1595
		goto out;
L
Linus Torvalds 已提交
1596 1597

	skb_reserve(skb, self->max_header_size);
1598
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
1599

1600
	IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
1601 1602
	skb_put(skb, len);
	err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
L
Linus Torvalds 已提交
1603 1604
	if (err) {
		kfree_skb(skb);
A
Arnd Bergmann 已提交
1605
		goto out;
L
Linus Torvalds 已提交
1606 1607 1608 1609 1610 1611 1612 1613
	}

	/*
	 * Just send the message to TinyTP, and let it deal with possible
	 * errors. No need to duplicate all that here
	 */
	err = irttp_udata_request(self->tsap, skb);
	if (err) {
1614
		IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
A
Arnd Bergmann 已提交
1615
		goto out;
L
Linus Torvalds 已提交
1616
	}
1617 1618

	release_sock(sk);
L
Linus Torvalds 已提交
1619
	return len;
1620

A
Arnd Bergmann 已提交
1621
out:
1622
	release_sock(sk);
A
Arnd Bergmann 已提交
1623
	return err;
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
}

/*
 * Function irda_sendmsg_ultra (iocb, sock, msg, len)
 *
 *    Send message down to IrLMP for the unreliable Ultra
 *    packet service...
 */
#ifdef CONFIG_IRDA_ULTRA
static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock,
			      struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self;
	__u8 pid = 0;
	int bound = 0;
	struct sk_buff *skb;
	int err;

1643
	IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
L
Linus Torvalds 已提交
1644

A
Arnd Bergmann 已提交
1645
	err = -EINVAL;
L
Linus Torvalds 已提交
1646
	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1647 1648 1649
		return -EINVAL;

	lock_sock(sk);
L
Linus Torvalds 已提交
1650

A
Arnd Bergmann 已提交
1651
	err = -EPIPE;
L
Linus Torvalds 已提交
1652 1653
	if (sk->sk_shutdown & SEND_SHUTDOWN) {
		send_sig(SIGPIPE, current, 0);
A
Arnd Bergmann 已提交
1654
		goto out;
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661
	}

	self = irda_sk(sk);

	/* Check if an address was specified with sendto. Jean II */
	if (msg->msg_name) {
		struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name;
A
Arnd Bergmann 已提交
1662
		err = -EINVAL;
L
Linus Torvalds 已提交
1663 1664
		/* Check address, extract pid. Jean II */
		if (msg->msg_namelen < sizeof(*addr))
A
Arnd Bergmann 已提交
1665
			goto out;
L
Linus Torvalds 已提交
1666
		if (addr->sir_family != AF_IRDA)
A
Arnd Bergmann 已提交
1667
			goto out;
L
Linus Torvalds 已提交
1668 1669 1670

		pid = addr->sir_lsap_sel;
		if (pid & 0x80) {
1671
			IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
A
Arnd Bergmann 已提交
1672 1673
			err = -EOPNOTSUPP;
			goto out;
L
Linus Torvalds 已提交
1674 1675 1676 1677 1678 1679 1680
		}
	} else {
		/* Check that the socket is properly bound to an Ultra
		 * port. Jean II */
		if ((self->lsap == NULL) ||
		    (sk->sk_state != TCP_ESTABLISHED)) {
			IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n",
1681
				   __func__);
A
Arnd Bergmann 已提交
1682 1683
			err = -ENOTCONN;
			goto out;
L
Linus Torvalds 已提交
1684 1685 1686 1687 1688 1689
		}
		/* Use PID from socket */
		bound = 1;
	}

	/*
A
Alexey Dobriyan 已提交
1690
	 * Check that we don't send out too big frames. This is an unreliable
L
Linus Torvalds 已提交
1691 1692 1693 1694 1695
	 * service, so we have no fragmentation and no coalescence
	 */
	if (len > self->max_data_size) {
		IRDA_DEBUG(0, "%s(), Warning to much data! "
			   "Chopping frame from %zd to %d bytes!\n",
1696
			   __func__, len, self->max_data_size);
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701
		len = self->max_data_size;
	}

	skb = sock_alloc_send_skb(sk, len + self->max_header_size,
				  msg->msg_flags & MSG_DONTWAIT, &err);
A
Arnd Bergmann 已提交
1702
	err = -ENOBUFS;
L
Linus Torvalds 已提交
1703
	if (!skb)
A
Arnd Bergmann 已提交
1704
		goto out;
L
Linus Torvalds 已提交
1705 1706

	skb_reserve(skb, self->max_header_size);
1707
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
1708

1709
	IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
1710 1711
	skb_put(skb, len);
	err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
L
Linus Torvalds 已提交
1712 1713
	if (err) {
		kfree_skb(skb);
A
Arnd Bergmann 已提交
1714
		goto out;
L
Linus Torvalds 已提交
1715 1716 1717 1718
	}

	err = irlmp_connless_data_request((bound ? self->lsap : NULL),
					  skb, pid);
A
Arnd Bergmann 已提交
1719
	if (err)
1720
		IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
A
Arnd Bergmann 已提交
1721
out:
1722
	release_sock(sk);
A
Arnd Bergmann 已提交
1723
	return err ? : len;
L
Linus Torvalds 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
}
#endif /* CONFIG_IRDA_ULTRA */

/*
 * Function irda_shutdown (sk, how)
 */
static int irda_shutdown(struct socket *sock, int how)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);

1735
	IRDA_DEBUG(1, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
1736

1737
	lock_sock(sk);
A
Arnd Bergmann 已提交
1738

L
Linus Torvalds 已提交
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	sk->sk_state       = TCP_CLOSE;
	sk->sk_shutdown   |= SEND_SHUTDOWN;
	sk->sk_state_change(sk);

	if (self->iriap) {
		iriap_close(self->iriap);
		self->iriap = NULL;
	}

	if (self->tsap) {
		irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
		irttp_close_tsap(self->tsap);
		self->tsap = NULL;
	}

	/* A few cleanup so the socket look as good as new... */
	self->rx_flow = self->tx_flow = FLOW_START;	/* needed ??? */
	self->daddr = DEV_ADDR_ANY;	/* Until we get re-connected */
	self->saddr = 0x0;		/* so IrLMP assign us any link */

1759
	release_sock(sk);
A
Arnd Bergmann 已提交
1760

1761
	return 0;
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
}

/*
 * Function irda_poll (file, sock, wait)
 */
static unsigned int irda_poll(struct file * file, struct socket *sock,
			      poll_table *wait)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);
	unsigned int mask;

1774
	IRDA_DEBUG(4, "%s()\n", __func__);
L
Linus Torvalds 已提交
1775

E
Eric Dumazet 已提交
1776
	poll_wait(file, sk_sleep(sk), wait);
L
Linus Torvalds 已提交
1777 1778 1779 1780 1781 1782
	mask = 0;

	/* Exceptional events? */
	if (sk->sk_err)
		mask |= POLLERR;
	if (sk->sk_shutdown & RCV_SHUTDOWN) {
1783
		IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
L
Linus Torvalds 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
		mask |= POLLHUP;
	}

	/* Readable? */
	if (!skb_queue_empty(&sk->sk_receive_queue)) {
		IRDA_DEBUG(4, "Socket is readable\n");
		mask |= POLLIN | POLLRDNORM;
	}

	/* Connection-based need to check for termination and startup */
	switch (sk->sk_type) {
	case SOCK_STREAM:
		if (sk->sk_state == TCP_CLOSE) {
1797
			IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
			mask |= POLLHUP;
		}

		if (sk->sk_state == TCP_ESTABLISHED) {
			if ((self->tx_flow == FLOW_START) &&
			    sock_writeable(sk))
			{
				mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
			}
		}
		break;
	case SOCK_SEQPACKET:
		if ((self->tx_flow == FLOW_START) &&
		    sock_writeable(sk))
		{
			mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
		}
		break;
	case SOCK_DGRAM:
		if (sock_writeable(sk))
			mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
		break;
	default:
		break;
	}

1824
	return mask;
A
Arnd Bergmann 已提交
1825 1826
}

L
Linus Torvalds 已提交
1827 1828 1829 1830 1831 1832
/*
 * Function irda_ioctl (sock, cmd, arg)
 */
static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	struct sock *sk = sock->sk;
A
Arnd Bergmann 已提交
1833
	int err;
L
Linus Torvalds 已提交
1834

1835
	IRDA_DEBUG(4, "%s(), cmd=%#x\n", __func__, cmd);
L
Linus Torvalds 已提交
1836

A
Arnd Bergmann 已提交
1837
	err = -EINVAL;
L
Linus Torvalds 已提交
1838 1839 1840
	switch (cmd) {
	case TIOCOUTQ: {
		long amount;
1841 1842

		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
L
Linus Torvalds 已提交
1843 1844
		if (amount < 0)
			amount = 0;
A
Arnd Bergmann 已提交
1845 1846
		err = put_user(amount, (unsigned int __user *)arg);
		break;
L
Linus Torvalds 已提交
1847 1848 1849 1850 1851 1852 1853 1854
	}

	case TIOCINQ: {
		struct sk_buff *skb;
		long amount = 0L;
		/* These two are safe on a single CPU system as only user tasks fiddle here */
		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
			amount = skb->len;
A
Arnd Bergmann 已提交
1855 1856
		err = put_user(amount, (unsigned int __user *)arg);
		break;
L
Linus Torvalds 已提交
1857 1858 1859 1860
	}

	case SIOCGSTAMP:
		if (sk != NULL)
A
Arnd Bergmann 已提交
1861 1862
			err = sock_get_timestamp(sk, (struct timeval __user *)arg);
		break;
L
Linus Torvalds 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873

	case SIOCGIFADDR:
	case SIOCSIFADDR:
	case SIOCGIFDSTADDR:
	case SIOCSIFDSTADDR:
	case SIOCGIFBRDADDR:
	case SIOCSIFBRDADDR:
	case SIOCGIFNETMASK:
	case SIOCSIFNETMASK:
	case SIOCGIFMETRIC:
	case SIOCSIFMETRIC:
A
Arnd Bergmann 已提交
1874
		break;
L
Linus Torvalds 已提交
1875
	default:
1876
		IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __func__);
A
Arnd Bergmann 已提交
1877
		err = -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1878 1879
	}

A
Arnd Bergmann 已提交
1880
	return err;
L
Linus Torvalds 已提交
1881 1882
}

1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
#ifdef CONFIG_COMPAT
/*
 * Function irda_ioctl (sock, cmd, arg)
 */
static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	/*
	 * All IRDA's ioctl are standard ones.
	 */
	return -ENOIOCTLCMD;
}
#endif

L
Linus Torvalds 已提交
1896 1897 1898 1899 1900 1901
/*
 * Function irda_setsockopt (sock, level, optname, optval, optlen)
 *
 *    Set some options for the socket
 *
 */
1902
static int irda_setsockopt(struct socket *sock, int level, int optname,
1903
			   char __user *optval, unsigned int optlen)
L
Linus Torvalds 已提交
1904 1905 1906 1907 1908 1909
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);
	struct irda_ias_set    *ias_opt;
	struct ias_object      *ias_obj;
	struct ias_attrib *	ias_attr;	/* Attribute in IAS object */
1910
	int opt, free_ias = 0, err = 0;
L
Linus Torvalds 已提交
1911

1912
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
1913 1914 1915 1916

	if (level != SOL_IRLMP)
		return -ENOPROTOOPT;

1917 1918
	lock_sock(sk);

L
Linus Torvalds 已提交
1919 1920 1921 1922 1923 1924 1925 1926 1927
	switch (optname) {
	case IRLMP_IAS_SET:
		/* The user want to add an attribute to an existing IAS object
		 * (in the IAS database) or to create a new object with this
		 * attribute.
		 * We first query IAS to know if the object exist, and then
		 * create the right attribute...
		 */

1928 1929 1930 1931
		if (optlen != sizeof(struct irda_ias_set)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
1932 1933

		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1934 1935 1936 1937
		if (ias_opt == NULL) {
			err = -ENOMEM;
			goto out;
		}
L
Linus Torvalds 已提交
1938 1939 1940 1941

		/* Copy query to the driver. */
		if (copy_from_user(ias_opt, optval, optlen)) {
			kfree(ias_opt);
1942 1943
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952
		}

		/* Find the object we target.
		 * If the user gives us an empty string, we use the object
		 * associated with this socket. This will workaround
		 * duplicated class name - Jean II */
		if(ias_opt->irda_class_name[0] == '\0') {
			if(self->ias_obj == NULL) {
				kfree(ias_opt);
1953 1954
				err = -EINVAL;
				goto out;
L
Linus Torvalds 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
			}
			ias_obj = self->ias_obj;
		} else
			ias_obj = irias_find_object(ias_opt->irda_class_name);

		/* Only ROOT can mess with the global IAS database.
		 * Users can only add attributes to the object associated
		 * with the socket they own - Jean II */
		if((!capable(CAP_NET_ADMIN)) &&
		   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
			kfree(ias_opt);
1966 1967
			err = -EPERM;
			goto out;
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974
		}

		/* If the object doesn't exist, create it */
		if(ias_obj == (struct ias_object *) NULL) {
			/* Create a new object */
			ias_obj = irias_new_object(ias_opt->irda_class_name,
						   jiffies);
J
Jesper Juhl 已提交
1975 1976
			if (ias_obj == NULL) {
				kfree(ias_opt);
1977 1978
				err = -ENOMEM;
				goto out;
J
Jesper Juhl 已提交
1979 1980
			}
			free_ias = 1;
L
Linus Torvalds 已提交
1981 1982 1983 1984 1985
		}

		/* Do we have the attribute already ? */
		if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
			kfree(ias_opt);
J
Jesper Juhl 已提交
1986 1987 1988 1989
			if (free_ias) {
				kfree(ias_obj->name);
				kfree(ias_obj);
			}
1990 1991
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
		}

		/* Look at the type */
		switch(ias_opt->irda_attrib_type) {
		case IAS_INTEGER:
			/* Add an integer attribute */
			irias_add_integer_attrib(
				ias_obj,
				ias_opt->irda_attrib_name,
				ias_opt->attribute.irda_attrib_int,
				IAS_USER_ATTR);
			break;
		case IAS_OCT_SEQ:
			/* Check length */
			if(ias_opt->attribute.irda_attrib_octet_seq.len >
			   IAS_MAX_OCTET_STRING) {
				kfree(ias_opt);
J
Jesper Juhl 已提交
2009 2010 2011 2012 2013
				if (free_ias) {
					kfree(ias_obj->name);
					kfree(ias_obj);
				}

2014 2015
				err = -EINVAL;
				goto out;
L
Linus Torvalds 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
			}
			/* Add an octet sequence attribute */
			irias_add_octseq_attrib(
			      ias_obj,
			      ias_opt->irda_attrib_name,
			      ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
			      ias_opt->attribute.irda_attrib_octet_seq.len,
			      IAS_USER_ATTR);
			break;
		case IAS_STRING:
			/* Should check charset & co */
			/* Check length */
			/* The length is encoded in a __u8, and
			 * IAS_MAX_STRING == 256, so there is no way
			 * userspace can pass us a string too large.
			 * Jean II */
			/* NULL terminate the string (avoid troubles) */
			ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
			/* Add a string attribute */
			irias_add_string_attrib(
				ias_obj,
				ias_opt->irda_attrib_name,
				ias_opt->attribute.irda_attrib_string.string,
				IAS_USER_ATTR);
			break;
		default :
			kfree(ias_opt);
J
Jesper Juhl 已提交
2043 2044 2045 2046
			if (free_ias) {
				kfree(ias_obj->name);
				kfree(ias_obj);
			}
2047 2048
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
		}
		irias_insert_object(ias_obj);
		kfree(ias_opt);
		break;
	case IRLMP_IAS_DEL:
		/* The user want to delete an object from our local IAS
		 * database. We just need to query the IAS, check is the
		 * object is not owned by the kernel and delete it.
		 */

2059 2060 2061 2062
		if (optlen != sizeof(struct irda_ias_set)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2063 2064

		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2065 2066 2067 2068
		if (ias_opt == NULL) {
			err = -ENOMEM;
			goto out;
		}
L
Linus Torvalds 已提交
2069 2070 2071 2072

		/* Copy query to the driver. */
		if (copy_from_user(ias_opt, optval, optlen)) {
			kfree(ias_opt);
2073 2074
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
		}

		/* Find the object we target.
		 * If the user gives us an empty string, we use the object
		 * associated with this socket. This will workaround
		 * duplicated class name - Jean II */
		if(ias_opt->irda_class_name[0] == '\0')
			ias_obj = self->ias_obj;
		else
			ias_obj = irias_find_object(ias_opt->irda_class_name);
		if(ias_obj == (struct ias_object *) NULL) {
			kfree(ias_opt);
2087 2088
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094 2095 2096
		}

		/* Only ROOT can mess with the global IAS database.
		 * Users can only del attributes from the object associated
		 * with the socket they own - Jean II */
		if((!capable(CAP_NET_ADMIN)) &&
		   ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
			kfree(ias_opt);
2097 2098
			err = -EPERM;
			goto out;
L
Linus Torvalds 已提交
2099 2100 2101 2102 2103 2104 2105
		}

		/* Find the attribute (in the object) we target */
		ias_attr = irias_find_attrib(ias_obj,
					     ias_opt->irda_attrib_name);
		if(ias_attr == (struct ias_attrib *) NULL) {
			kfree(ias_opt);
2106 2107
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
2108 2109 2110 2111
		}

		/* Check is the user space own the object */
		if(ias_attr->value->owner != IAS_USER_ATTR) {
2112
			IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __func__);
L
Linus Torvalds 已提交
2113
			kfree(ias_opt);
2114 2115
			err = -EPERM;
			goto out;
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121 2122
		}

		/* Remove the attribute (and maybe the object) */
		irias_delete_attrib(ias_obj, ias_attr, 1);
		kfree(ias_opt);
		break;
	case IRLMP_MAX_SDU_SIZE:
2123 2124 2125 2126
		if (optlen < sizeof(int)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2127

2128 2129 2130 2131
		if (get_user(opt, (int __user *)optval)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2132 2133 2134 2135

		/* Only possible for a seqpacket service (TTP with SAR) */
		if (sk->sk_type != SOCK_SEQPACKET) {
			IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n",
2136
				   __func__, opt);
L
Linus Torvalds 已提交
2137 2138 2139
			self->max_sdu_size_rx = opt;
		} else {
			IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n",
2140
				     __func__);
2141 2142
			err = -ENOPROTOOPT;
			goto out;
L
Linus Torvalds 已提交
2143 2144 2145
		}
		break;
	case IRLMP_HINTS_SET:
2146 2147 2148 2149
		if (optlen < sizeof(int)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2150 2151

		/* The input is really a (__u8 hints[2]), easier as an int */
2152 2153 2154 2155
		if (get_user(opt, (int __user *)optval)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168

		/* Unregister any old registration */
		if (self->skey)
			irlmp_unregister_service(self->skey);

		self->skey = irlmp_register_service((__u16) opt);
		break;
	case IRLMP_HINT_MASK_SET:
		/* As opposed to the previous case which set the hint bits
		 * that we advertise, this one set the filter we use when
		 * making a discovery (nodes which don't match any hint
		 * bit in the mask are not reported).
		 */
2169 2170 2171 2172
		if (optlen < sizeof(int)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2173 2174

		/* The input is really a (__u8 hints[2]), easier as an int */
2175 2176 2177 2178
		if (get_user(opt, (int __user *)optval)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189

		/* Set the new hint mask */
		self->mask.word = (__u16) opt;
		/* Mask out extension bits */
		self->mask.word &= 0x7f7f;
		/* Check if no bits */
		if(!self->mask.word)
			self->mask.word = 0xFFFF;

		break;
	default:
2190 2191
		err = -ENOPROTOOPT;
		break;
L
Linus Torvalds 已提交
2192 2193
	}

2194 2195
out:
	release_sock(sk);
A
Arnd Bergmann 已提交
2196 2197 2198 2199

	return err;
}

L
Linus Torvalds 已提交
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
/*
 * Function irda_extract_ias_value(ias_opt, ias_value)
 *
 *    Translate internal IAS value structure to the user space representation
 *
 * The external representation of IAS values, as we exchange them with
 * user space program is quite different from the internal representation,
 * as stored in the IAS database (because we need a flat structure for
 * crossing kernel boundary).
 * This function transform the former in the latter. We also check
 * that the value type is valid.
 */
static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
				  struct ias_value *ias_value)
{
	/* Look at the type */
	switch (ias_value->type) {
	case IAS_INTEGER:
		/* Copy the integer */
		ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
		break;
	case IAS_OCT_SEQ:
		/* Set length */
		ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
		/* Copy over */
		memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
		       ias_value->t.oct_seq, ias_value->len);
		break;
	case IAS_STRING:
		/* Set length */
		ias_opt->attribute.irda_attrib_string.len = ias_value->len;
		ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
		/* Copy over */
		memcpy(ias_opt->attribute.irda_attrib_string.string,
		       ias_value->t.string, ias_value->len);
		/* NULL terminate the string (avoid troubles) */
		ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
		break;
	case IAS_MISSING:
	default :
		return -EINVAL;
	}

	/* Copy type over */
	ias_opt->irda_attrib_type = ias_value->type;

	return 0;
}

/*
 * Function irda_getsockopt (sock, level, optname, optval, optlen)
 */
2252
static int irda_getsockopt(struct socket *sock, int level, int optname,
L
Linus Torvalds 已提交
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
			   char __user *optval, int __user *optlen)
{
	struct sock *sk = sock->sk;
	struct irda_sock *self = irda_sk(sk);
	struct irda_device_list list;
	struct irda_device_info *discoveries;
	struct irda_ias_set *	ias_opt;	/* IAS get/query params */
	struct ias_object *	ias_obj;	/* Object in IAS */
	struct ias_attrib *	ias_attr;	/* Attribute in IAS object */
	int daddr = DEV_ADDR_ANY;	/* Dest address for IAS queries */
	int val = 0;
	int len = 0;
2265
	int err = 0;
L
Linus Torvalds 已提交
2266 2267
	int offset, total;

2268
	IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
L
Linus Torvalds 已提交
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278

	if (level != SOL_IRLMP)
		return -ENOPROTOOPT;

	if (get_user(len, optlen))
		return -EFAULT;

	if(len < 0)
		return -EINVAL;

2279 2280
	lock_sock(sk);

L
Linus Torvalds 已提交
2281 2282
	switch (optname) {
	case IRLMP_ENUMDEVICES:
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

		/* Offset to first device entry */
		offset = sizeof(struct irda_device_list) -
			sizeof(struct irda_device_info);

		if (len < offset) {
			err = -EINVAL;
			goto out;
		}

L
Linus Torvalds 已提交
2293 2294 2295 2296
		/* Ask lmp for the current discovery log */
		discoveries = irlmp_get_discoveries(&list.len, self->mask.word,
						    self->nslots);
		/* Check if the we got some results */
2297 2298 2299 2300
		if (discoveries == NULL) {
			err = -EAGAIN;
			goto out;		/* Didn't find any devices */
		}
L
Linus Torvalds 已提交
2301 2302

		/* Write total list length back to client */
2303
		if (copy_to_user(optval, &list, offset))
L
Linus Torvalds 已提交
2304 2305 2306
			err = -EFAULT;

		/* Copy the list itself - watch for overflow */
2307
		if (list.len > 2048) {
L
Linus Torvalds 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
			err = -EINVAL;
			goto bed;
		}
		total = offset + (list.len * sizeof(struct irda_device_info));
		if (total > len)
			total = len;
		if (copy_to_user(optval+offset, discoveries, total - offset))
			err = -EFAULT;

		/* Write total number of bytes used back to client */
		if (put_user(total, optlen))
			err = -EFAULT;
bed:
		/* Free up our buffer */
		kfree(discoveries);
		break;
	case IRLMP_MAX_SDU_SIZE:
		val = self->max_data_size;
		len = sizeof(int);
2327 2328 2329 2330 2331 2332 2333 2334 2335
		if (put_user(len, optlen)) {
			err = -EFAULT;
			goto out;
		}

		if (copy_to_user(optval, &val, len)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2336 2337 2338 2339 2340 2341 2342 2343

		break;
	case IRLMP_IAS_GET:
		/* The user want an object from our local IAS database.
		 * We just need to query the IAS and return the value
		 * that we found */

		/* Check that the user has allocated the right space for us */
2344 2345 2346 2347
		if (len != sizeof(struct irda_ias_set)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2348 2349

		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2350 2351 2352 2353
		if (ias_opt == NULL) {
			err = -ENOMEM;
			goto out;
		}
L
Linus Torvalds 已提交
2354 2355 2356 2357

		/* Copy query to the driver. */
		if (copy_from_user(ias_opt, optval, len)) {
			kfree(ias_opt);
2358 2359
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
		}

		/* Find the object we target.
		 * If the user gives us an empty string, we use the object
		 * associated with this socket. This will workaround
		 * duplicated class name - Jean II */
		if(ias_opt->irda_class_name[0] == '\0')
			ias_obj = self->ias_obj;
		else
			ias_obj = irias_find_object(ias_opt->irda_class_name);
		if(ias_obj == (struct ias_object *) NULL) {
			kfree(ias_opt);
2372 2373
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
2374 2375 2376 2377 2378 2379 2380
		}

		/* Find the attribute (in the object) we target */
		ias_attr = irias_find_attrib(ias_obj,
					     ias_opt->irda_attrib_name);
		if(ias_attr == (struct ias_attrib *) NULL) {
			kfree(ias_opt);
2381 2382
			err = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
2383 2384 2385 2386 2387 2388
		}

		/* Translate from internal to user structure */
		err = irda_extract_ias_value(ias_opt, ias_attr->value);
		if(err) {
			kfree(ias_opt);
2389
			goto out;
L
Linus Torvalds 已提交
2390 2391 2392 2393 2394 2395
		}

		/* Copy reply to the user */
		if (copy_to_user(optval, ias_opt,
				 sizeof(struct irda_ias_set))) {
			kfree(ias_opt);
2396 2397
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
		}
		/* Note : don't need to put optlen, we checked it */
		kfree(ias_opt);
		break;
	case IRLMP_IAS_QUERY:
		/* The user want an object from a remote IAS database.
		 * We need to use IAP to query the remote database and
		 * then wait for the answer to come back. */

		/* Check that the user has allocated the right space for us */
2408 2409 2410 2411
		if (len != sizeof(struct irda_ias_set)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2412 2413

		ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2414 2415 2416 2417
		if (ias_opt == NULL) {
			err = -ENOMEM;
			goto out;
		}
L
Linus Torvalds 已提交
2418 2419 2420 2421

		/* Copy query to the driver. */
		if (copy_from_user(ias_opt, optval, len)) {
			kfree(ias_opt);
2422 2423
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
		}

		/* At this point, there are two cases...
		 * 1) the socket is connected - that's the easy case, we
		 *	just query the device we are connected to...
		 * 2) the socket is not connected - the user doesn't want
		 *	to connect and/or may not have a valid service name
		 *	(so can't create a fake connection). In this case,
		 *	we assume that the user pass us a valid destination
		 *	address in the requesting structure...
		 */
		if(self->daddr != DEV_ADDR_ANY) {
			/* We are connected - reuse known daddr */
			daddr = self->daddr;
		} else {
			/* We are not connected, we must specify a valid
			 * destination address */
			daddr = ias_opt->daddr;
			if((!daddr) || (daddr == DEV_ADDR_ANY)) {
				kfree(ias_opt);
2444 2445
				err = -EINVAL;
				goto out;
L
Linus Torvalds 已提交
2446 2447 2448 2449 2450 2451
			}
		}

		/* Check that we can proceed with IAP */
		if (self->iriap) {
			IRDA_WARNING("%s: busy with a previous query\n",
2452
				     __func__);
L
Linus Torvalds 已提交
2453
			kfree(ias_opt);
2454 2455
			err = -EBUSY;
			goto out;
L
Linus Torvalds 已提交
2456 2457 2458 2459 2460 2461 2462
		}

		self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
					 irda_getvalue_confirm);

		if (self->iriap == NULL) {
			kfree(ias_opt);
2463 2464
			err = -ENOMEM;
			goto out;
L
Linus Torvalds 已提交
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
		}

		/* Treat unexpected wakeup as disconnect */
		self->errno = -EHOSTUNREACH;

		/* Query remote LM-IAS */
		iriap_getvaluebyclass_request(self->iriap,
					      self->saddr, daddr,
					      ias_opt->irda_class_name,
					      ias_opt->irda_attrib_name);

		/* Wait for answer, if not yet finished (or failed) */
		if (wait_event_interruptible(self->query_wait,
					     (self->iriap == NULL))) {
			/* pending request uses copy of ias_opt-content
			 * we can free it regardless! */
			kfree(ias_opt);
			/* Treat signals as disconnect */
2483 2484
			err = -EHOSTUNREACH;
			goto out;
L
Linus Torvalds 已提交
2485 2486 2487 2488 2489 2490 2491 2492 2493
		}

		/* Check what happened */
		if (self->errno)
		{
			kfree(ias_opt);
			/* Requested object/attribute doesn't exist */
			if((self->errno == IAS_CLASS_UNKNOWN) ||
			   (self->errno == IAS_ATTRIB_UNKNOWN))
2494
				err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
2495
			else
2496 2497 2498
				err = -EHOSTUNREACH;

			goto out;
L
Linus Torvalds 已提交
2499 2500 2501 2502 2503 2504 2505 2506
		}

		/* Translate from internal to user structure */
		err = irda_extract_ias_value(ias_opt, self->ias_result);
		if (self->ias_result)
			irias_delete_value(self->ias_result);
		if (err) {
			kfree(ias_opt);
2507
			goto out;
L
Linus Torvalds 已提交
2508 2509 2510 2511 2512 2513
		}

		/* Copy reply to the user */
		if (copy_to_user(optval, ias_opt,
				 sizeof(struct irda_ias_set))) {
			kfree(ias_opt);
2514 2515
			err = -EFAULT;
			goto out;
L
Linus Torvalds 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
		}
		/* Note : don't need to put optlen, we checked it */
		kfree(ias_opt);
		break;
	case IRLMP_WAITDEVICE:
		/* This function is just another way of seeing life ;-)
		 * IRLMP_ENUMDEVICES assumes that you have a static network,
		 * and that you just want to pick one of the devices present.
		 * On the other hand, in here we assume that no device is
		 * present and that at some point in the future a device will
		 * come into range. When this device arrive, we just wake
		 * up the caller, so that he has time to connect to it before
		 * the device goes away...
		 * Note : once the node has been discovered for more than a
		 * few second, it won't trigger this function, unless it
		 * goes away and come back changes its hint bits (so we
		 * might call it IRLMP_WAITNEWDEVICE).
		 */

		/* Check that the user is passing us an int */
2536 2537 2538 2539
		if (len != sizeof(int)) {
			err = -EINVAL;
			goto out;
		}
L
Linus Torvalds 已提交
2540
		/* Get timeout in ms (max time we block the caller) */
2541 2542 2543 2544
		if (get_user(val, (int __user *)optval)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555

		/* Tell IrLMP we want to be notified */
		irlmp_update_client(self->ckey, self->mask.word,
				    irda_selective_discovery_indication,
				    NULL, (void *) self);

		/* Do some discovery (and also return cached results) */
		irlmp_discovery_request(self->nslots);

		/* Wait until a node is discovered */
		if (!self->cachedaddr) {
2556
			IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __func__);
L
Linus Torvalds 已提交
2557 2558 2559

			/* Set watchdog timer to expire in <val> ms. */
			self->errno = 0;
2560 2561
			setup_timer(&self->watchdog, irda_discovery_timeout,
					(unsigned long)self);
2562 2563
			mod_timer(&self->watchdog,
				  jiffies + msecs_to_jiffies(val));
L
Linus Torvalds 已提交
2564 2565 2566 2567

			/* Wait for IR-LMP to call us back */
			__wait_event_interruptible(self->query_wait,
			      (self->cachedaddr != 0 || self->errno == -ETIME),
2568
						   err);
L
Linus Torvalds 已提交
2569 2570

			/* If watchdog is still activated, kill it! */
2571
			del_timer(&(self->watchdog));
L
Linus Torvalds 已提交
2572

2573
			IRDA_DEBUG(1, "%s(), ...waking up !\n", __func__);
L
Linus Torvalds 已提交
2574

2575 2576
			if (err != 0)
				goto out;
L
Linus Torvalds 已提交
2577 2578 2579
		}
		else
			IRDA_DEBUG(1, "%s(), found immediately !\n",
2580
				   __func__);
L
Linus Torvalds 已提交
2581 2582 2583 2584 2585 2586

		/* Tell IrLMP that we have been notified */
		irlmp_update_client(self->ckey, self->mask.word,
				    NULL, NULL, NULL);

		/* Check if the we got some results */
2587 2588 2589 2590
		if (!self->cachedaddr) {
			err = -EAGAIN;		/* Didn't find any devices */
			goto out;
		}
L
Linus Torvalds 已提交
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
		daddr = self->cachedaddr;
		/* Cleanup */
		self->cachedaddr = 0;

		/* We return the daddr of the device that trigger the
		 * wakeup. As irlmp pass us only the new devices, we
		 * are sure that it's not an old device.
		 * If the user want more details, he should query
		 * the whole discovery log and pick one device...
		 */
2601 2602 2603 2604
		if (put_user(daddr, (int __user *)optval)) {
			err = -EFAULT;
			goto out;
		}
L
Linus Torvalds 已提交
2605 2606 2607

		break;
	default:
2608
		err = -ENOPROTOOPT;
L
Linus Torvalds 已提交
2609 2610
	}

2611
out:
A
Arnd Bergmann 已提交
2612

2613
	release_sock(sk);
A
Arnd Bergmann 已提交
2614 2615 2616 2617

	return err;
}

2618
static const struct net_proto_family irda_family_ops = {
L
Linus Torvalds 已提交
2619 2620 2621 2622 2623
	.family = PF_IRDA,
	.create = irda_create,
	.owner	= THIS_MODULE,
};

A
Arnd Bergmann 已提交
2624
static const struct proto_ops irda_stream_ops = {
L
Linus Torvalds 已提交
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
	.family =	PF_IRDA,
	.owner =	THIS_MODULE,
	.release =	irda_release,
	.bind =		irda_bind,
	.connect =	irda_connect,
	.socketpair =	sock_no_socketpair,
	.accept =	irda_accept,
	.getname =	irda_getname,
	.poll =		irda_poll,
	.ioctl =	irda_ioctl,
2635 2636 2637
#ifdef CONFIG_COMPAT
	.compat_ioctl =	irda_compat_ioctl,
#endif
L
Linus Torvalds 已提交
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
	.listen =	irda_listen,
	.shutdown =	irda_shutdown,
	.setsockopt =	irda_setsockopt,
	.getsockopt =	irda_getsockopt,
	.sendmsg =	irda_sendmsg,
	.recvmsg =	irda_recvmsg_stream,
	.mmap =		sock_no_mmap,
	.sendpage =	sock_no_sendpage,
};

A
Arnd Bergmann 已提交
2648
static const struct proto_ops irda_seqpacket_ops = {
L
Linus Torvalds 已提交
2649 2650 2651 2652 2653 2654 2655 2656
	.family =	PF_IRDA,
	.owner =	THIS_MODULE,
	.release =	irda_release,
	.bind =		irda_bind,
	.connect =	irda_connect,
	.socketpair =	sock_no_socketpair,
	.accept =	irda_accept,
	.getname =	irda_getname,
2657
	.poll =		datagram_poll,
L
Linus Torvalds 已提交
2658
	.ioctl =	irda_ioctl,
2659 2660 2661
#ifdef CONFIG_COMPAT
	.compat_ioctl =	irda_compat_ioctl,
#endif
L
Linus Torvalds 已提交
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
	.listen =	irda_listen,
	.shutdown =	irda_shutdown,
	.setsockopt =	irda_setsockopt,
	.getsockopt =	irda_getsockopt,
	.sendmsg =	irda_sendmsg,
	.recvmsg =	irda_recvmsg_dgram,
	.mmap =		sock_no_mmap,
	.sendpage =	sock_no_sendpage,
};

A
Arnd Bergmann 已提交
2672
static const struct proto_ops irda_dgram_ops = {
L
Linus Torvalds 已提交
2673 2674 2675 2676 2677 2678 2679 2680
	.family =	PF_IRDA,
	.owner =	THIS_MODULE,
	.release =	irda_release,
	.bind =		irda_bind,
	.connect =	irda_connect,
	.socketpair =	sock_no_socketpair,
	.accept =	irda_accept,
	.getname =	irda_getname,
2681
	.poll =		datagram_poll,
L
Linus Torvalds 已提交
2682
	.ioctl =	irda_ioctl,
2683 2684 2685
#ifdef CONFIG_COMPAT
	.compat_ioctl =	irda_compat_ioctl,
#endif
L
Linus Torvalds 已提交
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
	.listen =	irda_listen,
	.shutdown =	irda_shutdown,
	.setsockopt =	irda_setsockopt,
	.getsockopt =	irda_getsockopt,
	.sendmsg =	irda_sendmsg_dgram,
	.recvmsg =	irda_recvmsg_dgram,
	.mmap =		sock_no_mmap,
	.sendpage =	sock_no_sendpage,
};

#ifdef CONFIG_IRDA_ULTRA
A
Arnd Bergmann 已提交
2697
static const struct proto_ops irda_ultra_ops = {
L
Linus Torvalds 已提交
2698 2699 2700 2701 2702 2703 2704 2705
	.family =	PF_IRDA,
	.owner =	THIS_MODULE,
	.release =	irda_release,
	.bind =		irda_bind,
	.connect =	sock_no_connect,
	.socketpair =	sock_no_socketpair,
	.accept =	sock_no_accept,
	.getname =	irda_getname,
2706
	.poll =		datagram_poll,
L
Linus Torvalds 已提交
2707
	.ioctl =	irda_ioctl,
2708 2709 2710
#ifdef CONFIG_COMPAT
	.compat_ioctl =	irda_compat_ioctl,
#endif
L
Linus Torvalds 已提交
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
	.listen =	sock_no_listen,
	.shutdown =	irda_shutdown,
	.setsockopt =	irda_setsockopt,
	.getsockopt =	irda_getsockopt,
	.sendmsg =	irda_sendmsg_ultra,
	.recvmsg =	irda_recvmsg_dgram,
	.mmap =		sock_no_mmap,
	.sendpage =	sock_no_sendpage,
};
#endif /* CONFIG_IRDA_ULTRA */

/*
 * Function irsock_init (pro)
 *
 *    Initialize IrDA protocol
 *
 */
int __init irsock_init(void)
{
	int rc = proto_register(&irda_proto, 0);

	if (rc == 0)
		rc = sock_register(&irda_family_ops);

	return rc;
}

/*
 * Function irsock_cleanup (void)
 *
 *    Remove IrDA protocol
 *
 */
S
Samuel Ortiz 已提交
2744
void irsock_cleanup(void)
L
Linus Torvalds 已提交
2745 2746 2747 2748
{
	sock_unregister(PF_IRDA);
	proto_unregister(&irda_proto);
}