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>
18 #include <net/llc_c_ac.h>
19 #include <net/llc_c_ev.h>
20 #include <net/llc_c_st.h>
21 #include <net/llc_conn.h>
22 #include <net/llc_pdu.h>
23 #include <net/llc_sap.h>
25 #include <net/tcp_states.h>
28 #define dprintk(args...) printk(KERN_DEBUG args)
30 #define dprintk(args...)
33 static int llc_find_offset(int state
, int ev_type
);
34 static void llc_conn_send_pdus(struct sock
*sk
);
35 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
);
36 static int llc_exec_conn_trans_actions(struct sock
*sk
,
37 const struct llc_conn_state_trans
*trans
,
39 static const struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
42 /* Offset table on connection states transition diagram */
43 static int llc_offset_table
[NBR_CONN_STATES
][NBR_CONN_EV
];
45 int sysctl_llc2_ack_timeout
= LLC2_ACK_TIME
* HZ
;
46 int sysctl_llc2_p_timeout
= LLC2_P_TIME
* HZ
;
47 int sysctl_llc2_rej_timeout
= LLC2_REJ_TIME
* HZ
;
48 int sysctl_llc2_busy_timeout
= LLC2_BUSY_TIME
* HZ
;
51 * llc_conn_state_process - sends event to connection state machine
53 * @skb: occurred event
55 * Sends an event to connection state machine. After processing event
56 * (executing it's actions and changing state), upper layer will be
57 * indicated or confirmed, if needed. Returns 0 for success, 1 for
58 * failure. The socket lock has to be held before calling this function.
60 * This function always consumes a reference to the skb.
62 int llc_conn_state_process(struct sock
*sk
, struct sk_buff
*skb
)
65 struct llc_sock
*llc
= llc_sk(skb
->sk
);
66 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
68 ev
->ind_prim
= ev
->cfm_prim
= 0;
70 * Send event to state machine
72 rc
= llc_conn_service(skb
->sk
, skb
);
73 if (unlikely(rc
!= 0)) {
74 printk(KERN_ERR
"%s: llc_conn_service failed\n", __func__
);
78 switch (ev
->ind_prim
) {
81 llc_save_primitive(sk
, skb
, LLC_DATA_PRIM
);
82 if (unlikely(sock_queue_rcv_skb(sk
, skb
))) {
86 printk(KERN_ERR
"%s: sock_queue_rcv_skb failed!\n",
93 * Can't be sock_queue_rcv_skb, because we have to leave the
94 * skb->sk pointing to the newly created struct sock in
95 * llc_conn_handler. -acme
98 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
99 sk
->sk_state_change(sk
);
103 if (sk
->sk_type
== SOCK_STREAM
&&
104 sk
->sk_state
== TCP_ESTABLISHED
) {
105 sk
->sk_shutdown
= SHUTDOWN_MASK
;
106 sk
->sk_socket
->state
= SS_UNCONNECTED
;
107 sk
->sk_state
= TCP_CLOSE
;
108 if (!sock_flag(sk
, SOCK_DEAD
)) {
109 sock_set_flag(sk
, SOCK_DEAD
);
110 sk
->sk_state_change(sk
);
118 * RESET is not being notified to upper layers for now
120 printk(KERN_INFO
"%s: received a reset ind!\n", __func__
);
124 printk(KERN_INFO
"%s: received unknown %d prim!\n",
125 __func__
, ev
->ind_prim
);
130 switch (ev
->cfm_prim
) {
132 if (!llc_data_accept_state(llc
->state
))
133 sk
->sk_write_space(sk
);
135 rc
= llc
->failed_data_req
= 1;
138 if (sk
->sk_type
== SOCK_STREAM
&&
139 sk
->sk_state
== TCP_SYN_SENT
) {
141 sk
->sk_socket
->state
= SS_UNCONNECTED
;
142 sk
->sk_state
= TCP_CLOSE
;
144 sk
->sk_socket
->state
= SS_CONNECTED
;
145 sk
->sk_state
= TCP_ESTABLISHED
;
147 sk
->sk_state_change(sk
);
152 if (sk
->sk_type
== SOCK_STREAM
&& sk
->sk_state
== TCP_CLOSING
) {
153 sk
->sk_socket
->state
= SS_UNCONNECTED
;
154 sk
->sk_state
= TCP_CLOSE
;
155 sk
->sk_state_change(sk
);
162 * RESET is not being notified to upper layers for now
164 printk(KERN_INFO
"%s: received a reset conf!\n", __func__
);
168 printk(KERN_INFO
"%s: received unknown %d prim!\n",
169 __func__
, ev
->cfm_prim
);
170 /* No confirmation */
178 void llc_conn_send_pdu(struct sock
*sk
, struct sk_buff
*skb
)
180 /* queue PDU to send to MAC layer */
181 skb_queue_tail(&sk
->sk_write_queue
, skb
);
182 llc_conn_send_pdus(sk
);
186 * llc_conn_rtn_pdu - sends received data pdu to upper layer
187 * @sk: Active connection
188 * @skb: Received data frame
190 * Sends received data pdu to upper layer (by using indicate function).
191 * Prepares service parameters (prim and prim_data). calling indication
192 * function will be done in llc_conn_state_process.
194 void llc_conn_rtn_pdu(struct sock
*sk
, struct sk_buff
*skb
)
196 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
198 ev
->ind_prim
= LLC_DATA_PRIM
;
202 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
203 * @sk: active connection
205 * @first_p_bit: p_bit value of first pdu
207 * Resend all unacknowledged I PDUs, starting with the NR; send first as
208 * command PDU with P bit equal first_p_bit; if more than one send
209 * subsequent as command PDUs with P bit equal zero (0).
211 void llc_conn_resend_i_pdu_as_cmd(struct sock
*sk
, u8 nr
, u8 first_p_bit
)
214 struct llc_pdu_sn
*pdu
;
216 struct llc_sock
*llc
;
217 u8 howmany_resend
= 0;
219 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
223 * Process unack PDUs only if unack queue is not empty; remove
224 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
228 while ((skb
= skb_dequeue(&llc
->pdu_unack_q
)) != NULL
) {
229 pdu
= llc_pdu_sn_hdr(skb
);
230 llc_pdu_set_cmd_rsp(skb
, LLC_PDU_CMD
);
231 llc_pdu_set_pf_bit(skb
, first_p_bit
);
232 skb_queue_tail(&sk
->sk_write_queue
, skb
);
234 llc
->vS
= LLC_I_GET_NS(pdu
);
237 if (howmany_resend
> 0)
238 llc
->vS
= (llc
->vS
+ 1) % LLC_2_SEQ_NBR_MODULO
;
239 /* any PDUs to re-send are queued up; start sending to MAC */
240 llc_conn_send_pdus(sk
);
245 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
246 * @sk: active connection.
248 * @first_f_bit: f_bit value of first pdu.
250 * Resend all unacknowledged I PDUs, starting with the NR; send first as
251 * response PDU with F bit equal first_f_bit; if more than one send
252 * subsequent as response PDUs with F bit equal zero (0).
254 void llc_conn_resend_i_pdu_as_rsp(struct sock
*sk
, u8 nr
, u8 first_f_bit
)
258 struct llc_sock
*llc
= llc_sk(sk
);
259 u8 howmany_resend
= 0;
261 llc_conn_remove_acked_pdus(sk
, nr
, &nbr_unack_pdus
);
265 * Process unack PDUs only if unack queue is not empty; remove
266 * appropriate PDUs, fix them up, and put them on mac_pdu_q
268 while ((skb
= skb_dequeue(&llc
->pdu_unack_q
)) != NULL
) {
269 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
271 llc_pdu_set_cmd_rsp(skb
, LLC_PDU_RSP
);
272 llc_pdu_set_pf_bit(skb
, first_f_bit
);
273 skb_queue_tail(&sk
->sk_write_queue
, skb
);
275 llc
->vS
= LLC_I_GET_NS(pdu
);
278 if (howmany_resend
> 0)
279 llc
->vS
= (llc
->vS
+ 1) % LLC_2_SEQ_NBR_MODULO
;
280 /* any PDUs to re-send are queued up; start sending to MAC */
281 llc_conn_send_pdus(sk
);
286 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
287 * @sk: active connection
289 * @how_many_unacked: size of pdu_unack_q after removing acked pdus
291 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
292 * the number of pdus that removed from queue.
294 int llc_conn_remove_acked_pdus(struct sock
*sk
, u8 nr
, u16
*how_many_unacked
)
298 struct llc_pdu_sn
*pdu
;
300 struct llc_sock
*llc
= llc_sk(sk
);
301 int q_len
= skb_queue_len(&llc
->pdu_unack_q
);
305 skb
= skb_peek(&llc
->pdu_unack_q
);
306 pdu
= llc_pdu_sn_hdr(skb
);
308 /* finding position of last acked pdu in queue */
309 pdu_pos
= ((int)LLC_2_SEQ_NBR_MODULO
+ (int)nr
-
310 (int)LLC_I_GET_NS(pdu
)) % LLC_2_SEQ_NBR_MODULO
;
312 for (i
= 0; i
< pdu_pos
&& i
< q_len
; i
++) {
313 skb
= skb_dequeue(&llc
->pdu_unack_q
);
318 *how_many_unacked
= skb_queue_len(&llc
->pdu_unack_q
);
323 * llc_conn_send_pdus - Sends queued PDUs
324 * @sk: active connection
326 * Sends queued pdus to MAC layer for transmission.
328 static void llc_conn_send_pdus(struct sock
*sk
)
332 while ((skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
) {
333 struct llc_pdu_sn
*pdu
= llc_pdu_sn_hdr(skb
);
335 if (LLC_PDU_TYPE_IS_I(pdu
) &&
336 !(skb
->dev
->flags
& IFF_LOOPBACK
)) {
337 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
339 skb_queue_tail(&llc_sk(sk
)->pdu_unack_q
, skb
);
349 * llc_conn_service - finds transition and changes state of connection
351 * @skb: happened event
353 * This function finds transition that matches with happened event, then
354 * executes related actions and finally changes state of connection.
355 * Returns 0 for success, 1 for failure.
357 static int llc_conn_service(struct sock
*sk
, struct sk_buff
*skb
)
359 const struct llc_conn_state_trans
*trans
;
360 struct llc_sock
*llc
= llc_sk(sk
);
363 if (llc
->state
> NBR_CONN_STATES
)
366 trans
= llc_qualify_conn_ev(sk
, skb
);
368 rc
= llc_exec_conn_trans_actions(sk
, trans
, skb
);
369 if (!rc
&& trans
->next_state
!= NO_STATE_CHANGE
) {
370 llc
->state
= trans
->next_state
;
371 if (!llc_data_accept_state(llc
->state
))
372 sk
->sk_state_change(sk
);
380 * llc_qualify_conn_ev - finds transition for event
382 * @skb: happened event
384 * This function finds transition that matches with happened event.
385 * Returns pointer to found transition on success, %NULL otherwise.
387 static const struct llc_conn_state_trans
*llc_qualify_conn_ev(struct sock
*sk
,
390 const struct llc_conn_state_trans
**next_trans
;
391 const llc_conn_ev_qfyr_t
*next_qualifier
;
392 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
393 struct llc_sock
*llc
= llc_sk(sk
);
394 struct llc_conn_state
*curr_state
=
395 &llc_conn_state_table
[llc
->state
- 1];
397 /* search thru events for this state until
398 * list exhausted or until no more
400 for (next_trans
= curr_state
->transitions
+
401 llc_find_offset(llc
->state
- 1, ev
->type
);
402 (*next_trans
)->ev
; next_trans
++) {
403 if (!((*next_trans
)->ev
)(sk
, skb
)) {
404 /* got POSSIBLE event match; the event may require
405 * qualification based on the values of a number of
406 * state flags; if all qualifications are met (i.e.,
407 * if all qualifying functions return success, or 0,
408 * then this is THE event we're looking for
410 for (next_qualifier
= (*next_trans
)->ev_qualifiers
;
411 next_qualifier
&& *next_qualifier
&&
412 !(*next_qualifier
)(sk
, skb
); next_qualifier
++)
414 if (!next_qualifier
|| !*next_qualifier
)
415 /* all qualifiers executed successfully; this is
416 * our transition; return it so we can perform
417 * the associated actions & change the state
426 * llc_exec_conn_trans_actions - executes related actions
428 * @trans: transition that it's actions must be performed
431 * Executes actions that is related to happened event. Returns 0 for
432 * success, 1 to indicate failure of at least one action.
434 static int llc_exec_conn_trans_actions(struct sock
*sk
,
435 const struct llc_conn_state_trans
*trans
,
439 const llc_conn_action_t
*next_action
;
441 for (next_action
= trans
->ev_actions
;
442 next_action
&& *next_action
; next_action
++) {
443 int rc2
= (*next_action
)(sk
, skb
);
454 static inline bool llc_estab_match(const struct llc_sap
*sap
,
455 const struct llc_addr
*daddr
,
456 const struct llc_addr
*laddr
,
457 const struct sock
*sk
,
458 const struct net
*net
)
460 struct llc_sock
*llc
= llc_sk(sk
);
462 return net_eq(sock_net(sk
), net
) &&
463 llc
->laddr
.lsap
== laddr
->lsap
&&
464 llc
->daddr
.lsap
== daddr
->lsap
&&
465 ether_addr_equal(llc
->laddr
.mac
, laddr
->mac
) &&
466 ether_addr_equal(llc
->daddr
.mac
, daddr
->mac
);
470 * __llc_lookup_established - Finds connection for the remote/local sap/mac
472 * @daddr: address of remote LLC (MAC + SAP)
473 * @laddr: address of local LLC (MAC + SAP)
474 * @net: netns to look up a socket in
476 * Search connection list of the SAP and finds connection using the remote
477 * mac, remote sap, local mac, and local sap. Returns pointer for
478 * connection found, %NULL otherwise.
479 * Caller has to make sure local_bh is disabled.
481 static struct sock
*__llc_lookup_established(struct llc_sap
*sap
,
482 struct llc_addr
*daddr
,
483 struct llc_addr
*laddr
,
484 const struct net
*net
)
487 struct hlist_nulls_node
*node
;
488 int slot
= llc_sk_laddr_hashfn(sap
, laddr
);
489 struct hlist_nulls_head
*laddr_hb
= &sap
->sk_laddr_hash
[slot
];
493 sk_nulls_for_each_rcu(rc
, node
, laddr_hb
) {
494 if (llc_estab_match(sap
, daddr
, laddr
, rc
, net
)) {
495 /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
496 if (unlikely(!refcount_inc_not_zero(&rc
->sk_refcnt
)))
498 if (unlikely(llc_sk(rc
)->sap
!= sap
||
499 !llc_estab_match(sap
, daddr
, laddr
, rc
, net
))) {
508 * if the nulls value we got at the end of this lookup is
509 * not the expected one, we must restart lookup.
510 * We probably met an item that was moved to another chain.
512 if (unlikely(get_nulls_value(node
) != slot
))
519 struct sock
*llc_lookup_established(struct llc_sap
*sap
,
520 struct llc_addr
*daddr
,
521 struct llc_addr
*laddr
,
522 const struct net
*net
)
527 sk
= __llc_lookup_established(sap
, daddr
, laddr
, net
);
532 static inline bool llc_listener_match(const struct llc_sap
*sap
,
533 const struct llc_addr
*laddr
,
534 const struct sock
*sk
,
535 const struct net
*net
)
537 struct llc_sock
*llc
= llc_sk(sk
);
539 return net_eq(sock_net(sk
), net
) &&
540 sk
->sk_type
== SOCK_STREAM
&& sk
->sk_state
== TCP_LISTEN
&&
541 llc
->laddr
.lsap
== laddr
->lsap
&&
542 ether_addr_equal(llc
->laddr
.mac
, laddr
->mac
);
545 static struct sock
*__llc_lookup_listener(struct llc_sap
*sap
,
546 struct llc_addr
*laddr
,
547 const struct net
*net
)
550 struct hlist_nulls_node
*node
;
551 int slot
= llc_sk_laddr_hashfn(sap
, laddr
);
552 struct hlist_nulls_head
*laddr_hb
= &sap
->sk_laddr_hash
[slot
];
556 sk_nulls_for_each_rcu(rc
, node
, laddr_hb
) {
557 if (llc_listener_match(sap
, laddr
, rc
, net
)) {
558 /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
559 if (unlikely(!refcount_inc_not_zero(&rc
->sk_refcnt
)))
561 if (unlikely(llc_sk(rc
)->sap
!= sap
||
562 !llc_listener_match(sap
, laddr
, rc
, net
))) {
571 * if the nulls value we got at the end of this lookup is
572 * not the expected one, we must restart lookup.
573 * We probably met an item that was moved to another chain.
575 if (unlikely(get_nulls_value(node
) != slot
))
583 * llc_lookup_listener - Finds listener for local MAC + SAP
585 * @laddr: address of local LLC (MAC + SAP)
586 * @net: netns to look up a socket in
588 * Search connection list of the SAP and finds connection listening on
589 * local mac, and local sap. Returns pointer for parent socket found,
591 * Caller has to make sure local_bh is disabled.
593 static struct sock
*llc_lookup_listener(struct llc_sap
*sap
,
594 struct llc_addr
*laddr
,
595 const struct net
*net
)
597 struct sock
*rc
= __llc_lookup_listener(sap
, laddr
, net
);
598 static struct llc_addr null_addr
;
601 rc
= __llc_lookup_listener(sap
, &null_addr
, net
);
606 static struct sock
*__llc_lookup(struct llc_sap
*sap
,
607 struct llc_addr
*daddr
,
608 struct llc_addr
*laddr
,
609 const struct net
*net
)
611 struct sock
*sk
= __llc_lookup_established(sap
, daddr
, laddr
, net
);
613 return sk
? : llc_lookup_listener(sap
, laddr
, net
);
617 * llc_data_accept_state - designates if in this state data can be sent.
618 * @state: state of connection.
620 * Returns 0 if data can be sent, 1 otherwise.
622 u8
llc_data_accept_state(u8 state
)
624 return state
!= LLC_CONN_STATE_NORMAL
&& state
!= LLC_CONN_STATE_BUSY
&&
625 state
!= LLC_CONN_STATE_REJ
;
629 * llc_find_next_offset - finds offset for next category of transitions
630 * @state: state table.
631 * @offset: start offset.
633 * Finds offset of next category of transitions in transition table.
634 * Returns the start index of next category.
636 static u16 __init
llc_find_next_offset(struct llc_conn_state
*state
, u16 offset
)
638 const struct llc_conn_state_trans
**next_trans
;
641 for (next_trans
= state
->transitions
+ offset
;
642 (*next_trans
)->ev
; next_trans
++)
648 * llc_build_offset_table - builds offset table of connection
650 * Fills offset table of connection state transition table
651 * (llc_offset_table).
653 void __init
llc_build_offset_table(void)
655 struct llc_conn_state
*curr_state
;
656 int state
, ev_type
, next_offset
;
658 for (state
= 0; state
< NBR_CONN_STATES
; state
++) {
659 curr_state
= &llc_conn_state_table
[state
];
661 for (ev_type
= 0; ev_type
< NBR_CONN_EV
; ev_type
++) {
662 llc_offset_table
[state
][ev_type
] = next_offset
;
663 next_offset
+= llc_find_next_offset(curr_state
,
670 * llc_find_offset - finds start offset of category of transitions
671 * @state: state of connection
672 * @ev_type: type of happened event
674 * Finds start offset of desired category of transitions. Returns the
675 * desired start offset.
677 static int llc_find_offset(int state
, int ev_type
)
680 /* at this stage, llc_offset_table[..][2] is not important. it is for
681 * init_pf_cycle and I don't know what is it.
684 case LLC_CONN_EV_TYPE_PRIM
:
685 rc
= llc_offset_table
[state
][0]; break;
686 case LLC_CONN_EV_TYPE_PDU
:
687 rc
= llc_offset_table
[state
][4]; break;
688 case LLC_CONN_EV_TYPE_SIMPLE
:
689 rc
= llc_offset_table
[state
][1]; break;
690 case LLC_CONN_EV_TYPE_P_TMR
:
691 case LLC_CONN_EV_TYPE_ACK_TMR
:
692 case LLC_CONN_EV_TYPE_REJ_TMR
:
693 case LLC_CONN_EV_TYPE_BUSY_TMR
:
694 rc
= llc_offset_table
[state
][3]; break;
700 * llc_sap_add_socket - adds a socket to a SAP
704 * This function adds a socket to the hash tables of a SAP.
706 void llc_sap_add_socket(struct llc_sap
*sap
, struct sock
*sk
)
708 struct llc_sock
*llc
= llc_sk(sk
);
709 struct hlist_head
*dev_hb
= llc_sk_dev_hash(sap
, llc
->dev
->ifindex
);
710 struct hlist_nulls_head
*laddr_hb
= llc_sk_laddr_hash(sap
, &llc
->laddr
);
713 llc_sk(sk
)->sap
= sap
;
715 spin_lock_bh(&sap
->sk_lock
);
716 sock_set_flag(sk
, SOCK_RCU_FREE
);
718 sk_nulls_add_node_rcu(sk
, laddr_hb
);
719 hlist_add_head(&llc
->dev_hash_node
, dev_hb
);
720 spin_unlock_bh(&sap
->sk_lock
);
724 * llc_sap_remove_socket - removes a socket from SAP
728 * This function removes a connection from the hash tables of a SAP if
729 * the connection was in this list.
731 void llc_sap_remove_socket(struct llc_sap
*sap
, struct sock
*sk
)
733 struct llc_sock
*llc
= llc_sk(sk
);
735 spin_lock_bh(&sap
->sk_lock
);
736 sk_nulls_del_node_init_rcu(sk
);
737 hlist_del(&llc
->dev_hash_node
);
739 spin_unlock_bh(&sap
->sk_lock
);
744 * llc_conn_rcv - sends received pdus to the connection state machine
745 * @sk: current connection structure.
746 * @skb: received frame.
748 * Sends received pdus to the connection state machine.
750 static int llc_conn_rcv(struct sock
*sk
, struct sk_buff
*skb
)
752 struct llc_conn_state_ev
*ev
= llc_conn_ev(skb
);
754 ev
->type
= LLC_CONN_EV_TYPE_PDU
;
756 return llc_conn_state_process(sk
, skb
);
759 static struct sock
*llc_create_incoming_sock(struct sock
*sk
,
760 struct net_device
*dev
,
761 struct llc_addr
*saddr
,
762 struct llc_addr
*daddr
)
764 struct sock
*newsk
= llc_sk_alloc(sock_net(sk
), sk
->sk_family
, GFP_ATOMIC
,
766 struct llc_sock
*newllc
, *llc
= llc_sk(sk
);
770 newllc
= llc_sk(newsk
);
771 memcpy(&newllc
->laddr
, daddr
, sizeof(newllc
->laddr
));
772 memcpy(&newllc
->daddr
, saddr
, sizeof(newllc
->daddr
));
775 llc_sap_add_socket(llc
->sap
, newsk
);
776 llc_sap_hold(llc
->sap
);
781 void llc_conn_handler(struct llc_sap
*sap
, struct sk_buff
*skb
)
783 struct llc_addr saddr
, daddr
;
786 llc_pdu_decode_sa(skb
, saddr
.mac
);
787 llc_pdu_decode_ssap(skb
, &saddr
.lsap
);
788 llc_pdu_decode_da(skb
, daddr
.mac
);
789 llc_pdu_decode_dsap(skb
, &daddr
.lsap
);
791 sk
= __llc_lookup(sap
, &saddr
, &daddr
, dev_net(skb
->dev
));
797 * This has to be done here and not at the upper layer ->accept
798 * method because of the way the PROCOM state machine works:
799 * it needs to set several state variables (see, for instance,
800 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
801 * the originator of the new connection, and this state has to be
802 * in the newly created struct sock private area. -acme
804 if (unlikely(sk
->sk_state
== TCP_LISTEN
)) {
805 struct sock
*newsk
= llc_create_incoming_sock(sk
, skb
->dev
,
809 skb_set_owner_r(skb
, newsk
);
812 * Can't be skb_set_owner_r, this will be done at the
813 * llc_conn_state_process function, later on, when we will use
814 * skb_queue_rcv_skb to send it to upper layers, this is
815 * another trick required to cope with how the PROCOM state
816 * machine works. -acme
821 skb
->destructor
= sock_efree
;
823 if (!sock_owned_by_user(sk
))
824 llc_conn_rcv(sk
, skb
);
826 dprintk("%s: adding to backlog...\n", __func__
);
827 llc_set_backlog_type(skb
, LLC_PACKET
);
828 if (sk_add_backlog(sk
, skb
, READ_ONCE(sk
->sk_rcvbuf
)))
843 #undef LLC_REFCNT_DEBUG
844 #ifdef LLC_REFCNT_DEBUG
845 static atomic_t llc_sock_nr
;
849 * llc_backlog_rcv - Processes rx frames and expired timers.
850 * @sk: LLC sock (p8022 connection)
851 * @skb: queued rx frame or event
853 * This function processes frames that has received and timers that has
854 * expired during sending an I pdu (refer to data_req_handler). frames
855 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
856 * callback functions(llc_c_ac.c).
858 static int llc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
861 struct llc_sock
*llc
= llc_sk(sk
);
863 if (likely(llc_backlog_type(skb
) == LLC_PACKET
)) {
864 if (likely(llc
->state
> 1)) /* not closed */
865 rc
= llc_conn_rcv(sk
, skb
);
868 } else if (llc_backlog_type(skb
) == LLC_EVENT
) {
869 /* timer expiration event */
870 if (likely(llc
->state
> 1)) /* not closed */
871 rc
= llc_conn_state_process(sk
, skb
);
875 printk(KERN_ERR
"%s: invalid skb in backlog\n", __func__
);
886 * llc_sk_init - Initializes a socket with default llc values.
887 * @sk: socket to initialize.
889 * Initializes a socket with default llc values.
891 static void llc_sk_init(struct sock
*sk
)
893 struct llc_sock
*llc
= llc_sk(sk
);
895 llc
->state
= LLC_CONN_STATE_ADM
;
896 llc
->inc_cntr
= llc
->dec_cntr
= 2;
897 llc
->dec_step
= llc
->connect_step
= 1;
899 timer_setup(&llc
->ack_timer
.timer
, llc_conn_ack_tmr_cb
, 0);
900 llc
->ack_timer
.expire
= sysctl_llc2_ack_timeout
;
902 timer_setup(&llc
->pf_cycle_timer
.timer
, llc_conn_pf_cycle_tmr_cb
, 0);
903 llc
->pf_cycle_timer
.expire
= sysctl_llc2_p_timeout
;
905 timer_setup(&llc
->rej_sent_timer
.timer
, llc_conn_rej_tmr_cb
, 0);
906 llc
->rej_sent_timer
.expire
= sysctl_llc2_rej_timeout
;
908 timer_setup(&llc
->busy_state_timer
.timer
, llc_conn_busy_tmr_cb
, 0);
909 llc
->busy_state_timer
.expire
= sysctl_llc2_busy_timeout
;
911 llc
->n2
= 2; /* max retransmit */
912 llc
->k
= 2; /* tx win size, will adjust dynam */
913 llc
->rw
= 128; /* rx win size (opt and equal to
914 * tx_win of remote LLC) */
915 skb_queue_head_init(&llc
->pdu_unack_q
);
916 sk
->sk_backlog_rcv
= llc_backlog_rcv
;
920 * llc_sk_alloc - Allocates LLC sock
921 * @net: network namespace
922 * @family: upper layer protocol family
923 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
924 * @prot: struct proto associated with this new sock instance
925 * @kern: is this to be a kernel socket?
927 * Allocates a LLC sock and initializes it. Returns the new LLC sock
928 * or %NULL if there's no memory available for one
930 struct sock
*llc_sk_alloc(struct net
*net
, int family
, gfp_t priority
, struct proto
*prot
, int kern
)
932 struct sock
*sk
= sk_alloc(net
, family
, priority
, prot
, kern
);
937 sock_init_data(NULL
, sk
);
938 #ifdef LLC_REFCNT_DEBUG
939 atomic_inc(&llc_sock_nr
);
940 printk(KERN_DEBUG
"LLC socket %p created in %s, now we have %d alive\n", sk
,
941 __func__
, atomic_read(&llc_sock_nr
));
947 void llc_sk_stop_all_timers(struct sock
*sk
, bool sync
)
949 struct llc_sock
*llc
= llc_sk(sk
);
952 del_timer_sync(&llc
->pf_cycle_timer
.timer
);
953 del_timer_sync(&llc
->ack_timer
.timer
);
954 del_timer_sync(&llc
->rej_sent_timer
.timer
);
955 del_timer_sync(&llc
->busy_state_timer
.timer
);
957 del_timer(&llc
->pf_cycle_timer
.timer
);
958 del_timer(&llc
->ack_timer
.timer
);
959 del_timer(&llc
->rej_sent_timer
.timer
);
960 del_timer(&llc
->busy_state_timer
.timer
);
963 llc
->ack_must_be_send
= 0;
968 * llc_sk_free - Frees a LLC socket
969 * @sk: - socket to free
973 void llc_sk_free(struct sock
*sk
)
975 struct llc_sock
*llc
= llc_sk(sk
);
977 llc
->state
= LLC_CONN_OUT_OF_SVC
;
978 /* Stop all (possibly) running timers */
979 llc_sk_stop_all_timers(sk
, true);
980 #ifdef DEBUG_LLC_CONN_ALLOC
981 printk(KERN_INFO
"%s: unackq=%d, txq=%d\n", __func__
,
982 skb_queue_len(&llc
->pdu_unack_q
),
983 skb_queue_len(&sk
->sk_write_queue
));
985 skb_queue_purge(&sk
->sk_receive_queue
);
986 skb_queue_purge(&sk
->sk_write_queue
);
987 skb_queue_purge(&llc
->pdu_unack_q
);
988 #ifdef LLC_REFCNT_DEBUG
989 if (refcount_read(&sk
->sk_refcnt
) != 1) {
990 printk(KERN_DEBUG
"Destruction of LLC sock %p delayed in %s, cnt=%d\n",
991 sk
, __func__
, refcount_read(&sk
->sk_refcnt
));
992 printk(KERN_DEBUG
"%d LLC sockets are still alive\n",
993 atomic_read(&llc_sock_nr
));
995 atomic_dec(&llc_sock_nr
);
996 printk(KERN_DEBUG
"LLC socket %p released in %s, %d are still alive\n", sk
,
997 __func__
, atomic_read(&llc_sock_nr
));
1004 * llc_sk_reset - resets a connection
1005 * @sk: LLC socket to reset
1007 * Resets a connection to the out of service state. Stops its timers
1008 * and frees any frames in the queues of the connection.
1010 void llc_sk_reset(struct sock
*sk
)
1012 struct llc_sock
*llc
= llc_sk(sk
);
1014 llc_conn_ac_stop_all_timers(sk
, NULL
);
1015 skb_queue_purge(&sk
->sk_write_queue
);
1016 skb_queue_purge(&llc
->pdu_unack_q
);
1017 llc
->remote_busy_flag
= 0;
1018 llc
->cause_flag
= 0;
1019 llc
->retry_count
= 0;
1020 llc_conn_set_p_flag(sk
, 0);
1024 llc
->first_pdu_Ns
= 0;
1025 llc
->ack_must_be_send
= 0;
1030 llc
->failed_data_req
= 0 ;