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