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