2 * llc_conn.c - Driver routines for connection component.
4 * Copyright (c) 1997 by Procom Technology, Inc.
5 * 2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
12 * See the GNU General Public License for more details.
15 #include <linux/init.h>
16 #include <net/llc_sap.h>
17 #include <net/llc_conn.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>
26 #define dprintk(args...) printk(KERN_DEBUG args)
28 #define dprintk(args...)
31 static int llc_find_offset(int state
, int ev_type
);
32 static void llc_conn_send_pdus(struct sock
*sk
);
33 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
);
34 static int llc_exec_conn_trans_actions(struct sock
*sk
,
35 struct llc_conn_state_trans
*trans
,
37 static struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
40 /* Offset table on connection states transition diagram */
41 static int llc_offset_table
[NBR_CONN_STATES
][NBR_CONN_EV
];
43 int sysctl_llc2_ack_timeout
= LLC2_ACK_TIME
* HZ
;
44 int sysctl_llc2_p_timeout
= LLC2_P_TIME
* HZ
;
45 int sysctl_llc2_rej_timeout
= LLC2_REJ_TIME
* HZ
;
46 int sysctl_llc2_busy_timeout
= LLC2_BUSY_TIME
* HZ
;
49 * llc_conn_state_process - sends event to connection state machine
51 * @skb: occurred event
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.
58 int llc_conn_state_process(struct sock
*sk
, struct sk_buff
*skb
)
61 struct llc_sock
*llc
= llc_sk(skb
->sk
);
62 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
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
70 ev
->ind_prim
= ev
->cfm_prim
= 0;
72 * Send event to state machine
74 rc
= llc_conn_service(skb
->sk
, skb
);
75 if (unlikely(rc
!= 0)) {
76 printk(KERN_ERR
"%s: llc_conn_service failed\n", __func__
);
80 if (unlikely(!ev
->ind_prim
&& !ev
->cfm_prim
)) {
81 /* indicate or confirm not required */
87 if (unlikely(ev
->ind_prim
&& ev
->cfm_prim
)) /* Paranoia */
90 switch (ev
->ind_prim
) {
92 llc_save_primitive(sk
, skb
, LLC_DATA_PRIM
);
93 if (unlikely(sock_queue_rcv_skb(sk
, skb
))) {
97 printk(KERN_ERR
"%s: sock_queue_rcv_skb failed!\n",
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
108 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
109 sk
->sk_state_change(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
);
129 * RESET is not being notified to upper layers for now
131 printk(KERN_INFO
"%s: received a reset ind!\n", __func__
);
136 printk(KERN_INFO
"%s: received unknown %d prim!\n",
137 __func__
, ev
->ind_prim
);
144 switch (ev
->cfm_prim
) {
146 if (!llc_data_accept_state(llc
->state
))
147 sk
->sk_write_space(sk
);
149 rc
= llc
->failed_data_req
= 1;
152 if (sk
->sk_type
== SOCK_STREAM
&&
153 sk
->sk_state
== TCP_SYN_SENT
) {
155 sk
->sk_socket
->state
= SS_UNCONNECTED
;
156 sk
->sk_state
= TCP_CLOSE
;
158 sk
->sk_socket
->state
= SS_CONNECTED
;
159 sk
->sk_state
= TCP_ESTABLISHED
;
161 sk
->sk_state_change(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
);
176 * RESET is not being notified to upper layers for now
178 printk(KERN_INFO
"%s: received a reset conf!\n", __func__
);
182 printk(KERN_INFO
"%s: received unknown %d prim!\n",
183 __func__
, ev
->cfm_prim
);
186 goto out_skb_put
; /* No confirmation */
195 void llc_conn_send_pdu(struct sock
*sk
, struct sk_buff
*skb
)
197 /* queue PDU to send to MAC layer */
198 skb_queue_tail(&sk
->sk_write_queue
, skb
);
199 llc_conn_send_pdus(sk
);
203 * llc_conn_rtn_pdu - sends received data pdu to upper layer
204 * @sk: Active connection
205 * @skb: Received data frame
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.
211 void llc_conn_rtn_pdu(struct sock
*sk
, struct sk_buff
*skb
)
213 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
215 ev
->ind_prim
= LLC_DATA_PRIM
;
219 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
220 * @sk: active connection
222 * @first_p_bit: p_bit value of first pdu
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).
228 void llc_conn_resend_i_pdu_as_cmd(struct sock
*sk
, u8 nr
, u8 first_p_bit
)
231 struct llc_pdu_sn
*pdu
;
233 struct llc_sock
*llc
;
234 u8 howmany_resend
= 0;
236 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
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.
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
);
251 llc
->vS
= LLC_I_GET_NS(pdu
);
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
);
262 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
263 * @sk: active connection.
265 * @first_f_bit: f_bit value of first pdu.
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).
271 void llc_conn_resend_i_pdu_as_rsp(struct sock
*sk
, u8 nr
, u8 first_f_bit
)
275 struct llc_sock
*llc
= llc_sk(sk
);
276 u8 howmany_resend
= 0;
278 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
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
285 while ((skb
= skb_dequeue(&llc
->pdu_unack_q
)) != NULL
) {
286 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
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
);
292 llc
->vS
= LLC_I_GET_NS(pdu
);
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
);
303 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
304 * @sk: active connection
306 * how_many_unacked: size of pdu_unack_q after removing acked pdus
308 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
309 * the number of pdus that removed from queue.
311 int llc_conn_remove_acked_pdus(struct sock
*sk
, u8 nr
, u16
*how_many_unacked
)
315 struct llc_pdu_sn
*pdu
;
317 struct llc_sock
*llc
= llc_sk(sk
);
318 int q_len
= skb_queue_len(&llc
->pdu_unack_q
);
322 skb
= skb_peek(&llc
->pdu_unack_q
);
323 pdu
= llc_pdu_sn_hdr(skb
);
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
;
329 for (i
= 0; i
< pdu_pos
&& i
< q_len
; i
++) {
330 skb
= skb_dequeue(&llc
->pdu_unack_q
);
335 *how_many_unacked
= skb_queue_len(&llc
->pdu_unack_q
);
340 * llc_conn_send_pdus - Sends queued PDUs
341 * @sk: active connection
343 * Sends queued pdus to MAC layer for transmission.
345 static void llc_conn_send_pdus(struct sock
*sk
)
349 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
) {
350 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
352 if (LLC_PDU_TYPE_IS_I(pdu
) &&
353 !(skb
->dev
->flags
& IFF_LOOPBACK
)) {
354 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
356 skb_queue_tail(&llc_sk(sk
)->pdu_unack_q
, skb
);
366 * llc_conn_service - finds transition and changes state of connection
368 * @skb: happened event
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.
374 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
)
377 struct llc_sock
*llc
= llc_sk(sk
);
378 struct llc_conn_state_trans
*trans
;
380 if (llc
->state
> NBR_CONN_STATES
)
383 trans
= llc_qualify_conn_ev(sk
, skb
);
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
);
397 * llc_qualify_conn_ev - finds transition for event
399 * @skb: happened event
401 * This function finds transition that matches with happened event.
402 * Returns pointer to found transition on success, %NULL otherwise.
404 static struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
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];
414 /* search thru events for this state until
415 * list exhausted or until no more
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
427 for (next_qualifier
= (*next_trans
)->ev_qualifiers
;
428 next_qualifier
&& *next_qualifier
&&
429 !(*next_qualifier
)(sk
, skb
); next_qualifier
++)
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
443 * llc_exec_conn_trans_actions - executes related actions
445 * @trans: transition that it's actions must be performed
448 * Executes actions that is related to happened event. Returns 0 for
449 * success, 1 to indicate failure of at least one action.
451 static int llc_exec_conn_trans_actions(struct sock
*sk
,
452 struct llc_conn_state_trans
*trans
,
456 llc_conn_action_t
*next_action
;
458 for (next_action
= trans
->ev_actions
;
459 next_action
&& *next_action
; next_action
++) {
460 int rc2
= (*next_action
)(sk
, skb
);
471 static inline bool llc_estab_match(const struct llc_sap
*sap
,
472 const struct llc_addr
*daddr
,
473 const struct llc_addr
*laddr
,
474 const struct sock
*sk
)
476 struct llc_sock
*llc
= llc_sk(sk
);
478 return llc
->laddr
.lsap
== laddr
->lsap
&&
479 llc
->daddr
.lsap
== daddr
->lsap
&&
480 llc_mac_match(llc
->laddr
.mac
, laddr
->mac
) &&
481 llc_mac_match(llc
->daddr
.mac
, daddr
->mac
);
485 * __llc_lookup_established - Finds connection for the remote/local sap/mac
487 * @daddr: address of remote LLC (MAC + SAP)
488 * @laddr: address of local LLC (MAC + SAP)
490 * Search connection list of the SAP and finds connection using the remote
491 * mac, remote sap, local mac, and local sap. Returns pointer for
492 * connection found, %NULL otherwise.
493 * Caller has to make sure local_bh is disabled.
495 static struct sock
*__llc_lookup_established(struct llc_sap
*sap
,
496 struct llc_addr
*daddr
,
497 struct llc_addr
*laddr
)
500 struct hlist_nulls_node
*node
;
504 sk_nulls_for_each_rcu(rc
, node
, &sap
->sk_list
) {
505 if (llc_estab_match(sap
, daddr
, laddr
, rc
)) {
506 /* Extra checks required by SLAB_DESTROY_BY_RCU */
507 if (unlikely(!atomic_inc_not_zero(&rc
->sk_refcnt
)))
509 if (unlikely(llc_sk(rc
)->sap
!= sap
||
510 !llc_estab_match(sap
, daddr
, laddr
, rc
))) {
523 struct sock
*llc_lookup_established(struct llc_sap
*sap
,
524 struct llc_addr
*daddr
,
525 struct llc_addr
*laddr
)
530 sk
= __llc_lookup_established(sap
, daddr
, laddr
);
535 static inline bool llc_listener_match(const struct llc_sap
*sap
,
536 const struct llc_addr
*laddr
,
537 const struct sock
*sk
)
539 struct llc_sock
*llc
= llc_sk(sk
);
541 return sk
->sk_type
== SOCK_STREAM
&& sk
->sk_state
== TCP_LISTEN
&&
542 llc
->laddr
.lsap
== laddr
->lsap
&&
543 (llc_mac_match(llc
->laddr
.mac
, laddr
->mac
) ||
544 llc_mac_null(llc
->laddr
.mac
));
548 * llc_lookup_listener - Finds listener for local MAC + SAP
550 * @laddr: address of local LLC (MAC + SAP)
552 * Search connection list of the SAP and finds connection listening on
553 * local mac, and local sap. Returns pointer for parent socket found,
555 * Caller has to make sure local_bh is disabled.
557 static struct sock
*llc_lookup_listener(struct llc_sap
*sap
,
558 struct llc_addr
*laddr
)
561 struct hlist_nulls_node
*node
;
565 sk_nulls_for_each_rcu(rc
, node
, &sap
->sk_list
) {
566 if (llc_listener_match(sap
, laddr
, rc
)) {
567 /* Extra checks required by SLAB_DESTROY_BY_RCU */
568 if (unlikely(!atomic_inc_not_zero(&rc
->sk_refcnt
)))
570 if (unlikely(llc_sk(rc
)->sap
!= sap
||
571 !llc_listener_match(sap
, laddr
, rc
))) {
584 static struct sock
*__llc_lookup(struct llc_sap
*sap
,
585 struct llc_addr
*daddr
,
586 struct llc_addr
*laddr
)
588 struct sock
*sk
= __llc_lookup_established(sap
, daddr
, laddr
);
590 return sk
? : llc_lookup_listener(sap
, laddr
);
594 * llc_data_accept_state - designates if in this state data can be sent.
595 * @state: state of connection.
597 * Returns 0 if data can be sent, 1 otherwise.
599 u8
llc_data_accept_state(u8 state
)
601 return state
!= LLC_CONN_STATE_NORMAL
&& state
!= LLC_CONN_STATE_BUSY
&&
602 state
!= LLC_CONN_STATE_REJ
;
606 * llc_find_next_offset - finds offset for next category of transitions
607 * @state: state table.
608 * @offset: start offset.
610 * Finds offset of next category of transitions in transition table.
611 * Returns the start index of next category.
613 static u16 __init
llc_find_next_offset(struct llc_conn_state
*state
, u16 offset
)
616 struct llc_conn_state_trans
**next_trans
;
618 for (next_trans
= state
->transitions
+ offset
;
619 (*next_trans
)->ev
; next_trans
++)
625 * llc_build_offset_table - builds offset table of connection
627 * Fills offset table of connection state transition table
628 * (llc_offset_table).
630 void __init
llc_build_offset_table(void)
632 struct llc_conn_state
*curr_state
;
633 int state
, ev_type
, next_offset
;
635 for (state
= 0; state
< NBR_CONN_STATES
; state
++) {
636 curr_state
= &llc_conn_state_table
[state
];
638 for (ev_type
= 0; ev_type
< NBR_CONN_EV
; ev_type
++) {
639 llc_offset_table
[state
][ev_type
] = next_offset
;
640 next_offset
+= llc_find_next_offset(curr_state
,
647 * llc_find_offset - finds start offset of category of transitions
648 * @state: state of connection
649 * @ev_type: type of happened event
651 * Finds start offset of desired category of transitions. Returns the
652 * desired start offset.
654 static int llc_find_offset(int state
, int ev_type
)
657 /* at this stage, llc_offset_table[..][2] is not important. it is for
658 * init_pf_cycle and I don't know what is it.
661 case LLC_CONN_EV_TYPE_PRIM
:
662 rc
= llc_offset_table
[state
][0]; break;
663 case LLC_CONN_EV_TYPE_PDU
:
664 rc
= llc_offset_table
[state
][4]; break;
665 case LLC_CONN_EV_TYPE_SIMPLE
:
666 rc
= llc_offset_table
[state
][1]; break;
667 case LLC_CONN_EV_TYPE_P_TMR
:
668 case LLC_CONN_EV_TYPE_ACK_TMR
:
669 case LLC_CONN_EV_TYPE_REJ_TMR
:
670 case LLC_CONN_EV_TYPE_BUSY_TMR
:
671 rc
= llc_offset_table
[state
][3]; break;
677 * llc_sap_add_socket - adds a socket to a SAP
681 * This function adds a socket to sk_list of a SAP.
683 void llc_sap_add_socket(struct llc_sap
*sap
, struct sock
*sk
)
686 spin_lock_bh(&sap
->sk_lock
);
687 llc_sk(sk
)->sap
= sap
;
688 sk_nulls_add_node_rcu(sk
, &sap
->sk_list
);
689 spin_unlock_bh(&sap
->sk_lock
);
693 * llc_sap_remove_socket - removes a socket from SAP
697 * This function removes a connection from sk_list of a SAP if
698 * the connection was in this list.
700 void llc_sap_remove_socket(struct llc_sap
*sap
, struct sock
*sk
)
702 spin_lock_bh(&sap
->sk_lock
);
703 sk_nulls_del_node_init_rcu(sk
);
704 spin_unlock_bh(&sap
->sk_lock
);
709 * llc_conn_rcv - sends received pdus to the connection state machine
710 * @sk: current connection structure.
711 * @skb: received frame.
713 * Sends received pdus to the connection state machine.
715 static int llc_conn_rcv(struct sock
* sk
, struct sk_buff
*skb
)
717 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
719 ev
->type
= LLC_CONN_EV_TYPE_PDU
;
721 return llc_conn_state_process(sk
, skb
);
724 static struct sock
*llc_create_incoming_sock(struct sock
*sk
,
725 struct net_device
*dev
,
726 struct llc_addr
*saddr
,
727 struct llc_addr
*daddr
)
729 struct sock
*newsk
= llc_sk_alloc(sock_net(sk
), sk
->sk_family
, GFP_ATOMIC
,
731 struct llc_sock
*newllc
, *llc
= llc_sk(sk
);
735 newllc
= llc_sk(newsk
);
736 memcpy(&newllc
->laddr
, daddr
, sizeof(newllc
->laddr
));
737 memcpy(&newllc
->daddr
, saddr
, sizeof(newllc
->daddr
));
740 llc_sap_add_socket(llc
->sap
, newsk
);
741 llc_sap_hold(llc
->sap
);
746 void llc_conn_handler(struct llc_sap
*sap
, struct sk_buff
*skb
)
748 struct llc_addr saddr
, daddr
;
751 llc_pdu_decode_sa(skb
, saddr
.mac
);
752 llc_pdu_decode_ssap(skb
, &saddr
.lsap
);
753 llc_pdu_decode_da(skb
, daddr
.mac
);
754 llc_pdu_decode_dsap(skb
, &daddr
.lsap
);
756 sk
= __llc_lookup(sap
, &saddr
, &daddr
);
762 * This has to be done here and not at the upper layer ->accept
763 * method because of the way the PROCOM state machine works:
764 * it needs to set several state variables (see, for instance,
765 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
766 * the originator of the new connection, and this state has to be
767 * in the newly created struct sock private area. -acme
769 if (unlikely(sk
->sk_state
== TCP_LISTEN
)) {
770 struct sock
*newsk
= llc_create_incoming_sock(sk
, skb
->dev
,
774 skb_set_owner_r(skb
, newsk
);
777 * Can't be skb_set_owner_r, this will be done at the
778 * llc_conn_state_process function, later on, when we will use
779 * skb_queue_rcv_skb to send it to upper layers, this is
780 * another trick required to cope with how the PROCOM state
781 * machine works. -acme
785 if (!sock_owned_by_user(sk
))
786 llc_conn_rcv(sk
, skb
);
788 dprintk("%s: adding to backlog...\n", __func__
);
789 llc_set_backlog_type(skb
, LLC_PACKET
);
790 sk_add_backlog(sk
, skb
);
804 #undef LLC_REFCNT_DEBUG
805 #ifdef LLC_REFCNT_DEBUG
806 static atomic_t llc_sock_nr
;
810 * llc_backlog_rcv - Processes rx frames and expired timers.
811 * @sk: LLC sock (p8022 connection)
812 * @skb: queued rx frame or event
814 * This function processes frames that has received and timers that has
815 * expired during sending an I pdu (refer to data_req_handler). frames
816 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
817 * callback functions(llc_c_ac.c).
819 static int llc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
822 struct llc_sock
*llc
= llc_sk(sk
);
824 if (likely(llc_backlog_type(skb
) == LLC_PACKET
)) {
825 if (likely(llc
->state
> 1)) /* not closed */
826 rc
= llc_conn_rcv(sk
, skb
);
829 } else if (llc_backlog_type(skb
) == LLC_EVENT
) {
830 /* timer expiration event */
831 if (likely(llc
->state
> 1)) /* not closed */
832 rc
= llc_conn_state_process(sk
, skb
);
836 printk(KERN_ERR
"%s: invalid skb in backlog\n", __func__
);
847 * llc_sk_init - Initializes a socket with default llc values.
848 * @sk: socket to initialize.
850 * Initializes a socket with default llc values.
852 static void llc_sk_init(struct sock
* sk
)
854 struct llc_sock
*llc
= llc_sk(sk
);
856 llc
->state
= LLC_CONN_STATE_ADM
;
857 llc
->inc_cntr
= llc
->dec_cntr
= 2;
858 llc
->dec_step
= llc
->connect_step
= 1;
860 setup_timer(&llc
->ack_timer
.timer
, llc_conn_ack_tmr_cb
,
862 llc
->ack_timer
.expire
= sysctl_llc2_ack_timeout
;
864 setup_timer(&llc
->pf_cycle_timer
.timer
, llc_conn_pf_cycle_tmr_cb
,
866 llc
->pf_cycle_timer
.expire
= sysctl_llc2_p_timeout
;
868 setup_timer(&llc
->rej_sent_timer
.timer
, llc_conn_rej_tmr_cb
,
870 llc
->rej_sent_timer
.expire
= sysctl_llc2_rej_timeout
;
872 setup_timer(&llc
->busy_state_timer
.timer
, llc_conn_busy_tmr_cb
,
874 llc
->busy_state_timer
.expire
= sysctl_llc2_busy_timeout
;
876 llc
->n2
= 2; /* max retransmit */
877 llc
->k
= 2; /* tx win size, will adjust dynam */
878 llc
->rw
= 128; /* rx win size (opt and equal to
879 * tx_win of remote LLC) */
880 skb_queue_head_init(&llc
->pdu_unack_q
);
881 sk
->sk_backlog_rcv
= llc_backlog_rcv
;
885 * llc_sk_alloc - Allocates LLC sock
886 * @family: upper layer protocol family
887 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
889 * Allocates a LLC sock and initializes it. Returns the new LLC sock
890 * or %NULL if there's no memory available for one
892 struct sock
*llc_sk_alloc(struct net
*net
, int family
, gfp_t priority
, struct proto
*prot
)
894 struct sock
*sk
= sk_alloc(net
, family
, priority
, prot
);
899 sock_init_data(NULL
, sk
);
900 #ifdef LLC_REFCNT_DEBUG
901 atomic_inc(&llc_sock_nr
);
902 printk(KERN_DEBUG
"LLC socket %p created in %s, now we have %d alive\n", sk
,
903 __func__
, atomic_read(&llc_sock_nr
));
910 * llc_sk_free - Frees a LLC socket
911 * @sk - socket to free
915 void llc_sk_free(struct sock
*sk
)
917 struct llc_sock
*llc
= llc_sk(sk
);
919 llc
->state
= LLC_CONN_OUT_OF_SVC
;
920 /* Stop all (possibly) running timers */
921 llc_conn_ac_stop_all_timers(sk
, NULL
);
922 #ifdef DEBUG_LLC_CONN_ALLOC
923 printk(KERN_INFO
"%s: unackq=%d, txq=%d\n", __func__
,
924 skb_queue_len(&llc
->pdu_unack_q
),
925 skb_queue_len(&sk
->sk_write_queue
));
927 skb_queue_purge(&sk
->sk_receive_queue
);
928 skb_queue_purge(&sk
->sk_write_queue
);
929 skb_queue_purge(&llc
->pdu_unack_q
);
930 #ifdef LLC_REFCNT_DEBUG
931 if (atomic_read(&sk
->sk_refcnt
) != 1) {
932 printk(KERN_DEBUG
"Destruction of LLC sock %p delayed in %s, cnt=%d\n",
933 sk
, __func__
, atomic_read(&sk
->sk_refcnt
));
934 printk(KERN_DEBUG
"%d LLC sockets are still alive\n",
935 atomic_read(&llc_sock_nr
));
937 atomic_dec(&llc_sock_nr
);
938 printk(KERN_DEBUG
"LLC socket %p released in %s, %d are still alive\n", sk
,
939 __func__
, atomic_read(&llc_sock_nr
));
946 * llc_sk_reset - resets a connection
947 * @sk: LLC socket to reset
949 * Resets a connection to the out of service state. Stops its timers
950 * and frees any frames in the queues of the connection.
952 void llc_sk_reset(struct sock
*sk
)
954 struct llc_sock
*llc
= llc_sk(sk
);
956 llc_conn_ac_stop_all_timers(sk
, NULL
);
957 skb_queue_purge(&sk
->sk_write_queue
);
958 skb_queue_purge(&llc
->pdu_unack_q
);
959 llc
->remote_busy_flag
= 0;
961 llc
->retry_count
= 0;
962 llc_conn_set_p_flag(sk
, 0);
966 llc
->first_pdu_Ns
= 0;
967 llc
->ack_must_be_send
= 0;
972 llc
->failed_data_req
= 0 ;