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 <linux/slab.h>
17 #include <net/llc_sap.h>
18 #include <net/llc_conn.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>
27 #define dprintk(args...) printk(KERN_DEBUG args)
29 #define dprintk(args...)
32 static int llc_find_offset(int state
, int ev_type
);
33 static int llc_conn_send_pdus(struct sock
*sk
, struct sk_buff
*skb
);
34 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
);
35 static int llc_exec_conn_trans_actions(struct sock
*sk
,
36 struct llc_conn_state_trans
*trans
,
38 static struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
41 /* Offset table on connection states transition diagram */
42 static int llc_offset_table
[NBR_CONN_STATES
][NBR_CONN_EV
];
44 int sysctl_llc2_ack_timeout
= LLC2_ACK_TIME
* HZ
;
45 int sysctl_llc2_p_timeout
= LLC2_P_TIME
* HZ
;
46 int sysctl_llc2_rej_timeout
= LLC2_REJ_TIME
* HZ
;
47 int sysctl_llc2_busy_timeout
= LLC2_BUSY_TIME
* HZ
;
50 * llc_conn_state_process - sends event to connection state machine
52 * @skb: occurred event
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.
59 int llc_conn_state_process(struct sock
*sk
, struct sk_buff
*skb
)
62 struct llc_sock
*llc
= llc_sk(skb
->sk
);
63 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
66 * We have to hold the skb, because llc_conn_service will kfree it in
67 * the sending path and we need to look at the skb->cb, where we encode
71 ev
->ind_prim
= ev
->cfm_prim
= 0;
73 * Send event to state machine
75 rc
= llc_conn_service(skb
->sk
, skb
);
76 if (unlikely(rc
!= 0)) {
77 printk(KERN_ERR
"%s: llc_conn_service failed\n", __func__
);
81 if (unlikely(!ev
->ind_prim
&& !ev
->cfm_prim
)) {
82 /* indicate or confirm not required */
88 if (unlikely(ev
->ind_prim
&& ev
->cfm_prim
)) /* Paranoia */
91 switch (ev
->ind_prim
) {
93 llc_save_primitive(sk
, skb
, LLC_DATA_PRIM
);
94 if (unlikely(sock_queue_rcv_skb(sk
, skb
))) {
98 printk(KERN_ERR
"%s: sock_queue_rcv_skb failed!\n",
105 * Can't be sock_queue_rcv_skb, because we have to leave the
106 * skb->sk pointing to the newly created struct sock in
107 * llc_conn_handler. -acme
109 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
110 sk
->sk_state_change(sk
);
114 if (sk
->sk_type
== SOCK_STREAM
&&
115 sk
->sk_state
== TCP_ESTABLISHED
) {
116 sk
->sk_shutdown
= SHUTDOWN_MASK
;
117 sk
->sk_socket
->state
= SS_UNCONNECTED
;
118 sk
->sk_state
= TCP_CLOSE
;
119 if (!sock_flag(sk
, SOCK_DEAD
)) {
120 sock_set_flag(sk
, SOCK_DEAD
);
121 sk
->sk_state_change(sk
);
130 * RESET is not being notified to upper layers for now
132 printk(KERN_INFO
"%s: received a reset ind!\n", __func__
);
137 printk(KERN_INFO
"%s: received unknown %d prim!\n",
138 __func__
, ev
->ind_prim
);
145 switch (ev
->cfm_prim
) {
147 if (!llc_data_accept_state(llc
->state
))
148 sk
->sk_write_space(sk
);
150 rc
= llc
->failed_data_req
= 1;
153 if (sk
->sk_type
== SOCK_STREAM
&&
154 sk
->sk_state
== TCP_SYN_SENT
) {
156 sk
->sk_socket
->state
= SS_UNCONNECTED
;
157 sk
->sk_state
= TCP_CLOSE
;
159 sk
->sk_socket
->state
= SS_CONNECTED
;
160 sk
->sk_state
= TCP_ESTABLISHED
;
162 sk
->sk_state_change(sk
);
167 if (sk
->sk_type
== SOCK_STREAM
&& sk
->sk_state
== TCP_CLOSING
) {
168 sk
->sk_socket
->state
= SS_UNCONNECTED
;
169 sk
->sk_state
= TCP_CLOSE
;
170 sk
->sk_state_change(sk
);
177 * RESET is not being notified to upper layers for now
179 printk(KERN_INFO
"%s: received a reset conf!\n", __func__
);
183 printk(KERN_INFO
"%s: received unknown %d prim!\n",
184 __func__
, ev
->cfm_prim
);
187 goto out_skb_put
; /* No confirmation */
196 int llc_conn_send_pdu(struct sock
*sk
, struct sk_buff
*skb
)
198 /* queue PDU to send to MAC layer */
199 skb_queue_tail(&sk
->sk_write_queue
, skb
);
200 return llc_conn_send_pdus(sk
, skb
);
204 * llc_conn_rtn_pdu - sends received data pdu to upper layer
205 * @sk: Active connection
206 * @skb: Received data frame
208 * Sends received data pdu to upper layer (by using indicate function).
209 * Prepares service parameters (prim and prim_data). calling indication
210 * function will be done in llc_conn_state_process.
212 void llc_conn_rtn_pdu(struct sock
*sk
, struct sk_buff
*skb
)
214 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
216 ev
->ind_prim
= LLC_DATA_PRIM
;
220 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
221 * @sk: active connection
223 * @first_p_bit: p_bit value of first pdu
225 * Resend all unacknowledged I PDUs, starting with the NR; send first as
226 * command PDU with P bit equal first_p_bit; if more than one send
227 * subsequent as command PDUs with P bit equal zero (0).
229 void llc_conn_resend_i_pdu_as_cmd(struct sock
*sk
, u8 nr
, u8 first_p_bit
)
232 struct llc_pdu_sn
*pdu
;
234 struct llc_sock
*llc
;
235 u8 howmany_resend
= 0;
237 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
241 * Process unack PDUs only if unack queue is not empty; remove
242 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
246 while ((skb
= skb_dequeue(&llc
->pdu_unack_q
)) != NULL
) {
247 pdu
= llc_pdu_sn_hdr(skb
);
248 llc_pdu_set_cmd_rsp(skb
, LLC_PDU_CMD
);
249 llc_pdu_set_pf_bit(skb
, first_p_bit
);
250 skb_queue_tail(&sk
->sk_write_queue
, skb
);
252 llc
->vS
= LLC_I_GET_NS(pdu
);
255 if (howmany_resend
> 0)
256 llc
->vS
= (llc
->vS
+ 1) % LLC_2_SEQ_NBR_MODULO
;
257 /* any PDUs to re-send are queued up; start sending to MAC */
258 llc_conn_send_pdus(sk
, NULL
);
263 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
264 * @sk: active connection.
266 * @first_f_bit: f_bit value of first pdu.
268 * Resend all unacknowledged I PDUs, starting with the NR; send first as
269 * response PDU with F bit equal first_f_bit; if more than one send
270 * subsequent as response PDUs with F bit equal zero (0).
272 void llc_conn_resend_i_pdu_as_rsp(struct sock
*sk
, u8 nr
, u8 first_f_bit
)
276 struct llc_sock
*llc
= llc_sk(sk
);
277 u8 howmany_resend
= 0;
279 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
283 * Process unack PDUs only if unack queue is not empty; remove
284 * appropriate PDUs, fix them up, and put them on mac_pdu_q
286 while ((skb
= skb_dequeue(&llc
->pdu_unack_q
)) != NULL
) {
287 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
289 llc_pdu_set_cmd_rsp(skb
, LLC_PDU_RSP
);
290 llc_pdu_set_pf_bit(skb
, first_f_bit
);
291 skb_queue_tail(&sk
->sk_write_queue
, skb
);
293 llc
->vS
= LLC_I_GET_NS(pdu
);
296 if (howmany_resend
> 0)
297 llc
->vS
= (llc
->vS
+ 1) % LLC_2_SEQ_NBR_MODULO
;
298 /* any PDUs to re-send are queued up; start sending to MAC */
299 llc_conn_send_pdus(sk
, NULL
);
304 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
305 * @sk: active connection
307 * how_many_unacked: size of pdu_unack_q after removing acked pdus
309 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
310 * the number of pdus that removed from queue.
312 int llc_conn_remove_acked_pdus(struct sock
*sk
, u8 nr
, u16
*how_many_unacked
)
316 struct llc_pdu_sn
*pdu
;
318 struct llc_sock
*llc
= llc_sk(sk
);
319 int q_len
= skb_queue_len(&llc
->pdu_unack_q
);
323 skb
= skb_peek(&llc
->pdu_unack_q
);
324 pdu
= llc_pdu_sn_hdr(skb
);
326 /* finding position of last acked pdu in queue */
327 pdu_pos
= ((int)LLC_2_SEQ_NBR_MODULO
+ (int)nr
-
328 (int)LLC_I_GET_NS(pdu
)) % LLC_2_SEQ_NBR_MODULO
;
330 for (i
= 0; i
< pdu_pos
&& i
< q_len
; i
++) {
331 skb
= skb_dequeue(&llc
->pdu_unack_q
);
336 *how_many_unacked
= skb_queue_len(&llc
->pdu_unack_q
);
341 * llc_conn_send_pdus - Sends queued PDUs
342 * @sk: active connection
343 * @hold_skb: the skb held by caller, or NULL if does not care
345 * Sends queued pdus to MAC layer for transmission. When @hold_skb is
346 * NULL, always return 0. Otherwise, return 0 if @hold_skb is sent
347 * successfully, or 1 for failure.
349 static int llc_conn_send_pdus(struct sock
*sk
, struct sk_buff
*hold_skb
)
354 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
) {
355 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
357 if (LLC_PDU_TYPE_IS_I(pdu
) &&
358 !(skb
->dev
->flags
& IFF_LOOPBACK
)) {
359 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
361 skb_queue_tail(&llc_sk(sk
)->pdu_unack_q
, skb
);
364 dev_queue_xmit(skb2
);
366 bool is_target
= skb
== hold_skb
;
371 rc
= dev_queue_xmit(skb
);
381 * llc_conn_service - finds transition and changes state of connection
383 * @skb: happened event
385 * This function finds transition that matches with happened event, then
386 * executes related actions and finally changes state of connection.
387 * Returns 0 for success, 1 for failure.
389 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
)
392 struct llc_sock
*llc
= llc_sk(sk
);
393 struct llc_conn_state_trans
*trans
;
395 if (llc
->state
> NBR_CONN_STATES
)
398 trans
= llc_qualify_conn_ev(sk
, skb
);
400 rc
= llc_exec_conn_trans_actions(sk
, trans
, skb
);
401 if (!rc
&& trans
->next_state
!= NO_STATE_CHANGE
) {
402 llc
->state
= trans
->next_state
;
403 if (!llc_data_accept_state(llc
->state
))
404 sk
->sk_state_change(sk
);
412 * llc_qualify_conn_ev - finds transition for event
414 * @skb: happened event
416 * This function finds transition that matches with happened event.
417 * Returns pointer to found transition on success, %NULL otherwise.
419 static struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
422 struct llc_conn_state_trans
**next_trans
;
423 const llc_conn_ev_qfyr_t
*next_qualifier
;
424 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
425 struct llc_sock
*llc
= llc_sk(sk
);
426 struct llc_conn_state
*curr_state
=
427 &llc_conn_state_table
[llc
->state
- 1];
429 /* search thru events for this state until
430 * list exhausted or until no more
432 for (next_trans
= curr_state
->transitions
+
433 llc_find_offset(llc
->state
- 1, ev
->type
);
434 (*next_trans
)->ev
; next_trans
++) {
435 if (!((*next_trans
)->ev
)(sk
, skb
)) {
436 /* got POSSIBLE event match; the event may require
437 * qualification based on the values of a number of
438 * state flags; if all qualifications are met (i.e.,
439 * if all qualifying functions return success, or 0,
440 * then this is THE event we're looking for
442 for (next_qualifier
= (*next_trans
)->ev_qualifiers
;
443 next_qualifier
&& *next_qualifier
&&
444 !(*next_qualifier
)(sk
, skb
); next_qualifier
++)
446 if (!next_qualifier
|| !*next_qualifier
)
447 /* all qualifiers executed successfully; this is
448 * our transition; return it so we can perform
449 * the associated actions & change the state
458 * llc_exec_conn_trans_actions - executes related actions
460 * @trans: transition that it's actions must be performed
463 * Executes actions that is related to happened event. Returns 0 for
464 * success, 1 to indicate failure of at least one action.
466 static int llc_exec_conn_trans_actions(struct sock
*sk
,
467 struct llc_conn_state_trans
*trans
,
471 const llc_conn_action_t
*next_action
;
473 for (next_action
= trans
->ev_actions
;
474 next_action
&& *next_action
; next_action
++) {
475 int rc2
= (*next_action
)(sk
, skb
);
486 static inline bool llc_estab_match(const struct llc_sap
*sap
,
487 const struct llc_addr
*daddr
,
488 const struct llc_addr
*laddr
,
489 const struct sock
*sk
)
491 struct llc_sock
*llc
= llc_sk(sk
);
493 return llc
->laddr
.lsap
== laddr
->lsap
&&
494 llc
->daddr
.lsap
== daddr
->lsap
&&
495 ether_addr_equal(llc
->laddr
.mac
, laddr
->mac
) &&
496 ether_addr_equal(llc
->daddr
.mac
, daddr
->mac
);
500 * __llc_lookup_established - Finds connection for the remote/local sap/mac
502 * @daddr: address of remote LLC (MAC + SAP)
503 * @laddr: address of local LLC (MAC + SAP)
505 * Search connection list of the SAP and finds connection using the remote
506 * mac, remote sap, local mac, and local sap. Returns pointer for
507 * connection found, %NULL otherwise.
508 * Caller has to make sure local_bh is disabled.
510 static struct sock
*__llc_lookup_established(struct llc_sap
*sap
,
511 struct llc_addr
*daddr
,
512 struct llc_addr
*laddr
)
515 struct hlist_nulls_node
*node
;
516 int slot
= llc_sk_laddr_hashfn(sap
, laddr
);
517 struct hlist_nulls_head
*laddr_hb
= &sap
->sk_laddr_hash
[slot
];
521 sk_nulls_for_each_rcu(rc
, node
, laddr_hb
) {
522 if (llc_estab_match(sap
, daddr
, laddr
, rc
)) {
523 /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
524 if (unlikely(!refcount_inc_not_zero(&rc
->sk_refcnt
)))
526 if (unlikely(llc_sk(rc
)->sap
!= sap
||
527 !llc_estab_match(sap
, daddr
, laddr
, rc
))) {
536 * if the nulls value we got at the end of this lookup is
537 * not the expected one, we must restart lookup.
538 * We probably met an item that was moved to another chain.
540 if (unlikely(get_nulls_value(node
) != slot
))
547 struct sock
*llc_lookup_established(struct llc_sap
*sap
,
548 struct llc_addr
*daddr
,
549 struct llc_addr
*laddr
)
554 sk
= __llc_lookup_established(sap
, daddr
, laddr
);
559 static inline bool llc_listener_match(const struct llc_sap
*sap
,
560 const struct llc_addr
*laddr
,
561 const struct sock
*sk
)
563 struct llc_sock
*llc
= llc_sk(sk
);
565 return sk
->sk_type
== SOCK_STREAM
&& sk
->sk_state
== TCP_LISTEN
&&
566 llc
->laddr
.lsap
== laddr
->lsap
&&
567 ether_addr_equal(llc
->laddr
.mac
, laddr
->mac
);
570 static struct sock
*__llc_lookup_listener(struct llc_sap
*sap
,
571 struct llc_addr
*laddr
)
574 struct hlist_nulls_node
*node
;
575 int slot
= llc_sk_laddr_hashfn(sap
, laddr
);
576 struct hlist_nulls_head
*laddr_hb
= &sap
->sk_laddr_hash
[slot
];
580 sk_nulls_for_each_rcu(rc
, node
, laddr_hb
) {
581 if (llc_listener_match(sap
, laddr
, rc
)) {
582 /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
583 if (unlikely(!refcount_inc_not_zero(&rc
->sk_refcnt
)))
585 if (unlikely(llc_sk(rc
)->sap
!= sap
||
586 !llc_listener_match(sap
, laddr
, rc
))) {
595 * if the nulls value we got at the end of this lookup is
596 * not the expected one, we must restart lookup.
597 * We probably met an item that was moved to another chain.
599 if (unlikely(get_nulls_value(node
) != slot
))
607 * llc_lookup_listener - Finds listener for local MAC + SAP
609 * @laddr: address of local LLC (MAC + SAP)
611 * Search connection list of the SAP and finds connection listening on
612 * local mac, and local sap. Returns pointer for parent socket found,
614 * Caller has to make sure local_bh is disabled.
616 static struct sock
*llc_lookup_listener(struct llc_sap
*sap
,
617 struct llc_addr
*laddr
)
619 static struct llc_addr null_addr
;
620 struct sock
*rc
= __llc_lookup_listener(sap
, laddr
);
623 rc
= __llc_lookup_listener(sap
, &null_addr
);
628 static struct sock
*__llc_lookup(struct llc_sap
*sap
,
629 struct llc_addr
*daddr
,
630 struct llc_addr
*laddr
)
632 struct sock
*sk
= __llc_lookup_established(sap
, daddr
, laddr
);
634 return sk
? : llc_lookup_listener(sap
, laddr
);
638 * llc_data_accept_state - designates if in this state data can be sent.
639 * @state: state of connection.
641 * Returns 0 if data can be sent, 1 otherwise.
643 u8
llc_data_accept_state(u8 state
)
645 return state
!= LLC_CONN_STATE_NORMAL
&& state
!= LLC_CONN_STATE_BUSY
&&
646 state
!= LLC_CONN_STATE_REJ
;
650 * llc_find_next_offset - finds offset for next category of transitions
651 * @state: state table.
652 * @offset: start offset.
654 * Finds offset of next category of transitions in transition table.
655 * Returns the start index of next category.
657 static u16 __init
llc_find_next_offset(struct llc_conn_state
*state
, u16 offset
)
660 struct llc_conn_state_trans
**next_trans
;
662 for (next_trans
= state
->transitions
+ offset
;
663 (*next_trans
)->ev
; next_trans
++)
669 * llc_build_offset_table - builds offset table of connection
671 * Fills offset table of connection state transition table
672 * (llc_offset_table).
674 void __init
llc_build_offset_table(void)
676 struct llc_conn_state
*curr_state
;
677 int state
, ev_type
, next_offset
;
679 for (state
= 0; state
< NBR_CONN_STATES
; state
++) {
680 curr_state
= &llc_conn_state_table
[state
];
682 for (ev_type
= 0; ev_type
< NBR_CONN_EV
; ev_type
++) {
683 llc_offset_table
[state
][ev_type
] = next_offset
;
684 next_offset
+= llc_find_next_offset(curr_state
,
691 * llc_find_offset - finds start offset of category of transitions
692 * @state: state of connection
693 * @ev_type: type of happened event
695 * Finds start offset of desired category of transitions. Returns the
696 * desired start offset.
698 static int llc_find_offset(int state
, int ev_type
)
701 /* at this stage, llc_offset_table[..][2] is not important. it is for
702 * init_pf_cycle and I don't know what is it.
705 case LLC_CONN_EV_TYPE_PRIM
:
706 rc
= llc_offset_table
[state
][0]; break;
707 case LLC_CONN_EV_TYPE_PDU
:
708 rc
= llc_offset_table
[state
][4]; break;
709 case LLC_CONN_EV_TYPE_SIMPLE
:
710 rc
= llc_offset_table
[state
][1]; break;
711 case LLC_CONN_EV_TYPE_P_TMR
:
712 case LLC_CONN_EV_TYPE_ACK_TMR
:
713 case LLC_CONN_EV_TYPE_REJ_TMR
:
714 case LLC_CONN_EV_TYPE_BUSY_TMR
:
715 rc
= llc_offset_table
[state
][3]; break;
721 * llc_sap_add_socket - adds a socket to a SAP
725 * This function adds a socket to the hash tables of a SAP.
727 void llc_sap_add_socket(struct llc_sap
*sap
, struct sock
*sk
)
729 struct llc_sock
*llc
= llc_sk(sk
);
730 struct hlist_head
*dev_hb
= llc_sk_dev_hash(sap
, llc
->dev
->ifindex
);
731 struct hlist_nulls_head
*laddr_hb
= llc_sk_laddr_hash(sap
, &llc
->laddr
);
734 llc_sk(sk
)->sap
= sap
;
736 spin_lock_bh(&sap
->sk_lock
);
737 sock_set_flag(sk
, SOCK_RCU_FREE
);
739 sk_nulls_add_node_rcu(sk
, laddr_hb
);
740 hlist_add_head(&llc
->dev_hash_node
, dev_hb
);
741 spin_unlock_bh(&sap
->sk_lock
);
745 * llc_sap_remove_socket - removes a socket from SAP
749 * This function removes a connection from the hash tables of a SAP if
750 * the connection was in this list.
752 void llc_sap_remove_socket(struct llc_sap
*sap
, struct sock
*sk
)
754 struct llc_sock
*llc
= llc_sk(sk
);
756 spin_lock_bh(&sap
->sk_lock
);
757 sk_nulls_del_node_init_rcu(sk
);
758 hlist_del(&llc
->dev_hash_node
);
760 spin_unlock_bh(&sap
->sk_lock
);
765 * llc_conn_rcv - sends received pdus to the connection state machine
766 * @sk: current connection structure.
767 * @skb: received frame.
769 * Sends received pdus to the connection state machine.
771 static int llc_conn_rcv(struct sock
*sk
, struct sk_buff
*skb
)
773 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
775 ev
->type
= LLC_CONN_EV_TYPE_PDU
;
777 return llc_conn_state_process(sk
, skb
);
780 static struct sock
*llc_create_incoming_sock(struct sock
*sk
,
781 struct net_device
*dev
,
782 struct llc_addr
*saddr
,
783 struct llc_addr
*daddr
)
785 struct sock
*newsk
= llc_sk_alloc(sock_net(sk
), sk
->sk_family
, GFP_ATOMIC
,
787 struct llc_sock
*newllc
, *llc
= llc_sk(sk
);
791 newllc
= llc_sk(newsk
);
792 memcpy(&newllc
->laddr
, daddr
, sizeof(newllc
->laddr
));
793 memcpy(&newllc
->daddr
, saddr
, sizeof(newllc
->daddr
));
796 llc_sap_add_socket(llc
->sap
, newsk
);
797 llc_sap_hold(llc
->sap
);
802 void llc_conn_handler(struct llc_sap
*sap
, struct sk_buff
*skb
)
804 struct llc_addr saddr
, daddr
;
807 llc_pdu_decode_sa(skb
, saddr
.mac
);
808 llc_pdu_decode_ssap(skb
, &saddr
.lsap
);
809 llc_pdu_decode_da(skb
, daddr
.mac
);
810 llc_pdu_decode_dsap(skb
, &daddr
.lsap
);
812 sk
= __llc_lookup(sap
, &saddr
, &daddr
);
818 * This has to be done here and not at the upper layer ->accept
819 * method because of the way the PROCOM state machine works:
820 * it needs to set several state variables (see, for instance,
821 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
822 * the originator of the new connection, and this state has to be
823 * in the newly created struct sock private area. -acme
825 if (unlikely(sk
->sk_state
== TCP_LISTEN
)) {
826 struct sock
*newsk
= llc_create_incoming_sock(sk
, skb
->dev
,
830 skb_set_owner_r(skb
, newsk
);
833 * Can't be skb_set_owner_r, this will be done at the
834 * llc_conn_state_process function, later on, when we will use
835 * skb_queue_rcv_skb to send it to upper layers, this is
836 * another trick required to cope with how the PROCOM state
837 * machine works. -acme
842 skb
->destructor
= sock_efree
;
844 if (!sock_owned_by_user(sk
))
845 llc_conn_rcv(sk
, skb
);
847 dprintk("%s: adding to backlog...\n", __func__
);
848 llc_set_backlog_type(skb
, LLC_PACKET
);
849 if (sk_add_backlog(sk
, skb
, sk
->sk_rcvbuf
))
864 #undef LLC_REFCNT_DEBUG
865 #ifdef LLC_REFCNT_DEBUG
866 static atomic_t llc_sock_nr
;
870 * llc_backlog_rcv - Processes rx frames and expired timers.
871 * @sk: LLC sock (p8022 connection)
872 * @skb: queued rx frame or event
874 * This function processes frames that has received and timers that has
875 * expired during sending an I pdu (refer to data_req_handler). frames
876 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
877 * callback functions(llc_c_ac.c).
879 static int llc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
882 struct llc_sock
*llc
= llc_sk(sk
);
884 if (likely(llc_backlog_type(skb
) == LLC_PACKET
)) {
885 if (likely(llc
->state
> 1)) /* not closed */
886 rc
= llc_conn_rcv(sk
, skb
);
889 } else if (llc_backlog_type(skb
) == LLC_EVENT
) {
890 /* timer expiration event */
891 if (likely(llc
->state
> 1)) /* not closed */
892 rc
= llc_conn_state_process(sk
, skb
);
896 printk(KERN_ERR
"%s: invalid skb in backlog\n", __func__
);
907 * llc_sk_init - Initializes a socket with default llc values.
908 * @sk: socket to initialize.
910 * Initializes a socket with default llc values.
912 static void llc_sk_init(struct sock
*sk
)
914 struct llc_sock
*llc
= llc_sk(sk
);
916 llc
->state
= LLC_CONN_STATE_ADM
;
917 llc
->inc_cntr
= llc
->dec_cntr
= 2;
918 llc
->dec_step
= llc
->connect_step
= 1;
920 timer_setup(&llc
->ack_timer
.timer
, llc_conn_ack_tmr_cb
, 0);
921 llc
->ack_timer
.expire
= sysctl_llc2_ack_timeout
;
923 timer_setup(&llc
->pf_cycle_timer
.timer
, llc_conn_pf_cycle_tmr_cb
, 0);
924 llc
->pf_cycle_timer
.expire
= sysctl_llc2_p_timeout
;
926 timer_setup(&llc
->rej_sent_timer
.timer
, llc_conn_rej_tmr_cb
, 0);
927 llc
->rej_sent_timer
.expire
= sysctl_llc2_rej_timeout
;
929 timer_setup(&llc
->busy_state_timer
.timer
, llc_conn_busy_tmr_cb
, 0);
930 llc
->busy_state_timer
.expire
= sysctl_llc2_busy_timeout
;
932 llc
->n2
= 2; /* max retransmit */
933 llc
->k
= 2; /* tx win size, will adjust dynam */
934 llc
->rw
= 128; /* rx win size (opt and equal to
935 * tx_win of remote LLC) */
936 skb_queue_head_init(&llc
->pdu_unack_q
);
937 sk
->sk_backlog_rcv
= llc_backlog_rcv
;
941 * llc_sk_alloc - Allocates LLC sock
942 * @family: upper layer protocol family
943 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
945 * Allocates a LLC sock and initializes it. Returns the new LLC sock
946 * or %NULL if there's no memory available for one
948 struct sock
*llc_sk_alloc(struct net
*net
, int family
, gfp_t priority
, struct proto
*prot
, int kern
)
950 struct sock
*sk
= sk_alloc(net
, family
, priority
, prot
, kern
);
955 sock_init_data(NULL
, sk
);
956 #ifdef LLC_REFCNT_DEBUG
957 atomic_inc(&llc_sock_nr
);
958 printk(KERN_DEBUG
"LLC socket %p created in %s, now we have %d alive\n", sk
,
959 __func__
, atomic_read(&llc_sock_nr
));
965 void llc_sk_stop_all_timers(struct sock
*sk
, bool sync
)
967 struct llc_sock
*llc
= llc_sk(sk
);
970 del_timer_sync(&llc
->pf_cycle_timer
.timer
);
971 del_timer_sync(&llc
->ack_timer
.timer
);
972 del_timer_sync(&llc
->rej_sent_timer
.timer
);
973 del_timer_sync(&llc
->busy_state_timer
.timer
);
975 del_timer(&llc
->pf_cycle_timer
.timer
);
976 del_timer(&llc
->ack_timer
.timer
);
977 del_timer(&llc
->rej_sent_timer
.timer
);
978 del_timer(&llc
->busy_state_timer
.timer
);
981 llc
->ack_must_be_send
= 0;
986 * llc_sk_free - Frees a LLC socket
987 * @sk - socket to free
991 void llc_sk_free(struct sock
*sk
)
993 struct llc_sock
*llc
= llc_sk(sk
);
995 llc
->state
= LLC_CONN_OUT_OF_SVC
;
996 /* Stop all (possibly) running timers */
997 llc_sk_stop_all_timers(sk
, true);
998 #ifdef DEBUG_LLC_CONN_ALLOC
999 printk(KERN_INFO
"%s: unackq=%d, txq=%d\n", __func__
,
1000 skb_queue_len(&llc
->pdu_unack_q
),
1001 skb_queue_len(&sk
->sk_write_queue
));
1003 skb_queue_purge(&sk
->sk_receive_queue
);
1004 skb_queue_purge(&sk
->sk_write_queue
);
1005 skb_queue_purge(&llc
->pdu_unack_q
);
1006 #ifdef LLC_REFCNT_DEBUG
1007 if (refcount_read(&sk
->sk_refcnt
) != 1) {
1008 printk(KERN_DEBUG
"Destruction of LLC sock %p delayed in %s, cnt=%d\n",
1009 sk
, __func__
, refcount_read(&sk
->sk_refcnt
));
1010 printk(KERN_DEBUG
"%d LLC sockets are still alive\n",
1011 atomic_read(&llc_sock_nr
));
1013 atomic_dec(&llc_sock_nr
);
1014 printk(KERN_DEBUG
"LLC socket %p released in %s, %d are still alive\n", sk
,
1015 __func__
, atomic_read(&llc_sock_nr
));
1022 * llc_sk_reset - resets a connection
1023 * @sk: LLC socket to reset
1025 * Resets a connection to the out of service state. Stops its timers
1026 * and frees any frames in the queues of the connection.
1028 void llc_sk_reset(struct sock
*sk
)
1030 struct llc_sock
*llc
= llc_sk(sk
);
1032 llc_conn_ac_stop_all_timers(sk
, NULL
);
1033 skb_queue_purge(&sk
->sk_write_queue
);
1034 skb_queue_purge(&llc
->pdu_unack_q
);
1035 llc
->remote_busy_flag
= 0;
1036 llc
->cause_flag
= 0;
1037 llc
->retry_count
= 0;
1038 llc_conn_set_p_flag(sk
, 0);
1042 llc
->first_pdu_Ns
= 0;
1043 llc
->ack_must_be_send
= 0;
1048 llc
->failed_data_req
= 0 ;