Linux Audio

Check our new training course

Loading...
v6.13.7
   1/*
   2 * llc_conn.c - Driver routines for connection component.
   3 *
   4 * Copyright (c) 1997 by Procom Technology, Inc.
   5 *		 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
   6 *
   7 * This program can be redistributed or modified under the terms of the
   8 * GNU General Public License as published by the Free Software Foundation.
   9 * This program is distributed without any warranty or implied warranty
  10 * of merchantability or fitness for a particular purpose.
  11 *
  12 * See the GNU General Public License for more details.
  13 */
  14
  15#include <linux/init.h>
  16#include <linux/slab.h>
  17#include <net/llc.h>
  18#include <net/llc_c_ac.h>
  19#include <net/llc_c_ev.h>
  20#include <net/llc_c_st.h>
  21#include <net/llc_conn.h>
  22#include <net/llc_pdu.h>
  23#include <net/llc_sap.h>
 
  24#include <net/sock.h>
  25#include <net/tcp_states.h>
 
 
 
 
  26
  27#if 0
  28#define dprintk(args...) printk(KERN_DEBUG args)
  29#else
  30#define dprintk(args...)
  31#endif
  32
  33static int llc_find_offset(int state, int ev_type);
  34static void llc_conn_send_pdus(struct sock *sk);
  35static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
  36static int llc_exec_conn_trans_actions(struct sock *sk,
  37				       const struct llc_conn_state_trans *trans,
  38				       struct sk_buff *ev);
  39static const struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  40							      struct sk_buff *skb);
  41
  42/* Offset table on connection states transition diagram */
  43static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
  44
  45int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
  46int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
  47int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
  48int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
  49
  50/**
  51 *	llc_conn_state_process - sends event to connection state machine
  52 *	@sk: connection
  53 *	@skb: occurred event
  54 *
  55 *	Sends an event to connection state machine. After processing event
  56 *	(executing it's actions and changing state), upper layer will be
  57 *	indicated or confirmed, if needed. Returns 0 for success, 1 for
  58 *	failure. The socket lock has to be held before calling this function.
  59 *
  60 *	This function always consumes a reference to the skb.
  61 */
  62int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
  63{
  64	int rc;
  65	struct llc_sock *llc = llc_sk(skb->sk);
  66	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  67
  68	ev->ind_prim = ev->cfm_prim = 0;
  69	/*
  70	 * Send event to state machine
  71	 */
  72	rc = llc_conn_service(skb->sk, skb);
  73	if (unlikely(rc != 0)) {
  74		printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
  75		goto out_skb_put;
  76	}
  77
  78	switch (ev->ind_prim) {
  79	case LLC_DATA_PRIM:
  80		skb_get(skb);
  81		llc_save_primitive(sk, skb, LLC_DATA_PRIM);
  82		if (unlikely(sock_queue_rcv_skb(sk, skb))) {
  83			/*
  84			 * shouldn't happen
  85			 */
  86			printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
  87			       __func__);
  88			kfree_skb(skb);
  89		}
  90		break;
  91	case LLC_CONN_PRIM:
  92		/*
  93		 * Can't be sock_queue_rcv_skb, because we have to leave the
  94		 * skb->sk pointing to the newly created struct sock in
  95		 * llc_conn_handler. -acme
  96		 */
  97		skb_get(skb);
  98		skb_queue_tail(&sk->sk_receive_queue, skb);
  99		sk->sk_state_change(sk);
 100		break;
 101	case LLC_DISC_PRIM:
 102		sock_hold(sk);
 103		if (sk->sk_type == SOCK_STREAM &&
 104		    sk->sk_state == TCP_ESTABLISHED) {
 105			sk->sk_shutdown       = SHUTDOWN_MASK;
 106			sk->sk_socket->state  = SS_UNCONNECTED;
 107			sk->sk_state          = TCP_CLOSE;
 108			if (!sock_flag(sk, SOCK_DEAD)) {
 109				sock_set_flag(sk, SOCK_DEAD);
 110				sk->sk_state_change(sk);
 111			}
 112		}
 113		sock_put(sk);
 114		break;
 115	case LLC_RESET_PRIM:
 116		/*
 117		 * FIXME:
 118		 * RESET is not being notified to upper layers for now
 119		 */
 120		printk(KERN_INFO "%s: received a reset ind!\n", __func__);
 121		break;
 122	default:
 123		if (ev->ind_prim)
 124			printk(KERN_INFO "%s: received unknown %d prim!\n",
 125				__func__, ev->ind_prim);
 126		/* No indication */
 127		break;
 128	}
 129
 130	switch (ev->cfm_prim) {
 131	case LLC_DATA_PRIM:
 132		if (!llc_data_accept_state(llc->state))
 133			sk->sk_write_space(sk);
 134		else
 135			rc = llc->failed_data_req = 1;
 136		break;
 137	case LLC_CONN_PRIM:
 138		if (sk->sk_type == SOCK_STREAM &&
 139		    sk->sk_state == TCP_SYN_SENT) {
 140			if (ev->status) {
 141				sk->sk_socket->state = SS_UNCONNECTED;
 142				sk->sk_state         = TCP_CLOSE;
 143			} else {
 144				sk->sk_socket->state = SS_CONNECTED;
 145				sk->sk_state         = TCP_ESTABLISHED;
 146			}
 147			sk->sk_state_change(sk);
 148		}
 149		break;
 150	case LLC_DISC_PRIM:
 151		sock_hold(sk);
 152		if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
 153			sk->sk_socket->state = SS_UNCONNECTED;
 154			sk->sk_state         = TCP_CLOSE;
 155			sk->sk_state_change(sk);
 156		}
 157		sock_put(sk);
 158		break;
 159	case LLC_RESET_PRIM:
 160		/*
 161		 * FIXME:
 162		 * RESET is not being notified to upper layers for now
 163		 */
 164		printk(KERN_INFO "%s: received a reset conf!\n", __func__);
 165		break;
 166	default:
 167		if (ev->cfm_prim)
 168			printk(KERN_INFO "%s: received unknown %d prim!\n",
 169					__func__, ev->cfm_prim);
 170		/* No confirmation */
 171		break;
 172	}
 173out_skb_put:
 174	kfree_skb(skb);
 175	return rc;
 176}
 177
 178void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
 179{
 180	/* queue PDU to send to MAC layer */
 181	skb_queue_tail(&sk->sk_write_queue, skb);
 182	llc_conn_send_pdus(sk);
 183}
 184
 185/**
 186 *	llc_conn_rtn_pdu - sends received data pdu to upper layer
 187 *	@sk: Active connection
 188 *	@skb: Received data frame
 189 *
 190 *	Sends received data pdu to upper layer (by using indicate function).
 191 *	Prepares service parameters (prim and prim_data). calling indication
 192 *	function will be done in llc_conn_state_process.
 193 */
 194void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
 195{
 196	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 197
 198	ev->ind_prim = LLC_DATA_PRIM;
 199}
 200
 201/**
 202 *	llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
 203 *	@sk: active connection
 204 *	@nr: NR
 205 *	@first_p_bit: p_bit value of first pdu
 206 *
 207 *	Resend all unacknowledged I PDUs, starting with the NR; send first as
 208 *	command PDU with P bit equal first_p_bit; if more than one send
 209 *	subsequent as command PDUs with P bit equal zero (0).
 210 */
 211void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
 212{
 213	struct sk_buff *skb;
 214	struct llc_pdu_sn *pdu;
 215	u16 nbr_unack_pdus;
 216	struct llc_sock *llc;
 217	u8 howmany_resend = 0;
 218
 219	llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
 220	if (!nbr_unack_pdus)
 221		goto out;
 222	/*
 223	 * Process unack PDUs only if unack queue is not empty; remove
 224	 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
 225	 */
 226	llc = llc_sk(sk);
 227
 228	while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
 229		pdu = llc_pdu_sn_hdr(skb);
 230		llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
 231		llc_pdu_set_pf_bit(skb, first_p_bit);
 232		skb_queue_tail(&sk->sk_write_queue, skb);
 233		first_p_bit = 0;
 234		llc->vS = LLC_I_GET_NS(pdu);
 235		howmany_resend++;
 236	}
 237	if (howmany_resend > 0)
 238		llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
 239	/* any PDUs to re-send are queued up; start sending to MAC */
 240	llc_conn_send_pdus(sk);
 241out:;
 242}
 243
 244/**
 245 *	llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
 246 *	@sk: active connection.
 247 *	@nr: NR
 248 *	@first_f_bit: f_bit value of first pdu.
 249 *
 250 *	Resend all unacknowledged I PDUs, starting with the NR; send first as
 251 *	response PDU with F bit equal first_f_bit; if more than one send
 252 *	subsequent as response PDUs with F bit equal zero (0).
 253 */
 254void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
 255{
 256	struct sk_buff *skb;
 257	u16 nbr_unack_pdus;
 258	struct llc_sock *llc = llc_sk(sk);
 259	u8 howmany_resend = 0;
 260
 261	llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
 262	if (!nbr_unack_pdus)
 263		goto out;
 264	/*
 265	 * Process unack PDUs only if unack queue is not empty; remove
 266	 * appropriate PDUs, fix them up, and put them on mac_pdu_q
 267	 */
 268	while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
 269		struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
 270
 271		llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
 272		llc_pdu_set_pf_bit(skb, first_f_bit);
 273		skb_queue_tail(&sk->sk_write_queue, skb);
 274		first_f_bit = 0;
 275		llc->vS = LLC_I_GET_NS(pdu);
 276		howmany_resend++;
 277	}
 278	if (howmany_resend > 0)
 279		llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
 280	/* any PDUs to re-send are queued up; start sending to MAC */
 281	llc_conn_send_pdus(sk);
 282out:;
 283}
 284
 285/**
 286 *	llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
 287 *	@sk: active connection
 288 *	@nr: NR
 289 *	@how_many_unacked: size of pdu_unack_q after removing acked pdus
 290 *
 291 *	Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
 292 *	the number of pdus that removed from queue.
 293 */
 294int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
 295{
 296	int pdu_pos, i;
 297	struct sk_buff *skb;
 298	struct llc_pdu_sn *pdu;
 299	int nbr_acked = 0;
 300	struct llc_sock *llc = llc_sk(sk);
 301	int q_len = skb_queue_len(&llc->pdu_unack_q);
 302
 303	if (!q_len)
 304		goto out;
 305	skb = skb_peek(&llc->pdu_unack_q);
 306	pdu = llc_pdu_sn_hdr(skb);
 307
 308	/* finding position of last acked pdu in queue */
 309	pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
 310			(int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
 311
 312	for (i = 0; i < pdu_pos && i < q_len; i++) {
 313		skb = skb_dequeue(&llc->pdu_unack_q);
 314		kfree_skb(skb);
 315		nbr_acked++;
 316	}
 317out:
 318	*how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
 319	return nbr_acked;
 320}
 321
 322/**
 323 *	llc_conn_send_pdus - Sends queued PDUs
 324 *	@sk: active connection
 325 *
 326 *	Sends queued pdus to MAC layer for transmission.
 327 */
 328static void llc_conn_send_pdus(struct sock *sk)
 329{
 330	struct sk_buff *skb;
 331
 332	while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
 333		struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
 334
 335		if (LLC_PDU_TYPE_IS_I(pdu) &&
 336		    !(skb->dev->flags & IFF_LOOPBACK)) {
 337			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
 338
 339			skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
 340			if (!skb2)
 341				break;
 342			skb = skb2;
 343		}
 344		dev_queue_xmit(skb);
 345	}
 346}
 347
 348/**
 349 *	llc_conn_service - finds transition and changes state of connection
 350 *	@sk: connection
 351 *	@skb: happened event
 352 *
 353 *	This function finds transition that matches with happened event, then
 354 *	executes related actions and finally changes state of connection.
 355 *	Returns 0 for success, 1 for failure.
 356 */
 357static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
 358{
 359	const struct llc_conn_state_trans *trans;
 360	struct llc_sock *llc = llc_sk(sk);
 361	int rc = 1;
 
 
 362
 363	if (llc->state > NBR_CONN_STATES)
 364		goto out;
 365	rc = 0;
 366	trans = llc_qualify_conn_ev(sk, skb);
 367	if (trans) {
 368		rc = llc_exec_conn_trans_actions(sk, trans, skb);
 369		if (!rc && trans->next_state != NO_STATE_CHANGE) {
 370			llc->state = trans->next_state;
 371			if (!llc_data_accept_state(llc->state))
 372				sk->sk_state_change(sk);
 373		}
 374	}
 375out:
 376	return rc;
 377}
 378
 379/**
 380 *	llc_qualify_conn_ev - finds transition for event
 381 *	@sk: connection
 382 *	@skb: happened event
 383 *
 384 *	This function finds transition that matches with happened event.
 385 *	Returns pointer to found transition on success, %NULL otherwise.
 386 */
 387static const struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
 388							      struct sk_buff *skb)
 389{
 390	const struct llc_conn_state_trans **next_trans;
 391	const llc_conn_ev_qfyr_t *next_qualifier;
 392	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 393	struct llc_sock *llc = llc_sk(sk);
 394	struct llc_conn_state *curr_state =
 395					&llc_conn_state_table[llc->state - 1];
 396
 397	/* search thru events for this state until
 398	 * list exhausted or until no more
 399	 */
 400	for (next_trans = curr_state->transitions +
 401		llc_find_offset(llc->state - 1, ev->type);
 402	     (*next_trans)->ev; next_trans++) {
 403		if (!((*next_trans)->ev)(sk, skb)) {
 404			/* got POSSIBLE event match; the event may require
 405			 * qualification based on the values of a number of
 406			 * state flags; if all qualifications are met (i.e.,
 407			 * if all qualifying functions return success, or 0,
 408			 * then this is THE event we're looking for
 409			 */
 410			for (next_qualifier = (*next_trans)->ev_qualifiers;
 411			     next_qualifier && *next_qualifier &&
 412			     !(*next_qualifier)(sk, skb); next_qualifier++)
 413				/* nothing */;
 414			if (!next_qualifier || !*next_qualifier)
 415				/* all qualifiers executed successfully; this is
 416				 * our transition; return it so we can perform
 417				 * the associated actions & change the state
 418				 */
 419				return *next_trans;
 420		}
 421	}
 422	return NULL;
 423}
 424
 425/**
 426 *	llc_exec_conn_trans_actions - executes related actions
 427 *	@sk: connection
 428 *	@trans: transition that it's actions must be performed
 429 *	@skb: event
 430 *
 431 *	Executes actions that is related to happened event. Returns 0 for
 432 *	success, 1 to indicate failure of at least one action.
 433 */
 434static int llc_exec_conn_trans_actions(struct sock *sk,
 435				       const struct llc_conn_state_trans *trans,
 436				       struct sk_buff *skb)
 437{
 438	int rc = 0;
 439	const llc_conn_action_t *next_action;
 440
 441	for (next_action = trans->ev_actions;
 442	     next_action && *next_action; next_action++) {
 443		int rc2 = (*next_action)(sk, skb);
 444
 445		if (rc2 == 2) {
 446			rc = rc2;
 447			break;
 448		} else if (rc2)
 449			rc = 1;
 450	}
 451	return rc;
 452}
 453
 454static inline bool llc_estab_match(const struct llc_sap *sap,
 455				   const struct llc_addr *daddr,
 456				   const struct llc_addr *laddr,
 457				   const struct sock *sk,
 458				   const struct net *net)
 459{
 460	struct llc_sock *llc = llc_sk(sk);
 461
 462	return net_eq(sock_net(sk), net) &&
 463		llc->laddr.lsap == laddr->lsap &&
 464		llc->daddr.lsap == daddr->lsap &&
 465		ether_addr_equal(llc->laddr.mac, laddr->mac) &&
 466		ether_addr_equal(llc->daddr.mac, daddr->mac);
 467}
 468
 469/**
 470 *	__llc_lookup_established - Finds connection for the remote/local sap/mac
 471 *	@sap: SAP
 472 *	@daddr: address of remote LLC (MAC + SAP)
 473 *	@laddr: address of local LLC (MAC + SAP)
 474 *	@net: netns to look up a socket in
 475 *
 476 *	Search connection list of the SAP and finds connection using the remote
 477 *	mac, remote sap, local mac, and local sap. Returns pointer for
 478 *	connection found, %NULL otherwise.
 479 *	Caller has to make sure local_bh is disabled.
 480 */
 481static struct sock *__llc_lookup_established(struct llc_sap *sap,
 482					     struct llc_addr *daddr,
 483					     struct llc_addr *laddr,
 484					     const struct net *net)
 485{
 486	struct sock *rc;
 487	struct hlist_nulls_node *node;
 488	int slot = llc_sk_laddr_hashfn(sap, laddr);
 489	struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
 490
 491	rcu_read_lock();
 492again:
 493	sk_nulls_for_each_rcu(rc, node, laddr_hb) {
 494		if (llc_estab_match(sap, daddr, laddr, rc, net)) {
 495			/* Extra checks required by SLAB_TYPESAFE_BY_RCU */
 496			if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
 497				goto again;
 498			if (unlikely(llc_sk(rc)->sap != sap ||
 499				     !llc_estab_match(sap, daddr, laddr, rc, net))) {
 500				sock_put(rc);
 501				continue;
 502			}
 503			goto found;
 504		}
 505	}
 506	rc = NULL;
 507	/*
 508	 * if the nulls value we got at the end of this lookup is
 509	 * not the expected one, we must restart lookup.
 510	 * We probably met an item that was moved to another chain.
 511	 */
 512	if (unlikely(get_nulls_value(node) != slot))
 513		goto again;
 514found:
 515	rcu_read_unlock();
 516	return rc;
 517}
 518
 519struct sock *llc_lookup_established(struct llc_sap *sap,
 520				    struct llc_addr *daddr,
 521				    struct llc_addr *laddr,
 522				    const struct net *net)
 523{
 524	struct sock *sk;
 525
 526	local_bh_disable();
 527	sk = __llc_lookup_established(sap, daddr, laddr, net);
 528	local_bh_enable();
 529	return sk;
 530}
 531
 532static inline bool llc_listener_match(const struct llc_sap *sap,
 533				      const struct llc_addr *laddr,
 534				      const struct sock *sk,
 535				      const struct net *net)
 536{
 537	struct llc_sock *llc = llc_sk(sk);
 538
 539	return net_eq(sock_net(sk), net) &&
 540		sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
 541		llc->laddr.lsap == laddr->lsap &&
 542		ether_addr_equal(llc->laddr.mac, laddr->mac);
 543}
 544
 545static struct sock *__llc_lookup_listener(struct llc_sap *sap,
 546					  struct llc_addr *laddr,
 547					  const struct net *net)
 548{
 549	struct sock *rc;
 550	struct hlist_nulls_node *node;
 551	int slot = llc_sk_laddr_hashfn(sap, laddr);
 552	struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
 553
 554	rcu_read_lock();
 555again:
 556	sk_nulls_for_each_rcu(rc, node, laddr_hb) {
 557		if (llc_listener_match(sap, laddr, rc, net)) {
 558			/* Extra checks required by SLAB_TYPESAFE_BY_RCU */
 559			if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
 560				goto again;
 561			if (unlikely(llc_sk(rc)->sap != sap ||
 562				     !llc_listener_match(sap, laddr, rc, net))) {
 563				sock_put(rc);
 564				continue;
 565			}
 566			goto found;
 567		}
 568	}
 569	rc = NULL;
 570	/*
 571	 * if the nulls value we got at the end of this lookup is
 572	 * not the expected one, we must restart lookup.
 573	 * We probably met an item that was moved to another chain.
 574	 */
 575	if (unlikely(get_nulls_value(node) != slot))
 576		goto again;
 577found:
 578	rcu_read_unlock();
 579	return rc;
 580}
 581
 582/**
 583 *	llc_lookup_listener - Finds listener for local MAC + SAP
 584 *	@sap: SAP
 585 *	@laddr: address of local LLC (MAC + SAP)
 586 *	@net: netns to look up a socket in
 587 *
 588 *	Search connection list of the SAP and finds connection listening on
 589 *	local mac, and local sap. Returns pointer for parent socket found,
 590 *	%NULL otherwise.
 591 *	Caller has to make sure local_bh is disabled.
 592 */
 593static struct sock *llc_lookup_listener(struct llc_sap *sap,
 594					struct llc_addr *laddr,
 595					const struct net *net)
 596{
 597	struct sock *rc = __llc_lookup_listener(sap, laddr, net);
 598	static struct llc_addr null_addr;
 
 599
 600	if (!rc)
 601		rc = __llc_lookup_listener(sap, &null_addr, net);
 602
 603	return rc;
 604}
 605
 606static struct sock *__llc_lookup(struct llc_sap *sap,
 607				 struct llc_addr *daddr,
 608				 struct llc_addr *laddr,
 609				 const struct net *net)
 610{
 611	struct sock *sk = __llc_lookup_established(sap, daddr, laddr, net);
 612
 613	return sk ? : llc_lookup_listener(sap, laddr, net);
 614}
 615
 616/**
 617 *	llc_data_accept_state - designates if in this state data can be sent.
 618 *	@state: state of connection.
 619 *
 620 *	Returns 0 if data can be sent, 1 otherwise.
 621 */
 622u8 llc_data_accept_state(u8 state)
 623{
 624	return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
 625	       state != LLC_CONN_STATE_REJ;
 626}
 627
 628/**
 629 *	llc_find_next_offset - finds offset for next category of transitions
 630 *	@state: state table.
 631 *	@offset: start offset.
 632 *
 633 *	Finds offset of next category of transitions in transition table.
 634 *	Returns the start index of next category.
 635 */
 636static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
 637{
 638	const struct llc_conn_state_trans **next_trans;
 639	u16 cnt = 0;
 
 640
 641	for (next_trans = state->transitions + offset;
 642	     (*next_trans)->ev; next_trans++)
 643		++cnt;
 644	return cnt;
 645}
 646
 647/**
 648 *	llc_build_offset_table - builds offset table of connection
 649 *
 650 *	Fills offset table of connection state transition table
 651 *	(llc_offset_table).
 652 */
 653void __init llc_build_offset_table(void)
 654{
 655	struct llc_conn_state *curr_state;
 656	int state, ev_type, next_offset;
 657
 658	for (state = 0; state < NBR_CONN_STATES; state++) {
 659		curr_state = &llc_conn_state_table[state];
 660		next_offset = 0;
 661		for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
 662			llc_offset_table[state][ev_type] = next_offset;
 663			next_offset += llc_find_next_offset(curr_state,
 664							    next_offset) + 1;
 665		}
 666	}
 667}
 668
 669/**
 670 *	llc_find_offset - finds start offset of category of transitions
 671 *	@state: state of connection
 672 *	@ev_type: type of happened event
 673 *
 674 *	Finds start offset of desired category of transitions. Returns the
 675 *	desired start offset.
 676 */
 677static int llc_find_offset(int state, int ev_type)
 678{
 679	int rc = 0;
 680	/* at this stage, llc_offset_table[..][2] is not important. it is for
 681	 * init_pf_cycle and I don't know what is it.
 682	 */
 683	switch (ev_type) {
 684	case LLC_CONN_EV_TYPE_PRIM:
 685		rc = llc_offset_table[state][0]; break;
 686	case LLC_CONN_EV_TYPE_PDU:
 687		rc = llc_offset_table[state][4]; break;
 688	case LLC_CONN_EV_TYPE_SIMPLE:
 689		rc = llc_offset_table[state][1]; break;
 690	case LLC_CONN_EV_TYPE_P_TMR:
 691	case LLC_CONN_EV_TYPE_ACK_TMR:
 692	case LLC_CONN_EV_TYPE_REJ_TMR:
 693	case LLC_CONN_EV_TYPE_BUSY_TMR:
 694		rc = llc_offset_table[state][3]; break;
 695	}
 696	return rc;
 697}
 698
 699/**
 700 *	llc_sap_add_socket - adds a socket to a SAP
 701 *	@sap: SAP
 702 *	@sk: socket
 703 *
 704 *	This function adds a socket to the hash tables of a SAP.
 705 */
 706void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
 707{
 708	struct llc_sock *llc = llc_sk(sk);
 709	struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
 710	struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
 711
 712	llc_sap_hold(sap);
 713	llc_sk(sk)->sap = sap;
 714
 715	spin_lock_bh(&sap->sk_lock);
 716	sock_set_flag(sk, SOCK_RCU_FREE);
 717	sap->sk_count++;
 718	sk_nulls_add_node_rcu(sk, laddr_hb);
 719	hlist_add_head(&llc->dev_hash_node, dev_hb);
 720	spin_unlock_bh(&sap->sk_lock);
 721}
 722
 723/**
 724 *	llc_sap_remove_socket - removes a socket from SAP
 725 *	@sap: SAP
 726 *	@sk: socket
 727 *
 728 *	This function removes a connection from the hash tables of a SAP if
 729 *	the connection was in this list.
 730 */
 731void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
 732{
 733	struct llc_sock *llc = llc_sk(sk);
 734
 735	spin_lock_bh(&sap->sk_lock);
 736	sk_nulls_del_node_init_rcu(sk);
 737	hlist_del(&llc->dev_hash_node);
 738	sap->sk_count--;
 739	spin_unlock_bh(&sap->sk_lock);
 740	llc_sap_put(sap);
 741}
 742
 743/**
 744 *	llc_conn_rcv - sends received pdus to the connection state machine
 745 *	@sk: current connection structure.
 746 *	@skb: received frame.
 747 *
 748 *	Sends received pdus to the connection state machine.
 749 */
 750static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
 751{
 752	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 753
 754	ev->type   = LLC_CONN_EV_TYPE_PDU;
 755	ev->reason = 0;
 756	return llc_conn_state_process(sk, skb);
 757}
 758
 759static struct sock *llc_create_incoming_sock(struct sock *sk,
 760					     struct net_device *dev,
 761					     struct llc_addr *saddr,
 762					     struct llc_addr *daddr)
 763{
 764	struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
 765					  sk->sk_prot, 0);
 766	struct llc_sock *newllc, *llc = llc_sk(sk);
 767
 768	if (!newsk)
 769		goto out;
 770	newllc = llc_sk(newsk);
 771	memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
 772	memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
 773	newllc->dev = dev;
 774	dev_hold(dev);
 775	llc_sap_add_socket(llc->sap, newsk);
 776	llc_sap_hold(llc->sap);
 777out:
 778	return newsk;
 779}
 780
 781void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
 782{
 783	struct llc_addr saddr, daddr;
 784	struct sock *sk;
 785
 786	llc_pdu_decode_sa(skb, saddr.mac);
 787	llc_pdu_decode_ssap(skb, &saddr.lsap);
 788	llc_pdu_decode_da(skb, daddr.mac);
 789	llc_pdu_decode_dsap(skb, &daddr.lsap);
 790
 791	sk = __llc_lookup(sap, &saddr, &daddr, dev_net(skb->dev));
 792	if (!sk)
 793		goto drop;
 794
 795	bh_lock_sock(sk);
 796	/*
 797	 * This has to be done here and not at the upper layer ->accept
 798	 * method because of the way the PROCOM state machine works:
 799	 * it needs to set several state variables (see, for instance,
 800	 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
 801	 * the originator of the new connection, and this state has to be
 802	 * in the newly created struct sock private area. -acme
 803	 */
 804	if (unlikely(sk->sk_state == TCP_LISTEN)) {
 805		struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
 806							      &saddr, &daddr);
 807		if (!newsk)
 808			goto drop_unlock;
 809		skb_set_owner_r(skb, newsk);
 810	} else {
 811		/*
 812		 * Can't be skb_set_owner_r, this will be done at the
 813		 * llc_conn_state_process function, later on, when we will use
 814		 * skb_queue_rcv_skb to send it to upper layers, this is
 815		 * another trick required to cope with how the PROCOM state
 816		 * machine works. -acme
 817		 */
 818		skb_orphan(skb);
 819		sock_hold(sk);
 820		skb->sk = sk;
 821		skb->destructor = sock_efree;
 822	}
 823	if (!sock_owned_by_user(sk))
 824		llc_conn_rcv(sk, skb);
 825	else {
 826		dprintk("%s: adding to backlog...\n", __func__);
 827		llc_set_backlog_type(skb, LLC_PACKET);
 828		if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
 829			goto drop_unlock;
 830	}
 831out:
 832	bh_unlock_sock(sk);
 833	sock_put(sk);
 834	return;
 835drop:
 836	kfree_skb(skb);
 837	return;
 838drop_unlock:
 839	kfree_skb(skb);
 840	goto out;
 841}
 842
 843#undef LLC_REFCNT_DEBUG
 844#ifdef LLC_REFCNT_DEBUG
 845static atomic_t llc_sock_nr;
 846#endif
 847
 848/**
 849 *	llc_backlog_rcv - Processes rx frames and expired timers.
 850 *	@sk: LLC sock (p8022 connection)
 851 *	@skb: queued rx frame or event
 852 *
 853 *	This function processes frames that has received and timers that has
 854 *	expired during sending an I pdu (refer to data_req_handler).  frames
 855 *	queue by llc_rcv function (llc_mac.c) and timers queue by timer
 856 *	callback functions(llc_c_ac.c).
 857 */
 858static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
 859{
 860	int rc = 0;
 861	struct llc_sock *llc = llc_sk(sk);
 862
 863	if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
 864		if (likely(llc->state > 1)) /* not closed */
 865			rc = llc_conn_rcv(sk, skb);
 866		else
 867			goto out_kfree_skb;
 868	} else if (llc_backlog_type(skb) == LLC_EVENT) {
 869		/* timer expiration event */
 870		if (likely(llc->state > 1))  /* not closed */
 871			rc = llc_conn_state_process(sk, skb);
 872		else
 873			goto out_kfree_skb;
 874	} else {
 875		printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
 876		goto out_kfree_skb;
 877	}
 878out:
 879	return rc;
 880out_kfree_skb:
 881	kfree_skb(skb);
 882	goto out;
 883}
 884
 885/**
 886 *     llc_sk_init - Initializes a socket with default llc values.
 887 *     @sk: socket to initialize.
 888 *
 889 *     Initializes a socket with default llc values.
 890 */
 891static void llc_sk_init(struct sock *sk)
 892{
 893	struct llc_sock *llc = llc_sk(sk);
 894
 895	llc->state    = LLC_CONN_STATE_ADM;
 896	llc->inc_cntr = llc->dec_cntr = 2;
 897	llc->dec_step = llc->connect_step = 1;
 898
 899	timer_setup(&llc->ack_timer.timer, llc_conn_ack_tmr_cb, 0);
 900	llc->ack_timer.expire	      = sysctl_llc2_ack_timeout;
 901
 902	timer_setup(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb, 0);
 903	llc->pf_cycle_timer.expire	   = sysctl_llc2_p_timeout;
 904
 905	timer_setup(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb, 0);
 906	llc->rej_sent_timer.expire	   = sysctl_llc2_rej_timeout;
 907
 908	timer_setup(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb, 0);
 909	llc->busy_state_timer.expire	     = sysctl_llc2_busy_timeout;
 910
 911	llc->n2 = 2;   /* max retransmit */
 912	llc->k  = 2;   /* tx win size, will adjust dynam */
 913	llc->rw = 128; /* rx win size (opt and equal to
 914			* tx_win of remote LLC) */
 915	skb_queue_head_init(&llc->pdu_unack_q);
 916	sk->sk_backlog_rcv = llc_backlog_rcv;
 917}
 918
 919/**
 920 *	llc_sk_alloc - Allocates LLC sock
 921 *	@net: network namespace
 922 *	@family: upper layer protocol family
 923 *	@priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
 924 *	@prot: struct proto associated with this new sock instance
 925 *	@kern: is this to be a kernel socket?
 926 *
 927 *	Allocates a LLC sock and initializes it. Returns the new LLC sock
 928 *	or %NULL if there's no memory available for one
 929 */
 930struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
 931{
 932	struct sock *sk = sk_alloc(net, family, priority, prot, kern);
 933
 934	if (!sk)
 935		goto out;
 936	llc_sk_init(sk);
 937	sock_init_data(NULL, sk);
 938#ifdef LLC_REFCNT_DEBUG
 939	atomic_inc(&llc_sock_nr);
 940	printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
 941		__func__, atomic_read(&llc_sock_nr));
 942#endif
 943out:
 944	return sk;
 945}
 946
 947void llc_sk_stop_all_timers(struct sock *sk, bool sync)
 948{
 949	struct llc_sock *llc = llc_sk(sk);
 950
 951	if (sync) {
 952		del_timer_sync(&llc->pf_cycle_timer.timer);
 953		del_timer_sync(&llc->ack_timer.timer);
 954		del_timer_sync(&llc->rej_sent_timer.timer);
 955		del_timer_sync(&llc->busy_state_timer.timer);
 956	} else {
 957		del_timer(&llc->pf_cycle_timer.timer);
 958		del_timer(&llc->ack_timer.timer);
 959		del_timer(&llc->rej_sent_timer.timer);
 960		del_timer(&llc->busy_state_timer.timer);
 961	}
 962
 963	llc->ack_must_be_send = 0;
 964	llc->ack_pf = 0;
 965}
 966
 967/**
 968 *	llc_sk_free - Frees a LLC socket
 969 *	@sk: - socket to free
 970 *
 971 *	Frees a LLC socket
 972 */
 973void llc_sk_free(struct sock *sk)
 974{
 975	struct llc_sock *llc = llc_sk(sk);
 976
 977	llc->state = LLC_CONN_OUT_OF_SVC;
 978	/* Stop all (possibly) running timers */
 979	llc_sk_stop_all_timers(sk, true);
 980#ifdef DEBUG_LLC_CONN_ALLOC
 981	printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
 982		skb_queue_len(&llc->pdu_unack_q),
 983		skb_queue_len(&sk->sk_write_queue));
 984#endif
 985	skb_queue_purge(&sk->sk_receive_queue);
 986	skb_queue_purge(&sk->sk_write_queue);
 987	skb_queue_purge(&llc->pdu_unack_q);
 988#ifdef LLC_REFCNT_DEBUG
 989	if (refcount_read(&sk->sk_refcnt) != 1) {
 990		printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
 991			sk, __func__, refcount_read(&sk->sk_refcnt));
 992		printk(KERN_DEBUG "%d LLC sockets are still alive\n",
 993			atomic_read(&llc_sock_nr));
 994	} else {
 995		atomic_dec(&llc_sock_nr);
 996		printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
 997			__func__, atomic_read(&llc_sock_nr));
 998	}
 999#endif
1000	sock_put(sk);
1001}
1002
1003/**
1004 *	llc_sk_reset - resets a connection
1005 *	@sk: LLC socket to reset
1006 *
1007 *	Resets a connection to the out of service state. Stops its timers
1008 *	and frees any frames in the queues of the connection.
1009 */
1010void llc_sk_reset(struct sock *sk)
1011{
1012	struct llc_sock *llc = llc_sk(sk);
1013
1014	llc_conn_ac_stop_all_timers(sk, NULL);
1015	skb_queue_purge(&sk->sk_write_queue);
1016	skb_queue_purge(&llc->pdu_unack_q);
1017	llc->remote_busy_flag	= 0;
1018	llc->cause_flag		= 0;
1019	llc->retry_count	= 0;
1020	llc_conn_set_p_flag(sk, 0);
1021	llc->f_flag		= 0;
1022	llc->s_flag		= 0;
1023	llc->ack_pf		= 0;
1024	llc->first_pdu_Ns	= 0;
1025	llc->ack_must_be_send	= 0;
1026	llc->dec_step		= 1;
1027	llc->inc_cntr		= 2;
1028	llc->dec_cntr		= 2;
1029	llc->X			= 0;
1030	llc->failed_data_req	= 0 ;
1031	llc->last_nr		= 0;
1032}
v5.4
   1/*
   2 * llc_conn.c - Driver routines for connection component.
   3 *
   4 * Copyright (c) 1997 by Procom Technology, Inc.
   5 *		 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
   6 *
   7 * This program can be redistributed or modified under the terms of the
   8 * GNU General Public License as published by the Free Software Foundation.
   9 * This program is distributed without any warranty or implied warranty
  10 * of merchantability or fitness for a particular purpose.
  11 *
  12 * See the GNU General Public License for more details.
  13 */
  14
  15#include <linux/init.h>
  16#include <linux/slab.h>
 
 
 
 
 
 
  17#include <net/llc_sap.h>
  18#include <net/llc_conn.h>
  19#include <net/sock.h>
  20#include <net/tcp_states.h>
  21#include <net/llc_c_ev.h>
  22#include <net/llc_c_ac.h>
  23#include <net/llc_c_st.h>
  24#include <net/llc_pdu.h>
  25
  26#if 0
  27#define dprintk(args...) printk(KERN_DEBUG args)
  28#else
  29#define dprintk(args...)
  30#endif
  31
  32static int llc_find_offset(int state, int ev_type);
  33static void llc_conn_send_pdus(struct sock *sk);
  34static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
  35static int llc_exec_conn_trans_actions(struct sock *sk,
  36				       struct llc_conn_state_trans *trans,
  37				       struct sk_buff *ev);
  38static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
  39							struct sk_buff *skb);
  40
  41/* Offset table on connection states transition diagram */
  42static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
  43
  44int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
  45int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
  46int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
  47int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
  48
  49/**
  50 *	llc_conn_state_process - sends event to connection state machine
  51 *	@sk: connection
  52 *	@skb: occurred event
  53 *
  54 *	Sends an event to connection state machine. After processing event
  55 *	(executing it's actions and changing state), upper layer will be
  56 *	indicated or confirmed, if needed. Returns 0 for success, 1 for
  57 *	failure. The socket lock has to be held before calling this function.
  58 *
  59 *	This function always consumes a reference to the skb.
  60 */
  61int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
  62{
  63	int rc;
  64	struct llc_sock *llc = llc_sk(skb->sk);
  65	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
  66
  67	ev->ind_prim = ev->cfm_prim = 0;
  68	/*
  69	 * Send event to state machine
  70	 */
  71	rc = llc_conn_service(skb->sk, skb);
  72	if (unlikely(rc != 0)) {
  73		printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
  74		goto out_skb_put;
  75	}
  76
  77	switch (ev->ind_prim) {
  78	case LLC_DATA_PRIM:
  79		skb_get(skb);
  80		llc_save_primitive(sk, skb, LLC_DATA_PRIM);
  81		if (unlikely(sock_queue_rcv_skb(sk, skb))) {
  82			/*
  83			 * shouldn't happen
  84			 */
  85			printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
  86			       __func__);
  87			kfree_skb(skb);
  88		}
  89		break;
  90	case LLC_CONN_PRIM:
  91		/*
  92		 * Can't be sock_queue_rcv_skb, because we have to leave the
  93		 * skb->sk pointing to the newly created struct sock in
  94		 * llc_conn_handler. -acme
  95		 */
  96		skb_get(skb);
  97		skb_queue_tail(&sk->sk_receive_queue, skb);
  98		sk->sk_state_change(sk);
  99		break;
 100	case LLC_DISC_PRIM:
 101		sock_hold(sk);
 102		if (sk->sk_type == SOCK_STREAM &&
 103		    sk->sk_state == TCP_ESTABLISHED) {
 104			sk->sk_shutdown       = SHUTDOWN_MASK;
 105			sk->sk_socket->state  = SS_UNCONNECTED;
 106			sk->sk_state          = TCP_CLOSE;
 107			if (!sock_flag(sk, SOCK_DEAD)) {
 108				sock_set_flag(sk, SOCK_DEAD);
 109				sk->sk_state_change(sk);
 110			}
 111		}
 112		sock_put(sk);
 113		break;
 114	case LLC_RESET_PRIM:
 115		/*
 116		 * FIXME:
 117		 * RESET is not being notified to upper layers for now
 118		 */
 119		printk(KERN_INFO "%s: received a reset ind!\n", __func__);
 120		break;
 121	default:
 122		if (ev->ind_prim)
 123			printk(KERN_INFO "%s: received unknown %d prim!\n",
 124				__func__, ev->ind_prim);
 125		/* No indication */
 126		break;
 127	}
 128
 129	switch (ev->cfm_prim) {
 130	case LLC_DATA_PRIM:
 131		if (!llc_data_accept_state(llc->state))
 132			sk->sk_write_space(sk);
 133		else
 134			rc = llc->failed_data_req = 1;
 135		break;
 136	case LLC_CONN_PRIM:
 137		if (sk->sk_type == SOCK_STREAM &&
 138		    sk->sk_state == TCP_SYN_SENT) {
 139			if (ev->status) {
 140				sk->sk_socket->state = SS_UNCONNECTED;
 141				sk->sk_state         = TCP_CLOSE;
 142			} else {
 143				sk->sk_socket->state = SS_CONNECTED;
 144				sk->sk_state         = TCP_ESTABLISHED;
 145			}
 146			sk->sk_state_change(sk);
 147		}
 148		break;
 149	case LLC_DISC_PRIM:
 150		sock_hold(sk);
 151		if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
 152			sk->sk_socket->state = SS_UNCONNECTED;
 153			sk->sk_state         = TCP_CLOSE;
 154			sk->sk_state_change(sk);
 155		}
 156		sock_put(sk);
 157		break;
 158	case LLC_RESET_PRIM:
 159		/*
 160		 * FIXME:
 161		 * RESET is not being notified to upper layers for now
 162		 */
 163		printk(KERN_INFO "%s: received a reset conf!\n", __func__);
 164		break;
 165	default:
 166		if (ev->cfm_prim)
 167			printk(KERN_INFO "%s: received unknown %d prim!\n",
 168					__func__, ev->cfm_prim);
 169		/* No confirmation */
 170		break;
 171	}
 172out_skb_put:
 173	kfree_skb(skb);
 174	return rc;
 175}
 176
 177void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
 178{
 179	/* queue PDU to send to MAC layer */
 180	skb_queue_tail(&sk->sk_write_queue, skb);
 181	llc_conn_send_pdus(sk);
 182}
 183
 184/**
 185 *	llc_conn_rtn_pdu - sends received data pdu to upper layer
 186 *	@sk: Active connection
 187 *	@skb: Received data frame
 188 *
 189 *	Sends received data pdu to upper layer (by using indicate function).
 190 *	Prepares service parameters (prim and prim_data). calling indication
 191 *	function will be done in llc_conn_state_process.
 192 */
 193void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
 194{
 195	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 196
 197	ev->ind_prim = LLC_DATA_PRIM;
 198}
 199
 200/**
 201 *	llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
 202 *	@sk: active connection
 203 *	@nr: NR
 204 *	@first_p_bit: p_bit value of first pdu
 205 *
 206 *	Resend all unacknowledged I PDUs, starting with the NR; send first as
 207 *	command PDU with P bit equal first_p_bit; if more than one send
 208 *	subsequent as command PDUs with P bit equal zero (0).
 209 */
 210void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
 211{
 212	struct sk_buff *skb;
 213	struct llc_pdu_sn *pdu;
 214	u16 nbr_unack_pdus;
 215	struct llc_sock *llc;
 216	u8 howmany_resend = 0;
 217
 218	llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
 219	if (!nbr_unack_pdus)
 220		goto out;
 221	/*
 222	 * Process unack PDUs only if unack queue is not empty; remove
 223	 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
 224	 */
 225	llc = llc_sk(sk);
 226
 227	while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
 228		pdu = llc_pdu_sn_hdr(skb);
 229		llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
 230		llc_pdu_set_pf_bit(skb, first_p_bit);
 231		skb_queue_tail(&sk->sk_write_queue, skb);
 232		first_p_bit = 0;
 233		llc->vS = LLC_I_GET_NS(pdu);
 234		howmany_resend++;
 235	}
 236	if (howmany_resend > 0)
 237		llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
 238	/* any PDUs to re-send are queued up; start sending to MAC */
 239	llc_conn_send_pdus(sk);
 240out:;
 241}
 242
 243/**
 244 *	llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
 245 *	@sk: active connection.
 246 *	@nr: NR
 247 *	@first_f_bit: f_bit value of first pdu.
 248 *
 249 *	Resend all unacknowledged I PDUs, starting with the NR; send first as
 250 *	response PDU with F bit equal first_f_bit; if more than one send
 251 *	subsequent as response PDUs with F bit equal zero (0).
 252 */
 253void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
 254{
 255	struct sk_buff *skb;
 256	u16 nbr_unack_pdus;
 257	struct llc_sock *llc = llc_sk(sk);
 258	u8 howmany_resend = 0;
 259
 260	llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
 261	if (!nbr_unack_pdus)
 262		goto out;
 263	/*
 264	 * Process unack PDUs only if unack queue is not empty; remove
 265	 * appropriate PDUs, fix them up, and put them on mac_pdu_q
 266	 */
 267	while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
 268		struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
 269
 270		llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
 271		llc_pdu_set_pf_bit(skb, first_f_bit);
 272		skb_queue_tail(&sk->sk_write_queue, skb);
 273		first_f_bit = 0;
 274		llc->vS = LLC_I_GET_NS(pdu);
 275		howmany_resend++;
 276	}
 277	if (howmany_resend > 0)
 278		llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
 279	/* any PDUs to re-send are queued up; start sending to MAC */
 280	llc_conn_send_pdus(sk);
 281out:;
 282}
 283
 284/**
 285 *	llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
 286 *	@sk: active connection
 287 *	nr: NR
 288 *	how_many_unacked: size of pdu_unack_q after removing acked pdus
 289 *
 290 *	Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
 291 *	the number of pdus that removed from queue.
 292 */
 293int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
 294{
 295	int pdu_pos, i;
 296	struct sk_buff *skb;
 297	struct llc_pdu_sn *pdu;
 298	int nbr_acked = 0;
 299	struct llc_sock *llc = llc_sk(sk);
 300	int q_len = skb_queue_len(&llc->pdu_unack_q);
 301
 302	if (!q_len)
 303		goto out;
 304	skb = skb_peek(&llc->pdu_unack_q);
 305	pdu = llc_pdu_sn_hdr(skb);
 306
 307	/* finding position of last acked pdu in queue */
 308	pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
 309			(int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
 310
 311	for (i = 0; i < pdu_pos && i < q_len; i++) {
 312		skb = skb_dequeue(&llc->pdu_unack_q);
 313		kfree_skb(skb);
 314		nbr_acked++;
 315	}
 316out:
 317	*how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
 318	return nbr_acked;
 319}
 320
 321/**
 322 *	llc_conn_send_pdus - Sends queued PDUs
 323 *	@sk: active connection
 324 *
 325 *	Sends queued pdus to MAC layer for transmission.
 326 */
 327static void llc_conn_send_pdus(struct sock *sk)
 328{
 329	struct sk_buff *skb;
 330
 331	while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
 332		struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
 333
 334		if (LLC_PDU_TYPE_IS_I(pdu) &&
 335		    !(skb->dev->flags & IFF_LOOPBACK)) {
 336			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
 337
 338			skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
 339			if (!skb2)
 340				break;
 341			skb = skb2;
 342		}
 343		dev_queue_xmit(skb);
 344	}
 345}
 346
 347/**
 348 *	llc_conn_service - finds transition and changes state of connection
 349 *	@sk: connection
 350 *	@skb: happened event
 351 *
 352 *	This function finds transition that matches with happened event, then
 353 *	executes related actions and finally changes state of connection.
 354 *	Returns 0 for success, 1 for failure.
 355 */
 356static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
 357{
 
 
 358	int rc = 1;
 359	struct llc_sock *llc = llc_sk(sk);
 360	struct llc_conn_state_trans *trans;
 361
 362	if (llc->state > NBR_CONN_STATES)
 363		goto out;
 364	rc = 0;
 365	trans = llc_qualify_conn_ev(sk, skb);
 366	if (trans) {
 367		rc = llc_exec_conn_trans_actions(sk, trans, skb);
 368		if (!rc && trans->next_state != NO_STATE_CHANGE) {
 369			llc->state = trans->next_state;
 370			if (!llc_data_accept_state(llc->state))
 371				sk->sk_state_change(sk);
 372		}
 373	}
 374out:
 375	return rc;
 376}
 377
 378/**
 379 *	llc_qualify_conn_ev - finds transition for event
 380 *	@sk: connection
 381 *	@skb: happened event
 382 *
 383 *	This function finds transition that matches with happened event.
 384 *	Returns pointer to found transition on success, %NULL otherwise.
 385 */
 386static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
 387							struct sk_buff *skb)
 388{
 389	struct llc_conn_state_trans **next_trans;
 390	const llc_conn_ev_qfyr_t *next_qualifier;
 391	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 392	struct llc_sock *llc = llc_sk(sk);
 393	struct llc_conn_state *curr_state =
 394					&llc_conn_state_table[llc->state - 1];
 395
 396	/* search thru events for this state until
 397	 * list exhausted or until no more
 398	 */
 399	for (next_trans = curr_state->transitions +
 400		llc_find_offset(llc->state - 1, ev->type);
 401	     (*next_trans)->ev; next_trans++) {
 402		if (!((*next_trans)->ev)(sk, skb)) {
 403			/* got POSSIBLE event match; the event may require
 404			 * qualification based on the values of a number of
 405			 * state flags; if all qualifications are met (i.e.,
 406			 * if all qualifying functions return success, or 0,
 407			 * then this is THE event we're looking for
 408			 */
 409			for (next_qualifier = (*next_trans)->ev_qualifiers;
 410			     next_qualifier && *next_qualifier &&
 411			     !(*next_qualifier)(sk, skb); next_qualifier++)
 412				/* nothing */;
 413			if (!next_qualifier || !*next_qualifier)
 414				/* all qualifiers executed successfully; this is
 415				 * our transition; return it so we can perform
 416				 * the associated actions & change the state
 417				 */
 418				return *next_trans;
 419		}
 420	}
 421	return NULL;
 422}
 423
 424/**
 425 *	llc_exec_conn_trans_actions - executes related actions
 426 *	@sk: connection
 427 *	@trans: transition that it's actions must be performed
 428 *	@skb: event
 429 *
 430 *	Executes actions that is related to happened event. Returns 0 for
 431 *	success, 1 to indicate failure of at least one action.
 432 */
 433static int llc_exec_conn_trans_actions(struct sock *sk,
 434				       struct llc_conn_state_trans *trans,
 435				       struct sk_buff *skb)
 436{
 437	int rc = 0;
 438	const llc_conn_action_t *next_action;
 439
 440	for (next_action = trans->ev_actions;
 441	     next_action && *next_action; next_action++) {
 442		int rc2 = (*next_action)(sk, skb);
 443
 444		if (rc2 == 2) {
 445			rc = rc2;
 446			break;
 447		} else if (rc2)
 448			rc = 1;
 449	}
 450	return rc;
 451}
 452
 453static inline bool llc_estab_match(const struct llc_sap *sap,
 454				   const struct llc_addr *daddr,
 455				   const struct llc_addr *laddr,
 456				   const struct sock *sk)
 
 457{
 458	struct llc_sock *llc = llc_sk(sk);
 459
 460	return llc->laddr.lsap == laddr->lsap &&
 
 461		llc->daddr.lsap == daddr->lsap &&
 462		ether_addr_equal(llc->laddr.mac, laddr->mac) &&
 463		ether_addr_equal(llc->daddr.mac, daddr->mac);
 464}
 465
 466/**
 467 *	__llc_lookup_established - Finds connection for the remote/local sap/mac
 468 *	@sap: SAP
 469 *	@daddr: address of remote LLC (MAC + SAP)
 470 *	@laddr: address of local LLC (MAC + SAP)
 
 471 *
 472 *	Search connection list of the SAP and finds connection using the remote
 473 *	mac, remote sap, local mac, and local sap. Returns pointer for
 474 *	connection found, %NULL otherwise.
 475 *	Caller has to make sure local_bh is disabled.
 476 */
 477static struct sock *__llc_lookup_established(struct llc_sap *sap,
 478					     struct llc_addr *daddr,
 479					     struct llc_addr *laddr)
 
 480{
 481	struct sock *rc;
 482	struct hlist_nulls_node *node;
 483	int slot = llc_sk_laddr_hashfn(sap, laddr);
 484	struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
 485
 486	rcu_read_lock();
 487again:
 488	sk_nulls_for_each_rcu(rc, node, laddr_hb) {
 489		if (llc_estab_match(sap, daddr, laddr, rc)) {
 490			/* Extra checks required by SLAB_TYPESAFE_BY_RCU */
 491			if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
 492				goto again;
 493			if (unlikely(llc_sk(rc)->sap != sap ||
 494				     !llc_estab_match(sap, daddr, laddr, rc))) {
 495				sock_put(rc);
 496				continue;
 497			}
 498			goto found;
 499		}
 500	}
 501	rc = NULL;
 502	/*
 503	 * if the nulls value we got at the end of this lookup is
 504	 * not the expected one, we must restart lookup.
 505	 * We probably met an item that was moved to another chain.
 506	 */
 507	if (unlikely(get_nulls_value(node) != slot))
 508		goto again;
 509found:
 510	rcu_read_unlock();
 511	return rc;
 512}
 513
 514struct sock *llc_lookup_established(struct llc_sap *sap,
 515				    struct llc_addr *daddr,
 516				    struct llc_addr *laddr)
 
 517{
 518	struct sock *sk;
 519
 520	local_bh_disable();
 521	sk = __llc_lookup_established(sap, daddr, laddr);
 522	local_bh_enable();
 523	return sk;
 524}
 525
 526static inline bool llc_listener_match(const struct llc_sap *sap,
 527				      const struct llc_addr *laddr,
 528				      const struct sock *sk)
 
 529{
 530	struct llc_sock *llc = llc_sk(sk);
 531
 532	return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
 
 533		llc->laddr.lsap == laddr->lsap &&
 534		ether_addr_equal(llc->laddr.mac, laddr->mac);
 535}
 536
 537static struct sock *__llc_lookup_listener(struct llc_sap *sap,
 538					  struct llc_addr *laddr)
 
 539{
 540	struct sock *rc;
 541	struct hlist_nulls_node *node;
 542	int slot = llc_sk_laddr_hashfn(sap, laddr);
 543	struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
 544
 545	rcu_read_lock();
 546again:
 547	sk_nulls_for_each_rcu(rc, node, laddr_hb) {
 548		if (llc_listener_match(sap, laddr, rc)) {
 549			/* Extra checks required by SLAB_TYPESAFE_BY_RCU */
 550			if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
 551				goto again;
 552			if (unlikely(llc_sk(rc)->sap != sap ||
 553				     !llc_listener_match(sap, laddr, rc))) {
 554				sock_put(rc);
 555				continue;
 556			}
 557			goto found;
 558		}
 559	}
 560	rc = NULL;
 561	/*
 562	 * if the nulls value we got at the end of this lookup is
 563	 * not the expected one, we must restart lookup.
 564	 * We probably met an item that was moved to another chain.
 565	 */
 566	if (unlikely(get_nulls_value(node) != slot))
 567		goto again;
 568found:
 569	rcu_read_unlock();
 570	return rc;
 571}
 572
 573/**
 574 *	llc_lookup_listener - Finds listener for local MAC + SAP
 575 *	@sap: SAP
 576 *	@laddr: address of local LLC (MAC + SAP)
 
 577 *
 578 *	Search connection list of the SAP and finds connection listening on
 579 *	local mac, and local sap. Returns pointer for parent socket found,
 580 *	%NULL otherwise.
 581 *	Caller has to make sure local_bh is disabled.
 582 */
 583static struct sock *llc_lookup_listener(struct llc_sap *sap,
 584					struct llc_addr *laddr)
 
 585{
 
 586	static struct llc_addr null_addr;
 587	struct sock *rc = __llc_lookup_listener(sap, laddr);
 588
 589	if (!rc)
 590		rc = __llc_lookup_listener(sap, &null_addr);
 591
 592	return rc;
 593}
 594
 595static struct sock *__llc_lookup(struct llc_sap *sap,
 596				 struct llc_addr *daddr,
 597				 struct llc_addr *laddr)
 
 598{
 599	struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
 600
 601	return sk ? : llc_lookup_listener(sap, laddr);
 602}
 603
 604/**
 605 *	llc_data_accept_state - designates if in this state data can be sent.
 606 *	@state: state of connection.
 607 *
 608 *	Returns 0 if data can be sent, 1 otherwise.
 609 */
 610u8 llc_data_accept_state(u8 state)
 611{
 612	return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
 613	       state != LLC_CONN_STATE_REJ;
 614}
 615
 616/**
 617 *	llc_find_next_offset - finds offset for next category of transitions
 618 *	@state: state table.
 619 *	@offset: start offset.
 620 *
 621 *	Finds offset of next category of transitions in transition table.
 622 *	Returns the start index of next category.
 623 */
 624static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
 625{
 
 626	u16 cnt = 0;
 627	struct llc_conn_state_trans **next_trans;
 628
 629	for (next_trans = state->transitions + offset;
 630	     (*next_trans)->ev; next_trans++)
 631		++cnt;
 632	return cnt;
 633}
 634
 635/**
 636 *	llc_build_offset_table - builds offset table of connection
 637 *
 638 *	Fills offset table of connection state transition table
 639 *	(llc_offset_table).
 640 */
 641void __init llc_build_offset_table(void)
 642{
 643	struct llc_conn_state *curr_state;
 644	int state, ev_type, next_offset;
 645
 646	for (state = 0; state < NBR_CONN_STATES; state++) {
 647		curr_state = &llc_conn_state_table[state];
 648		next_offset = 0;
 649		for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
 650			llc_offset_table[state][ev_type] = next_offset;
 651			next_offset += llc_find_next_offset(curr_state,
 652							    next_offset) + 1;
 653		}
 654	}
 655}
 656
 657/**
 658 *	llc_find_offset - finds start offset of category of transitions
 659 *	@state: state of connection
 660 *	@ev_type: type of happened event
 661 *
 662 *	Finds start offset of desired category of transitions. Returns the
 663 *	desired start offset.
 664 */
 665static int llc_find_offset(int state, int ev_type)
 666{
 667	int rc = 0;
 668	/* at this stage, llc_offset_table[..][2] is not important. it is for
 669	 * init_pf_cycle and I don't know what is it.
 670	 */
 671	switch (ev_type) {
 672	case LLC_CONN_EV_TYPE_PRIM:
 673		rc = llc_offset_table[state][0]; break;
 674	case LLC_CONN_EV_TYPE_PDU:
 675		rc = llc_offset_table[state][4]; break;
 676	case LLC_CONN_EV_TYPE_SIMPLE:
 677		rc = llc_offset_table[state][1]; break;
 678	case LLC_CONN_EV_TYPE_P_TMR:
 679	case LLC_CONN_EV_TYPE_ACK_TMR:
 680	case LLC_CONN_EV_TYPE_REJ_TMR:
 681	case LLC_CONN_EV_TYPE_BUSY_TMR:
 682		rc = llc_offset_table[state][3]; break;
 683	}
 684	return rc;
 685}
 686
 687/**
 688 *	llc_sap_add_socket - adds a socket to a SAP
 689 *	@sap: SAP
 690 *	@sk: socket
 691 *
 692 *	This function adds a socket to the hash tables of a SAP.
 693 */
 694void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
 695{
 696	struct llc_sock *llc = llc_sk(sk);
 697	struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
 698	struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
 699
 700	llc_sap_hold(sap);
 701	llc_sk(sk)->sap = sap;
 702
 703	spin_lock_bh(&sap->sk_lock);
 704	sock_set_flag(sk, SOCK_RCU_FREE);
 705	sap->sk_count++;
 706	sk_nulls_add_node_rcu(sk, laddr_hb);
 707	hlist_add_head(&llc->dev_hash_node, dev_hb);
 708	spin_unlock_bh(&sap->sk_lock);
 709}
 710
 711/**
 712 *	llc_sap_remove_socket - removes a socket from SAP
 713 *	@sap: SAP
 714 *	@sk: socket
 715 *
 716 *	This function removes a connection from the hash tables of a SAP if
 717 *	the connection was in this list.
 718 */
 719void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
 720{
 721	struct llc_sock *llc = llc_sk(sk);
 722
 723	spin_lock_bh(&sap->sk_lock);
 724	sk_nulls_del_node_init_rcu(sk);
 725	hlist_del(&llc->dev_hash_node);
 726	sap->sk_count--;
 727	spin_unlock_bh(&sap->sk_lock);
 728	llc_sap_put(sap);
 729}
 730
 731/**
 732 *	llc_conn_rcv - sends received pdus to the connection state machine
 733 *	@sk: current connection structure.
 734 *	@skb: received frame.
 735 *
 736 *	Sends received pdus to the connection state machine.
 737 */
 738static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
 739{
 740	struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 741
 742	ev->type   = LLC_CONN_EV_TYPE_PDU;
 743	ev->reason = 0;
 744	return llc_conn_state_process(sk, skb);
 745}
 746
 747static struct sock *llc_create_incoming_sock(struct sock *sk,
 748					     struct net_device *dev,
 749					     struct llc_addr *saddr,
 750					     struct llc_addr *daddr)
 751{
 752	struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
 753					  sk->sk_prot, 0);
 754	struct llc_sock *newllc, *llc = llc_sk(sk);
 755
 756	if (!newsk)
 757		goto out;
 758	newllc = llc_sk(newsk);
 759	memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
 760	memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
 761	newllc->dev = dev;
 762	dev_hold(dev);
 763	llc_sap_add_socket(llc->sap, newsk);
 764	llc_sap_hold(llc->sap);
 765out:
 766	return newsk;
 767}
 768
 769void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
 770{
 771	struct llc_addr saddr, daddr;
 772	struct sock *sk;
 773
 774	llc_pdu_decode_sa(skb, saddr.mac);
 775	llc_pdu_decode_ssap(skb, &saddr.lsap);
 776	llc_pdu_decode_da(skb, daddr.mac);
 777	llc_pdu_decode_dsap(skb, &daddr.lsap);
 778
 779	sk = __llc_lookup(sap, &saddr, &daddr);
 780	if (!sk)
 781		goto drop;
 782
 783	bh_lock_sock(sk);
 784	/*
 785	 * This has to be done here and not at the upper layer ->accept
 786	 * method because of the way the PROCOM state machine works:
 787	 * it needs to set several state variables (see, for instance,
 788	 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
 789	 * the originator of the new connection, and this state has to be
 790	 * in the newly created struct sock private area. -acme
 791	 */
 792	if (unlikely(sk->sk_state == TCP_LISTEN)) {
 793		struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
 794							      &saddr, &daddr);
 795		if (!newsk)
 796			goto drop_unlock;
 797		skb_set_owner_r(skb, newsk);
 798	} else {
 799		/*
 800		 * Can't be skb_set_owner_r, this will be done at the
 801		 * llc_conn_state_process function, later on, when we will use
 802		 * skb_queue_rcv_skb to send it to upper layers, this is
 803		 * another trick required to cope with how the PROCOM state
 804		 * machine works. -acme
 805		 */
 806		skb_orphan(skb);
 807		sock_hold(sk);
 808		skb->sk = sk;
 809		skb->destructor = sock_efree;
 810	}
 811	if (!sock_owned_by_user(sk))
 812		llc_conn_rcv(sk, skb);
 813	else {
 814		dprintk("%s: adding to backlog...\n", __func__);
 815		llc_set_backlog_type(skb, LLC_PACKET);
 816		if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
 817			goto drop_unlock;
 818	}
 819out:
 820	bh_unlock_sock(sk);
 821	sock_put(sk);
 822	return;
 823drop:
 824	kfree_skb(skb);
 825	return;
 826drop_unlock:
 827	kfree_skb(skb);
 828	goto out;
 829}
 830
 831#undef LLC_REFCNT_DEBUG
 832#ifdef LLC_REFCNT_DEBUG
 833static atomic_t llc_sock_nr;
 834#endif
 835
 836/**
 837 *	llc_backlog_rcv - Processes rx frames and expired timers.
 838 *	@sk: LLC sock (p8022 connection)
 839 *	@skb: queued rx frame or event
 840 *
 841 *	This function processes frames that has received and timers that has
 842 *	expired during sending an I pdu (refer to data_req_handler).  frames
 843 *	queue by llc_rcv function (llc_mac.c) and timers queue by timer
 844 *	callback functions(llc_c_ac.c).
 845 */
 846static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
 847{
 848	int rc = 0;
 849	struct llc_sock *llc = llc_sk(sk);
 850
 851	if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
 852		if (likely(llc->state > 1)) /* not closed */
 853			rc = llc_conn_rcv(sk, skb);
 854		else
 855			goto out_kfree_skb;
 856	} else if (llc_backlog_type(skb) == LLC_EVENT) {
 857		/* timer expiration event */
 858		if (likely(llc->state > 1))  /* not closed */
 859			rc = llc_conn_state_process(sk, skb);
 860		else
 861			goto out_kfree_skb;
 862	} else {
 863		printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
 864		goto out_kfree_skb;
 865	}
 866out:
 867	return rc;
 868out_kfree_skb:
 869	kfree_skb(skb);
 870	goto out;
 871}
 872
 873/**
 874 *     llc_sk_init - Initializes a socket with default llc values.
 875 *     @sk: socket to initialize.
 876 *
 877 *     Initializes a socket with default llc values.
 878 */
 879static void llc_sk_init(struct sock *sk)
 880{
 881	struct llc_sock *llc = llc_sk(sk);
 882
 883	llc->state    = LLC_CONN_STATE_ADM;
 884	llc->inc_cntr = llc->dec_cntr = 2;
 885	llc->dec_step = llc->connect_step = 1;
 886
 887	timer_setup(&llc->ack_timer.timer, llc_conn_ack_tmr_cb, 0);
 888	llc->ack_timer.expire	      = sysctl_llc2_ack_timeout;
 889
 890	timer_setup(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb, 0);
 891	llc->pf_cycle_timer.expire	   = sysctl_llc2_p_timeout;
 892
 893	timer_setup(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb, 0);
 894	llc->rej_sent_timer.expire	   = sysctl_llc2_rej_timeout;
 895
 896	timer_setup(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb, 0);
 897	llc->busy_state_timer.expire	     = sysctl_llc2_busy_timeout;
 898
 899	llc->n2 = 2;   /* max retransmit */
 900	llc->k  = 2;   /* tx win size, will adjust dynam */
 901	llc->rw = 128; /* rx win size (opt and equal to
 902			* tx_win of remote LLC) */
 903	skb_queue_head_init(&llc->pdu_unack_q);
 904	sk->sk_backlog_rcv = llc_backlog_rcv;
 905}
 906
 907/**
 908 *	llc_sk_alloc - Allocates LLC sock
 
 909 *	@family: upper layer protocol family
 910 *	@priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
 
 
 911 *
 912 *	Allocates a LLC sock and initializes it. Returns the new LLC sock
 913 *	or %NULL if there's no memory available for one
 914 */
 915struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
 916{
 917	struct sock *sk = sk_alloc(net, family, priority, prot, kern);
 918
 919	if (!sk)
 920		goto out;
 921	llc_sk_init(sk);
 922	sock_init_data(NULL, sk);
 923#ifdef LLC_REFCNT_DEBUG
 924	atomic_inc(&llc_sock_nr);
 925	printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
 926		__func__, atomic_read(&llc_sock_nr));
 927#endif
 928out:
 929	return sk;
 930}
 931
 932void llc_sk_stop_all_timers(struct sock *sk, bool sync)
 933{
 934	struct llc_sock *llc = llc_sk(sk);
 935
 936	if (sync) {
 937		del_timer_sync(&llc->pf_cycle_timer.timer);
 938		del_timer_sync(&llc->ack_timer.timer);
 939		del_timer_sync(&llc->rej_sent_timer.timer);
 940		del_timer_sync(&llc->busy_state_timer.timer);
 941	} else {
 942		del_timer(&llc->pf_cycle_timer.timer);
 943		del_timer(&llc->ack_timer.timer);
 944		del_timer(&llc->rej_sent_timer.timer);
 945		del_timer(&llc->busy_state_timer.timer);
 946	}
 947
 948	llc->ack_must_be_send = 0;
 949	llc->ack_pf = 0;
 950}
 951
 952/**
 953 *	llc_sk_free - Frees a LLC socket
 954 *	@sk - socket to free
 955 *
 956 *	Frees a LLC socket
 957 */
 958void llc_sk_free(struct sock *sk)
 959{
 960	struct llc_sock *llc = llc_sk(sk);
 961
 962	llc->state = LLC_CONN_OUT_OF_SVC;
 963	/* Stop all (possibly) running timers */
 964	llc_sk_stop_all_timers(sk, true);
 965#ifdef DEBUG_LLC_CONN_ALLOC
 966	printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
 967		skb_queue_len(&llc->pdu_unack_q),
 968		skb_queue_len(&sk->sk_write_queue));
 969#endif
 970	skb_queue_purge(&sk->sk_receive_queue);
 971	skb_queue_purge(&sk->sk_write_queue);
 972	skb_queue_purge(&llc->pdu_unack_q);
 973#ifdef LLC_REFCNT_DEBUG
 974	if (refcount_read(&sk->sk_refcnt) != 1) {
 975		printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
 976			sk, __func__, refcount_read(&sk->sk_refcnt));
 977		printk(KERN_DEBUG "%d LLC sockets are still alive\n",
 978			atomic_read(&llc_sock_nr));
 979	} else {
 980		atomic_dec(&llc_sock_nr);
 981		printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
 982			__func__, atomic_read(&llc_sock_nr));
 983	}
 984#endif
 985	sock_put(sk);
 986}
 987
 988/**
 989 *	llc_sk_reset - resets a connection
 990 *	@sk: LLC socket to reset
 991 *
 992 *	Resets a connection to the out of service state. Stops its timers
 993 *	and frees any frames in the queues of the connection.
 994 */
 995void llc_sk_reset(struct sock *sk)
 996{
 997	struct llc_sock *llc = llc_sk(sk);
 998
 999	llc_conn_ac_stop_all_timers(sk, NULL);
1000	skb_queue_purge(&sk->sk_write_queue);
1001	skb_queue_purge(&llc->pdu_unack_q);
1002	llc->remote_busy_flag	= 0;
1003	llc->cause_flag		= 0;
1004	llc->retry_count	= 0;
1005	llc_conn_set_p_flag(sk, 0);
1006	llc->f_flag		= 0;
1007	llc->s_flag		= 0;
1008	llc->ack_pf		= 0;
1009	llc->first_pdu_Ns	= 0;
1010	llc->ack_must_be_send	= 0;
1011	llc->dec_step		= 1;
1012	llc->inc_cntr		= 2;
1013	llc->dec_cntr		= 2;
1014	llc->X			= 0;
1015	llc->failed_data_req	= 0 ;
1016	llc->last_nr		= 0;
1017}