af_llc.c 31.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * af_llc.c - LLC User Interface SAPs
 * Description:
 *   Functions in this module are implementation of socket based llc
 *   communications for the Linux operating system. Support of llc class
 *   one and class two is provided via SOCK_DGRAM and SOCK_STREAM
 *   respectively.
 *
 *   An llc2 connection is (mac + sap), only one llc2 sap connection
 *   is allowed per mac. Though one sap may have multiple mac + sap
 *   connections.
 *
 * Copyright (c) 2001 by Jay Schulist <jschlst@samba.org>
 *		 2002-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *
 * This program can be redistributed or modified under the terms of the
 * GNU General Public License as published by the Free Software Foundation.
 * This program is distributed without any warranty or implied warranty
 * of merchantability or fitness for a particular purpose.
 *
 * See the GNU General Public License for more details.
 */
23
#include <linux/compiler.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/rtnetlink.h>
#include <linux/init.h>
28
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32
#include <net/llc.h>
#include <net/llc_sap.h>
#include <net/llc_pdu.h>
#include <net/llc_conn.h>
33
#include <net/tcp_states.h>
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38

/* remember: uninitialized global data is zeroed because its in .bss */
static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
static u16 llc_ui_sap_link_no_max[256];
static struct sockaddr_llc llc_ui_addrnull;
39
static const struct proto_ops llc_ui_ops;
Linus Torvalds's avatar
Linus Torvalds committed
40

41 42 43
static int llc_ui_wait_for_conn(struct sock *sk, long timeout);
static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49 50

#if 0
#define dprintk(args...) printk(KERN_DEBUG args)
#else
#define dprintk(args...)
#endif

51 52 53 54
/* Maybe we'll add some more in the future. */
#define LLC_CMSG_PKTINFO	1


Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60
/**
 *	llc_ui_next_link_no - return the next unused link number for a sap
 *	@sap: Address of sap to get link number from.
 *
 *	Return the next unused link number for a given sap.
 */
61
static inline u16 llc_ui_next_link_no(int sap)
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64 65 66 67 68 69 70 71
{
	return llc_ui_sap_link_no_max[sap]++;
}

/**
 *	llc_proto_type - return eth protocol for ARP header type
 *	@arphrd: ARP header type.
 *
 *	Given an ARP header type return the corresponding ethernet protocol.
 */
Al Viro's avatar
Al Viro committed
72
static inline __be16 llc_proto_type(u16 arphrd)
Linus Torvalds's avatar
Linus Torvalds committed
73
{
74
	return htons(ETH_P_802_2);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79 80
}

/**
 *	llc_ui_addr_null - determines if a address structure is null
 *	@addr: Address to test if null.
 */
81
static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85 86 87 88 89 90 91 92 93 94
{
	return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
}

/**
 *	llc_ui_header_len - return length of llc header based on operation
 *	@sk: Socket which contains a valid llc socket type.
 *	@addr: Complete sockaddr_llc structure received from the user.
 *
 *	Provide the length of the llc header depending on what kind of
 *	operation the user would like to perform and the type of socket.
 *	Returns the correct llc header length.
 */
95
static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
Linus Torvalds's avatar
Linus Torvalds committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
	u8 rc = LLC_PDU_LEN_U;

	if (addr->sllc_test || addr->sllc_xid)
		rc = LLC_PDU_LEN_U;
	else if (sk->sk_type == SOCK_STREAM)
		rc = LLC_PDU_LEN_I;
	return rc;
}

/**
 *	llc_ui_send_data - send data via reliable llc2 connection
 *	@sk: Connection the socket is using.
 *	@skb: Data the user wishes to send.
 *	@noblock: can we block waiting for data?
 *
 *	Send data via reliable llc2 connection.
 *	Returns 0 upon success, non-zero if action did not succeed.
 */
static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
{
	struct llc_sock* llc = llc_sk(sk);
	int rc = 0;

120
	if (unlikely(llc_data_accept_state(llc->state) ||
121 122
		     llc->remote_busy_flag ||
		     llc->p_flag)) {
123
		long timeout = sock_sndtimeo(sk, noblock);
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126

		rc = llc_ui_wait_for_busy_core(sk, timeout);
	}
127
	if (unlikely(!rc))
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133
		rc = llc_build_and_send_pkt(sk, skb);
	return rc;
}

static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
{
134
	sock_graft(sk, sock);
Linus Torvalds's avatar
Linus Torvalds committed
135 136 137 138 139
	sk->sk_type	= sock->type;
	sock->ops	= &llc_ui_ops;
}

static struct proto llc_proto = {
140
	.name	  = "LLC",
Linus Torvalds's avatar
Linus Torvalds committed
141 142
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct llc_sock),
143
	.slab_flags = SLAB_DESTROY_BY_RCU,
Linus Torvalds's avatar
Linus Torvalds committed
144 145 146 147
};

/**
 *	llc_ui_create - alloc and init a new llc_ui socket
148
 *	@net: network namespace (must be default network)
Linus Torvalds's avatar
Linus Torvalds committed
149 150
 *	@sock: Socket to initialize and attach allocated sk to.
 *	@protocol: Unused.
151
 *	@kern: on behalf of kernel or userspace
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155 156
 *
 *	Allocate and initialize a new llc_ui socket, validate the user wants a
 *	socket type we have available.
 *	Returns 0 upon success, negative upon failure.
 */
157 158
static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
			 int kern)
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161 162
{
	struct sock *sk;
	int rc = -ESOCKTNOSUPPORT;

163
	if (!ns_capable(net->user_ns, CAP_NET_RAW))
164 165
		return -EPERM;

166
	if (!net_eq(net, &init_net))
167 168
		return -EAFNOSUPPORT;

169
	if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
Linus Torvalds's avatar
Linus Torvalds committed
170
		rc = -ENOMEM;
171
		sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto);
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		if (sk) {
			rc = 0;
			llc_ui_sk_init(sock, sk);
		}
	}
	return rc;
}

/**
 *	llc_ui_release - shutdown socket
 *	@sock: Socket to release.
 *
 *	Shutdown and deallocate an existing socket.
 */
static int llc_ui_release(struct socket *sock)
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc;

191
	if (unlikely(sk == NULL))
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195
		goto out;
	sock_hold(sk);
	lock_sock(sk);
	llc = llc_sk(sk);
196
	dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199
		llc->laddr.lsap, llc->daddr.lsap);
	if (!llc_send_disc(sk))
		llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
200
	if (!sock_flag(sk, SOCK_ZAPPED))
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		llc_sap_remove_socket(llc->sap, sk);
	release_sock(sk);
	if (llc->dev)
		dev_put(llc->dev);
	sock_put(sk);
	llc_sk_free(sk);
out:
	return 0;
}

/**
 *	llc_ui_autoport - provide dynamically allocate SAP number
 *
 *	Provide the caller with a dynamically allocated SAP number according
 *	to the rules that are set in this function. Returns: 0, upon failure,
 *	SAP number otherwise.
 */
static int llc_ui_autoport(void)
{
	struct llc_sap *sap;
	int i, tries = 0;

	while (tries < LLC_SAP_DYN_TRIES) {
		for (i = llc_ui_sap_last_autoport;
		     i < LLC_SAP_DYN_STOP; i += 2) {
			sap = llc_sap_find(i);
			if (!sap) {
				llc_ui_sap_last_autoport = i + 2;
				goto out;
			}
231
			llc_sap_put(sap);
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236 237 238 239 240 241
		}
		llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
		tries++;
	}
	i = 0;
out:
	return i;
}

/**
242 243 244 245 246 247
 *	llc_ui_autobind - automatically bind a socket to a sap
 *	@sock: socket to bind
 *	@addr: address to connect to
 *
 * 	Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
 * 	specifically used llc_ui_bind to bind to an specific address/sap
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250 251 252 253 254 255 256 257 258 259 260
 *
 *	Returns: 0 upon success, negative otherwise.
 */
static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
	struct llc_sap *sap;
	int rc = -EINVAL;

	if (!sock_flag(sk, SOCK_ZAPPED))
		goto out;
	rc = -ENODEV;
261 262 263 264 265 266 267 268
	if (sk->sk_bound_dev_if) {
		llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
		if (llc->dev && addr->sllc_arphrd != llc->dev->type) {
			dev_put(llc->dev);
			llc->dev = NULL;
		}
	} else
		llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	if (!llc->dev)
		goto out;
	rc = -EUSERS;
	llc->laddr.lsap = llc_ui_autoport();
	if (!llc->laddr.lsap)
		goto out;
	rc = -EBUSY; /* some other network layer is using the sap */
	sap = llc_sap_open(llc->laddr.lsap, NULL);
	if (!sap)
		goto out;
	memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
	memcpy(&llc->addr, addr, sizeof(llc->addr));
	/* assign new connection to its SAP */
	llc_sap_add_socket(sap, sk);
	sock_reset_flag(sk, SOCK_ZAPPED);
	rc = 0;
out:
	return rc;
}

/**
 *	llc_ui_bind - bind a socket to a specific address.
 *	@sock: Socket to bind an address to.
 *	@uaddr: Address the user wants the socket bound to.
 *	@addrlen: Length of the uaddr structure.
 *
 *	Bind a socket to a specific address. For llc a user is able to bind to
296
 *	a specific sap only or mac + sap.
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
 *	If the user desires to bind to a specific mac + sap, it is possible to
 *	have multiple sap connections via multiple macs.
 *	Bind and autobind for that matter must enforce the correct sap usage
 *	otherwise all hell will break loose.
 *	Returns: 0 upon success, negative otherwise.
 */
static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
{
	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
	struct llc_sap *sap;
	int rc = -EINVAL;

311
	dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
312
	if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
Linus Torvalds's avatar
Linus Torvalds committed
313 314
		goto out;
	rc = -EAFNOSUPPORT;
315
	if (unlikely(addr->sllc_family != AF_LLC))
Linus Torvalds's avatar
Linus Torvalds committed
316
		goto out;
317
	rc = -ENODEV;
318
	rcu_read_lock();
319
	if (sk->sk_bound_dev_if) {
320
		llc->dev = dev_get_by_index_rcu(&init_net, sk->sk_bound_dev_if);
321 322 323
		if (llc->dev) {
			if (!addr->sllc_arphrd)
				addr->sllc_arphrd = llc->dev->type;
324
			if (is_zero_ether_addr(addr->sllc_mac))
325 326 327
				memcpy(addr->sllc_mac, llc->dev->dev_addr,
				       IFHWADDRLEN);
			if (addr->sllc_arphrd != llc->dev->type ||
328 329
			    !ether_addr_equal(addr->sllc_mac,
					      llc->dev->dev_addr)) {
330 331 332 333 334
				rc = -EINVAL;
				llc->dev = NULL;
			}
		}
	} else
335
		llc->dev = dev_getbyhwaddr_rcu(&init_net, addr->sllc_arphrd,
336
					   addr->sllc_mac);
337 338 339
	if (llc->dev)
		dev_hold(llc->dev);
	rcu_read_unlock();
340 341
	if (!llc->dev)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	if (!addr->sllc_sap) {
		rc = -EUSERS;
		addr->sllc_sap = llc_ui_autoport();
		if (!addr->sllc_sap)
			goto out;
	}
	sap = llc_sap_find(addr->sllc_sap);
	if (!sap) {
		sap = llc_sap_open(addr->sllc_sap, NULL);
		rc = -EBUSY; /* some other network layer is using the sap */
		if (!sap)
			goto out;
	} else {
		struct llc_addr laddr, daddr;
		struct sock *ask;

		memset(&laddr, 0, sizeof(laddr));
		memset(&daddr, 0, sizeof(daddr));
		/*
361
		 * FIXME: check if the address is multicast,
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365 366 367 368 369
		 * 	  only SOCK_DGRAM can do this.
		 */
		memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
		laddr.lsap = addr->sllc_sap;
		rc = -EADDRINUSE; /* mac + sap clash. */
		ask = llc_lookup_established(sap, &daddr, &laddr);
		if (ask) {
			sock_put(ask);
370
			goto out_put;
Linus Torvalds's avatar
Linus Torvalds committed
371 372 373 374 375 376 377 378 379
		}
	}
	llc->laddr.lsap = addr->sllc_sap;
	memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
	memcpy(&llc->addr, addr, sizeof(llc->addr));
	/* assign new connection to its SAP */
	llc_sap_add_socket(sap, sk);
	sock_reset_flag(sk, SOCK_ZAPPED);
	rc = 0;
380 381
out_put:
	llc_sap_put(sap);
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
out:
	return rc;
}

/**
 *	llc_ui_shutdown - shutdown a connect llc2 socket.
 *	@sock: Socket to shutdown.
 *	@how: What part of the socket to shutdown.
 *
 *	Shutdown a connected llc2 socket. Currently this function only supports
 *	shutting down both sends and receives (2), we could probably make this
 *	function such that a user can shutdown only half the connection but not
 *	right now.
 *	Returns: 0 upon success, negative otherwise.
 */
static int llc_ui_shutdown(struct socket *sock, int how)
{
	struct sock *sk = sock->sk;
	int rc = -ENOTCONN;

	lock_sock(sk);
403
	if (unlikely(sk->sk_state != TCP_ESTABLISHED))
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
		goto out;
	rc = -EINVAL;
	if (how != 2)
		goto out;
	rc = llc_send_disc(sk);
	if (!rc)
		rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
	/* Wake up anyone sleeping in poll */
	sk->sk_state_change(sk);
out:
	release_sock(sk);
	return rc;
}

/**
 *	llc_ui_connect - Connect to a remote llc2 mac + sap.
 *	@sock: Socket which will be connected to the remote destination.
 *	@uaddr: Remote and possibly the local address of the new connection.
 *	@addrlen: Size of uaddr structure.
 *	@flags: Operational flags specified by the user.
 *
 *	Connect to a remote llc2 mac + sap. The caller must specify the
 *	destination mac and address to connect to. If the user hasn't previously
 *	called bind(2) with a smac the address of the first interface of the
 *	specified arp type will be used.
 *	This function will autobind if user did not previously call bind.
 *	Returns: 0 upon success, negative otherwise.
 */
static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
			  int addrlen, int flags)
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
	int rc = -EINVAL;

	lock_sock(sk);
441
	if (unlikely(addrlen != sizeof(*addr)))
Linus Torvalds's avatar
Linus Torvalds committed
442 443
		goto out;
	rc = -EAFNOSUPPORT;
444 445 446 447 448 449
	if (unlikely(addr->sllc_family != AF_LLC))
		goto out;
	if (unlikely(sk->sk_type != SOCK_STREAM))
		goto out;
	rc = -EALREADY;
	if (unlikely(sock->state == SS_CONNECTING))
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454 455 456 457
		goto out;
	/* bind connection to sap if user hasn't done it. */
	if (sock_flag(sk, SOCK_ZAPPED)) {
		/* bind to sap with null dev, exclusive */
		rc = llc_ui_autobind(sock, addr);
		if (rc)
			goto out;
	}
458 459
	llc->daddr.lsap = addr->sllc_sap;
	memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462
	sock->state = SS_CONNECTING;
	sk->sk_state   = TCP_SYN_SENT;
	llc->link   = llc_ui_next_link_no(llc->sap->laddr.lsap);
463
	rc = llc_establish_connection(sk, llc->dev->dev_addr,
Linus Torvalds's avatar
Linus Torvalds committed
464 465
				      addr->sllc_mac, addr->sllc_sap);
	if (rc) {
466
		dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469 470
		sock->state  = SS_UNCONNECTED;
		sk->sk_state = TCP_CLOSE;
		goto out;
	}
471 472

	if (sk->sk_state == TCP_SYN_SENT) {
473
		const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
474 475 476 477 478 479 480 481 482 483 484 485 486 487

		if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
			goto out;

		rc = sock_intr_errno(timeo);
		if (signal_pending(current))
			goto out;
	}

	if (sk->sk_state == TCP_CLOSE)
		goto sock_error;

	sock->state = SS_CONNECTED;
	rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
488 489 490
out:
	release_sock(sk);
	return rc;
491 492 493 494
sock_error:
	rc = sock_error(sk) ? : -ECONNABORTED;
	sock->state = SS_UNCONNECTED;
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
}

/**
 *	llc_ui_listen - allow a normal socket to accept incoming connections
 *	@sock: Socket to allow incoming connections on.
 *	@backlog: Number of connections to queue.
 *
 *	Allow a normal socket to accept incoming connections.
 *	Returns 0 upon success, negative otherwise.
 */
static int llc_ui_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	int rc = -EINVAL;

	lock_sock(sk);
511
	if (unlikely(sock->state != SS_UNCONNECTED))
Linus Torvalds's avatar
Linus Torvalds committed
512 513
		goto out;
	rc = -EOPNOTSUPP;
514
	if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds's avatar
Linus Torvalds committed
515 516 517 518 519
		goto out;
	rc = -EAGAIN;
	if (sock_flag(sk, SOCK_ZAPPED))
		goto out;
	rc = 0;
520
	if (!(unsigned int)backlog)	/* BSDism */
Linus Torvalds's avatar
Linus Torvalds committed
521 522 523 524 525 526 527 528 529 530 531 532
		backlog = 1;
	sk->sk_max_ack_backlog = backlog;
	if (sk->sk_state != TCP_LISTEN) {
		sk->sk_ack_backlog = 0;
		sk->sk_state	   = TCP_LISTEN;
	}
	sk->sk_socket->flags |= __SO_ACCEPTCON;
out:
	release_sock(sk);
	return rc;
}

533
static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
Linus Torvalds's avatar
Linus Torvalds committed
534
{
535 536
	DEFINE_WAIT(wait);
	int rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
537

538
	while (1) {
Eric Dumazet's avatar
Eric Dumazet committed
539
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
540 541
		if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
			break;
Linus Torvalds's avatar
Linus Torvalds committed
542 543 544 545 546 547
		rc = -ERESTARTSYS;
		if (signal_pending(current))
			break;
		rc = -EAGAIN;
		if (!timeout)
			break;
548
		rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
549
	}
Eric Dumazet's avatar
Eric Dumazet committed
550
	finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
551 552 553
	return rc;
}

554
static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
Linus Torvalds's avatar
Linus Torvalds committed
555
{
556
	DEFINE_WAIT(wait);
Linus Torvalds's avatar
Linus Torvalds committed
557

558
	while (1) {
Eric Dumazet's avatar
Eric Dumazet committed
559
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
560 561
		if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
			break;
562
		if (signal_pending(current) || !timeout)
Linus Torvalds's avatar
Linus Torvalds committed
563 564
			break;
	}
Eric Dumazet's avatar
Eric Dumazet committed
565
	finish_wait(sk_sleep(sk), &wait);
566
	return timeout;
Linus Torvalds's avatar
Linus Torvalds committed
567 568
}

569
static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
Linus Torvalds's avatar
Linus Torvalds committed
570
{
571
	DEFINE_WAIT(wait);
572 573
	struct llc_sock *llc = llc_sk(sk);
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
574

575
	while (1) {
Eric Dumazet's avatar
Eric Dumazet committed
576
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
577
		rc = 0;
578 579
		if (sk_wait_event(sk, &timeout,
				  (sk->sk_shutdown & RCV_SHUTDOWN) ||
580
				  (!llc_data_accept_state(llc->state) &&
581
				   !llc->remote_busy_flag &&
582
				   !llc->p_flag)))
Linus Torvalds's avatar
Linus Torvalds committed
583 584 585 586 587 588 589 590
			break;
		rc = -ERESTARTSYS;
		if (signal_pending(current))
			break;
		rc = -EAGAIN;
		if (!timeout)
			break;
	}
Eric Dumazet's avatar
Eric Dumazet committed
591
	finish_wait(sk_sleep(sk), &wait);
Linus Torvalds's avatar
Linus Torvalds committed
592 593 594
	return rc;
}

595
static int llc_wait_data(struct sock *sk, long timeo)
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598
{
	int rc;

599
	while (1) {
600 601 602
		/*
		 * POSIX 1003.1g mandates this order.
		 */
603 604
		rc = sock_error(sk);
		if (rc)
605
			break;
Linus Torvalds's avatar
Linus Torvalds committed
606
		rc = 0;
607
		if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvalds's avatar
Linus Torvalds committed
608
			break;
609 610 611 612
		rc = -EAGAIN;
		if (!timeo)
			break;
		rc = sock_intr_errno(timeo);
Linus Torvalds's avatar
Linus Torvalds committed
613 614
		if (signal_pending(current))
			break;
615 616
		rc = 0;
		if (sk_wait_data(sk, &timeo))
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 621
			break;
	}
	return rc;
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635
static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
{
	struct llc_sock *llc = llc_sk(skb->sk);

	if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
		struct llc_pktinfo info;

		info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
		llc_pdu_decode_dsap(skb, &info.lpi_sap);
		llc_pdu_decode_da(skb, info.lpi_mac);
		put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
/**
 *	llc_ui_accept - accept a new incoming connection.
 *	@sock: Socket which connections arrive on.
 *	@newsock: Socket to move incoming connection to.
 *	@flags: User specified operational flags.
 *
 *	Accept a new incoming connection.
 *	Returns 0 upon success, negative otherwise.
 */
static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
{
	struct sock *sk = sock->sk, *newsk;
	struct llc_sock *llc, *newllc;
	struct sk_buff *skb;
	int rc = -EOPNOTSUPP;

652
	dprintk("%s: accepting on %02X\n", __func__,
653
		llc_sk(sk)->laddr.lsap);
Linus Torvalds's avatar
Linus Torvalds committed
654
	lock_sock(sk);
655
	if (unlikely(sk->sk_type != SOCK_STREAM))
Linus Torvalds's avatar
Linus Torvalds committed
656 657
		goto out;
	rc = -EINVAL;
658 659
	if (unlikely(sock->state != SS_UNCONNECTED ||
		     sk->sk_state != TCP_LISTEN))
Linus Torvalds's avatar
Linus Torvalds committed
660 661
		goto out;
	/* wait for a connection to arrive. */
662
	if (skb_queue_empty(&sk->sk_receive_queue)) {
663
		rc = llc_wait_data(sk, sk->sk_rcvtimeo);
664 665 666
		if (rc)
			goto out;
	}
667
	dprintk("%s: got a new connection on %02X\n", __func__,
668
		llc_sk(sk)->laddr.lsap);
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	skb = skb_dequeue(&sk->sk_receive_queue);
	rc = -EINVAL;
	if (!skb->sk)
		goto frees;
	rc = 0;
	newsk = skb->sk;
	/* attach connection to a new socket. */
	llc_ui_sk_init(newsock, newsk);
	sock_reset_flag(newsk, SOCK_ZAPPED);
	newsk->sk_state		= TCP_ESTABLISHED;
	newsock->state		= SS_CONNECTED;
	llc			= llc_sk(sk);
	newllc			= llc_sk(newsk);
	memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
	newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);

	/* put original socket back into a clean listen state. */
	sk->sk_state = TCP_LISTEN;
	sk->sk_ack_backlog--;
688
	dprintk("%s: ok success on %02X, client on %02X\n", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691 692 693 694 695 696 697 698 699 700
		llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
frees:
	kfree_skb(skb);
out:
	release_sock(sk);
	return rc;
}

/**
 *	llc_ui_recvmsg - copy received data to the socket user.
 *	@sock: Socket to copy data from.
 *	@msg: Various user space related information.
701
 *	@len: Size of user buffer.
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705 706 707
 *	@flags: User specified flags.
 *
 *	Copy received data to the socket user.
 *	Returns non-negative upon success, negative otherwise.
 */
static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
708
			  struct msghdr *msg, size_t len, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
709 710
{
	struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name;
711 712 713 714
	const int nonblock = flags & MSG_DONTWAIT;
	struct sk_buff *skb = NULL;
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
715
	unsigned long cpu_flags;
Linus Torvalds's avatar
Linus Torvalds committed
716
	size_t copied = 0;
717
	u32 peek_seq = 0;
718
	u32 *seq, skb_len;
719 720 721
	unsigned long used;
	int target;	/* Read at least this many bytes */
	long timeo;
Linus Torvalds's avatar
Linus Torvalds committed
722 723

	lock_sock(sk);
724
	copied = -ENOTCONN;
725
	if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
Linus Torvalds's avatar
Linus Torvalds committed
726
		goto out;
727 728 729 730 731 732 733

	timeo = sock_rcvtimeo(sk, nonblock);

	seq = &llc->copied_seq;
	if (flags & MSG_PEEK) {
		peek_seq = llc->copied_seq;
		seq = &peek_seq;
734
	}
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
	copied = 0;

	do {
		u32 offset;

		/*
		 * We need to check signals first, to get correct SIGURG
		 * handling. FIXME: Need to check this doesn't impact 1003.1g
		 * and move it down to the bottom of the loop
		 */
		if (signal_pending(current)) {
			if (copied)
				break;
			copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
			break;
		}

		/* Next get a buffer. */

		skb = skb_peek(&sk->sk_receive_queue);
		if (skb) {
			offset = *seq;
			goto found_ok_skb;
		}
		/* Well, if we have backlog, try to process it now yet. */

		if (copied >= target && !sk->sk_backlog.tail)
			break;

		if (copied) {
			if (sk->sk_err ||
			    sk->sk_state == TCP_CLOSE ||
			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
			    !timeo ||
			    (flags & MSG_PEEK))
				break;
		} else {
			if (sock_flag(sk, SOCK_DONE))
				break;

			if (sk->sk_err) {
				copied = sock_error(sk);
				break;
			}
			if (sk->sk_shutdown & RCV_SHUTDOWN)
				break;

784
			if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
				if (!sock_flag(sk, SOCK_DONE)) {
					/*
					 * This occurs when user tries to read
					 * from never connected socket.
					 */
					copied = -ENOTCONN;
					break;
				}
				break;
			}
			if (!timeo) {
				copied = -EAGAIN;
				break;
			}
		}

		if (copied >= target) { /* Do not sleep, just process backlog. */
			release_sock(sk);
			lock_sock(sk);
		} else
			sk_wait_data(sk, &timeo);

		if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
808 809 810
			net_dbg_ratelimited("LLC(%s:%d): Application bug, race in MSG_PEEK\n",
					    current->comm,
					    task_pid_nr(current));
811 812 813 814
			peek_seq = llc->copied_seq;
		}
		continue;
	found_ok_skb:
815
		skb_len = skb->len;
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		/* Ok so how much can we use? */
		used = skb->len - offset;
		if (len < used)
			used = len;

		if (!(flags & MSG_TRUNC)) {
			int rc = skb_copy_datagram_iovec(skb, offset,
							 msg->msg_iov, used);
			if (rc) {
				/* Exception. Bailout! */
				if (!copied)
					copied = -EFAULT;
				break;
			}
		}

		*seq += used;
		copied += used;
		len -= used;

836 837 838 839
		/* For non stream protcols we get one packet per recvmsg call */
		if (sk->sk_type != SOCK_STREAM)
			goto copy_uaddr;

840
		if (!(flags & MSG_PEEK)) {
841
			spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
Eric Dumazet's avatar
Eric Dumazet committed
842
			sk_eat_skb(sk, skb, false);
843
			spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
844 845
			*seq = 0;
		}
846 847

		/* Partial read */
848
		if (used + offset < skb_len)
849
			continue;
850 851
	} while (len > 0);

Linus Torvalds's avatar
Linus Torvalds committed
852 853
out:
	release_sock(sk);
854 855 856 857 858 859
	return copied;
copy_uaddr:
	if (uaddr != NULL && skb != NULL) {
		memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
		msg->msg_namelen = sizeof(*uaddr);
	}
860 861
	if (llc_sk(sk)->cmsg_flags)
		llc_cmsg_rcv(msg, skb);
862 863

	if (!(flags & MSG_PEEK)) {
864
			spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
Eric Dumazet's avatar
Eric Dumazet committed
865
			sk_eat_skb(sk, skb, false);
866
			spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
867 868 869
			*seq = 0;
	}

870
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
}

/**
 *	llc_ui_sendmsg - Transmit data provided by the socket user.
 *	@sock: Socket to transmit data from.
 *	@msg: Various user related information.
 *	@len: Length of data to transmit.
 *
 *	Transmit data provided by the socket user.
 *	Returns non-negative upon success, negative otherwise.
 */
static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
			  struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
	struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name;
	int flags = msg->msg_flags;
	int noblock = flags & MSG_DONTWAIT;
	struct sk_buff *skb;
	size_t size = 0;
	int rc = -EINVAL, copied = 0, hdrlen;

894
	dprintk("%s: sending from %02X to %02X\n", __func__,
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
		llc->laddr.lsap, llc->daddr.lsap);
	lock_sock(sk);
	if (addr) {
		if (msg->msg_namelen < sizeof(*addr))
			goto release;
	} else {
		if (llc_ui_addr_null(&llc->addr))
			goto release;
		addr = &llc->addr;
	}
	/* must bind connection to sap if user hasn't done it. */
	if (sock_flag(sk, SOCK_ZAPPED)) {
		/* bind to sap with null dev, exclusive. */
		rc = llc_ui_autobind(sock, addr);
		if (rc)
			goto release;
	}
912
	hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
Linus Torvalds's avatar
Linus Torvalds committed
913
	size = hdrlen + len;
914 915
	if (size > llc->dev->mtu)
		size = llc->dev->mtu;
Linus Torvalds's avatar
Linus Torvalds committed
916 917 918 919 920 921
	copied = size - hdrlen;
	release_sock(sk);
	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
	lock_sock(sk);
	if (!skb)
		goto release;
922
	skb->dev      = llc->dev;
Linus Torvalds's avatar
Linus Torvalds committed
923
	skb->protocol = llc_proto_type(addr->sllc_arphrd);
924
	skb_reserve(skb, hdrlen);
Linus Torvalds's avatar
Linus Torvalds committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
	if (rc)
		goto out;
	if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
		llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
					  addr->sllc_sap);
		goto out;
	}
	if (addr->sllc_test) {
		llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
					    addr->sllc_sap);
		goto out;
	}
	if (addr->sllc_xid) {
		llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
					   addr->sllc_sap);
		goto out;
	}
	rc = -ENOPROTOOPT;
	if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
		goto out;
	rc = llc_ui_send_data(sk, skb, noblock);
out:
948
	if (rc) {
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951
		kfree_skb(skb);
release:
		dprintk("%s: failed sending from %02X to %02X: %d\n",
952
			__func__, llc->laddr.lsap, llc->daddr.lsap, rc);
953
	}
Linus Torvalds's avatar
Linus Torvalds committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	release_sock(sk);
	return rc ? : copied;
}

/**
 *	llc_ui_getname - return the address info of a socket
 *	@sock: Socket to get address of.
 *	@uaddr: Address structure to return information.
 *	@uaddrlen: Length of address structure.
 *	@peer: Does user want local or remote address information.
 *
 *	Return the address information of a socket.
 */
static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
			  int *uaddrlen, int peer)
{
	struct sockaddr_llc sllc;
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
973
	int rc = -EBADF;
Linus Torvalds's avatar
Linus Torvalds committed
974

975
	memset(&sllc, 0, sizeof(sllc));
Linus Torvalds's avatar
Linus Torvalds committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	lock_sock(sk);
	if (sock_flag(sk, SOCK_ZAPPED))
		goto out;
	*uaddrlen = sizeof(sllc);
	if (peer) {
		rc = -ENOTCONN;
		if (sk->sk_state != TCP_ESTABLISHED)
			goto out;
		if(llc->dev)
			sllc.sllc_arphrd = llc->dev->type;
		sllc.sllc_sap = llc->daddr.lsap;
		memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
	} else {
		rc = -EINVAL;
		if (!llc->sap)
			goto out;
		sllc.sllc_sap = llc->sap->laddr.lsap;

		if (llc->dev) {
			sllc.sllc_arphrd = llc->dev->type;
996
			memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
			       IFHWADDRLEN);
		}
	}
	rc = 0;
	sllc.sllc_family = AF_LLC;
	memcpy(uaddr, &sllc, sizeof(sllc));
out:
	release_sock(sk);
	return rc;
}

/**
 *	llc_ui_ioctl - io controls for PF_LLC
 *	@sock: Socket to get/set info
 *	@cmd: command
 *	@arg: optional argument for cmd
 *
 *	get/set info on llc sockets
 */
static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
			unsigned long arg)
{
1019
	return -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
1020 1021 1022 1023 1024 1025 1026
}

/**
 *	llc_ui_setsockopt - set various connection specific parameters.
 *	@sock: Socket to set options on.
 *	@level: Socket level user is requesting operations on.
 *	@optname: Operation name.
1027
 *	@optval: User provided operation data.
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029 1030 1031 1032
 *	@optlen: Length of optval.
 *
 *	Set various connection specific parameters.
 */
static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
1033
			     char __user *optval, unsigned int optlen)
Linus Torvalds's avatar
Linus Torvalds committed
1034 1035 1036
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
1037 1038
	unsigned int opt;
	int rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040

	lock_sock(sk);
1041
	if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
		goto out;
	rc = get_user(opt, (int __user *)optval);
	if (rc)
		goto out;
	rc = -EINVAL;
	switch (optname) {
	case LLC_OPT_RETRY:
		if (opt > LLC_OPT_MAX_RETRY)
			goto out;
		llc->n2 = opt;
		break;
	case LLC_OPT_SIZE:
		if (opt > LLC_OPT_MAX_SIZE)
			goto out;
		llc->n1 = opt;
		break;
	case LLC_OPT_ACK_TMR_EXP:
		if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
			goto out;
1061
		llc->ack_timer.expire = opt * HZ;
Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064 1065
		break;
	case LLC_OPT_P_TMR_EXP:
		if (opt > LLC_OPT_MAX_P_TMR_EXP)
			goto out;
1066
		llc->pf_cycle_timer.expire = opt * HZ;
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069 1070
		break;
	case LLC_OPT_REJ_TMR_EXP:
		if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
			goto out;
1071
		llc->rej_sent_timer.expire = opt * HZ;
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073 1074 1075
		break;
	case LLC_OPT_BUSY_TMR_EXP:
		if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
			goto out;
1076
		llc->busy_state_timer.expire = opt * HZ;
Linus Torvalds's avatar
Linus Torvalds committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		break;
	case LLC_OPT_TX_WIN:
		if (opt > LLC_OPT_MAX_WIN)
			goto out;
		llc->k = opt;
		break;
	case LLC_OPT_RX_WIN:
		if (opt > LLC_OPT_MAX_WIN)
			goto out;
		llc->rw = opt;
		break;
1088 1089 1090 1091 1092 1093
	case LLC_OPT_PKTINFO:
		if (opt)
			llc->cmsg_flags |= LLC_CMSG_PKTINFO;
		else
			llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	default:
		rc = -ENOPROTOOPT;
		goto out;
	}
	rc = 0;
out:
	release_sock(sk);
	return rc;
}

/**
 *	llc_ui_getsockopt - get connection specific socket info
 *	@sock: Socket to get information from.
 *	@level: Socket level user is requesting operations on.
 *	@optname: Operation name.
 *	@optval: Variable to return operation data in.
 *	@optlen: Length of optval.
 *
 *	Get connection specific socket information.
 */
static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
			     char __user *optval, int __user *optlen)
{
	struct sock *sk = sock->sk;
	struct llc_sock *llc = llc_sk(sk);
	int val = 0, len = 0, rc = -EINVAL;

	lock_sock(sk);
1122
	if (unlikely(level != SOL_LLC))
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124 1125 1126 1127 1128 1129 1130 1131
		goto out;
	rc = get_user(len, optlen);
	if (rc)
		goto out;
	rc = -EINVAL;
	if (len != sizeof(int))
		goto out;
	switch (optname) {
	case LLC_OPT_RETRY:
1132
		val = llc->n2;					break;
Linus Torvalds's avatar
Linus Torvalds committed
1133
	case LLC_OPT_SIZE:
1134
		val = llc->n1;					break;
Linus Torvalds's avatar
Linus Torvalds committed
1135
	case LLC_OPT_ACK_TMR_EXP:
1136
		val = llc->ack_timer.expire / HZ;		break;
Linus Torvalds's avatar
Linus Torvalds committed
1137
	case LLC_OPT_P_TMR_EXP:
1138
		val = llc->pf_cycle_timer.expire / HZ;		break;
Linus Torvalds's avatar
Linus Torvalds committed
1139
	case LLC_OPT_REJ_TMR_EXP:
1140
		val = llc->rej_sent_timer.expire / HZ;		break;
Linus Torvalds's avatar
Linus Torvalds committed
1141
	case LLC_OPT_BUSY_TMR_EXP:
1142
		val = llc->busy_state_timer.expire / HZ;	break;
Linus Torvalds's avatar
Linus Torvalds committed
1143 1144 1145 1146
	case LLC_OPT_TX_WIN:
		val = llc->k;				break;
	case LLC_OPT_RX_WIN:
		val = llc->rw;				break;
1147 1148 1149
	case LLC_OPT_PKTINFO:
		val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	default:
		rc = -ENOPROTOOPT;
		goto out;
	}
	rc = 0;
	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
		rc = -EFAULT;
out:
	release_sock(sk);
	return rc;
}

1162
static const struct net_proto_family llc_ui_family_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164 1165 1166 1167
	.family = PF_LLC,
	.create = llc_ui_create,
	.owner	= THIS_MODULE,
};

1168
static const struct proto_ops llc_ui_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	.family	     = PF_LLC,
	.owner       = THIS_MODULE,
	.release     = llc_ui_release,
	.bind	     = llc_ui_bind,
	.connect     = llc_ui_connect,
	.socketpair  = sock_no_socketpair,
	.accept      = llc_ui_accept,
	.getname     = llc_ui_getname,
	.poll	     = datagram_poll,
	.ioctl       = llc_ui_ioctl,
	.listen      = llc_ui_listen,
	.shutdown    = llc_ui_shutdown,
	.setsockopt  = llc_ui_setsockopt,
	.getsockopt  = llc_ui_getsockopt,
	.sendmsg     = llc_ui_sendmsg,
	.recvmsg     = llc_ui_recvmsg,
	.mmap	     = sock_no_mmap,
	.sendpage    = sock_no_sendpage,
};

1189
static const char llc_proc_err_msg[] __initconst =
1190
	KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
1191
static const char llc_sysctl_err_msg[] __initconst =
1192
	KERN_CRIT "LLC: Unable to register the sysctl entries\n";
1193
static const char llc_sock_err_msg[] __initconst =
1194
	KERN_CRIT "LLC: Unable to register the network family\n";
1195

Linus Torvalds's avatar
Linus Torvalds committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
static int __init llc2_init(void)
{
	int rc = proto_register(&llc_proto, 0);

	if (rc != 0)
		goto out;

	llc_build_offset_table();
	llc_station_init();
	llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
	rc = llc_proc_init();
1207 1208
	if (rc != 0) {
		printk(llc_proc_err_msg);
1209
		goto out_station;
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	}
	rc = llc_sysctl_init();
	if (rc) {
		printk(llc_sysctl_err_msg);
		goto out_proc;
	}
	rc = sock_register(&llc_ui_family_ops);
	if (rc) {
		printk(llc_sock_err_msg);
		goto out_sysctl;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1221 1222 1223 1224
	llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
	llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
out:
	return rc;
1225 1226 1227 1228
out_sysctl:
	llc_sysctl_exit();
out_proc:
	llc_proc_exit();
1229 1230
out_station:
	llc_station_exit();
Linus Torvalds's avatar
Linus Torvalds committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	proto_unregister(&llc_proto);
	goto out;
}

static void __exit llc2_exit(void)
{
	llc_station_exit();
	llc_remove_pack(LLC_DEST_SAP);
	llc_remove_pack(LLC_DEST_CONN);
	sock_unregister(PF_LLC);
	llc_proc_exit();
1242
	llc_sysctl_exit();
Linus Torvalds's avatar
Linus Torvalds committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	proto_unregister(&llc_proto);
}

module_init(llc2_init);
module_exit(llc2_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
MODULE_ALIAS_NETPROTO(PF_LLC);