GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / llc / llc_conn.c
blob9b1593f0457bc71c6254a30f76117c29393b58dc
1 /*
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>
19 #include <net/sock.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>
26 #define dprintk(args...)
28 static int llc_find_offset(int state, int ev_type);
29 static void llc_conn_send_pdus(struct sock *sk);
30 static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
31 static int llc_exec_conn_trans_actions(struct sock *sk,
32 struct llc_conn_state_trans *trans,
33 struct sk_buff *ev);
34 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
35 struct sk_buff *skb);
37 /* Offset table on connection states transition diagram */
38 static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
40 int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
41 int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
42 int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
43 int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
45 /**
46 * llc_conn_state_process - sends event to connection state machine
47 * @sk: connection
48 * @skb: occurred event
50 * Sends an event to connection state machine. After processing event
51 * (executing it's actions and changing state), upper layer will be
52 * indicated or confirmed, if needed. Returns 0 for success, 1 for
53 * failure. The socket lock has to be held before calling this function.
55 int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
57 int rc;
58 struct llc_sock *llc = llc_sk(skb->sk);
59 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
62 * We have to hold the skb, because llc_conn_service will kfree it in
63 * the sending path and we need to look at the skb->cb, where we encode
64 * llc_conn_state_ev.
66 skb_get(skb);
67 ev->ind_prim = ev->cfm_prim = 0;
69 * Send event to state machine
71 rc = llc_conn_service(skb->sk, skb);
72 if (unlikely(rc != 0)) {
73 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
74 goto out_kfree_skb;
77 if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
78 /* indicate or confirm not required */
79 if (!skb->next)
80 goto out_kfree_skb;
81 goto out_skb_put;
84 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
85 skb_get(skb);
87 switch (ev->ind_prim) {
88 case LLC_DATA_PRIM:
89 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
90 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
92 * shouldn't happen
94 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
95 __func__);
96 kfree_skb(skb);
98 break;
99 case LLC_CONN_PRIM:
101 * Can't be sock_queue_rcv_skb, because we have to leave the
102 * skb->sk pointing to the newly created struct sock in
103 * llc_conn_handler. -acme
105 skb_queue_tail(&sk->sk_receive_queue, skb);
106 sk->sk_state_change(sk);
107 break;
108 case LLC_DISC_PRIM:
109 sock_hold(sk);
110 if (sk->sk_type == SOCK_STREAM &&
111 sk->sk_state == TCP_ESTABLISHED) {
112 sk->sk_shutdown = SHUTDOWN_MASK;
113 sk->sk_socket->state = SS_UNCONNECTED;
114 sk->sk_state = TCP_CLOSE;
115 if (!sock_flag(sk, SOCK_DEAD)) {
116 sock_set_flag(sk, SOCK_DEAD);
117 sk->sk_state_change(sk);
120 kfree_skb(skb);
121 sock_put(sk);
122 break;
123 case LLC_RESET_PRIM:
124 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
125 kfree_skb(skb);
126 break;
127 default:
128 if (ev->ind_prim) {
129 printk(KERN_INFO "%s: received unknown %d prim!\n",
130 __func__, ev->ind_prim);
131 kfree_skb(skb);
133 /* No indication */
134 break;
137 switch (ev->cfm_prim) {
138 case LLC_DATA_PRIM:
139 if (!llc_data_accept_state(llc->state))
140 sk->sk_write_space(sk);
141 else
142 rc = llc->failed_data_req = 1;
143 break;
144 case LLC_CONN_PRIM:
145 if (sk->sk_type == SOCK_STREAM &&
146 sk->sk_state == TCP_SYN_SENT) {
147 if (ev->status) {
148 sk->sk_socket->state = SS_UNCONNECTED;
149 sk->sk_state = TCP_CLOSE;
150 } else {
151 sk->sk_socket->state = SS_CONNECTED;
152 sk->sk_state = TCP_ESTABLISHED;
154 sk->sk_state_change(sk);
156 break;
157 case LLC_DISC_PRIM:
158 sock_hold(sk);
159 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
160 sk->sk_socket->state = SS_UNCONNECTED;
161 sk->sk_state = TCP_CLOSE;
162 sk->sk_state_change(sk);
164 sock_put(sk);
165 break;
166 case LLC_RESET_PRIM:
167 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
168 break;
169 default:
170 if (ev->cfm_prim) {
171 printk(KERN_INFO "%s: received unknown %d prim!\n",
172 __func__, ev->cfm_prim);
173 break;
175 goto out_skb_put; /* No confirmation */
177 out_kfree_skb:
178 kfree_skb(skb);
179 out_skb_put:
180 kfree_skb(skb);
181 return rc;
184 void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
186 /* queue PDU to send to MAC layer */
187 skb_queue_tail(&sk->sk_write_queue, skb);
188 llc_conn_send_pdus(sk);
192 * llc_conn_rtn_pdu - sends received data pdu to upper layer
193 * @sk: Active connection
194 * @skb: Received data frame
196 * Sends received data pdu to upper layer (by using indicate function).
197 * Prepares service parameters (prim and prim_data). calling indication
198 * function will be done in llc_conn_state_process.
200 void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
202 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
204 ev->ind_prim = LLC_DATA_PRIM;
208 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
209 * @sk: active connection
210 * @nr: NR
211 * @first_p_bit: p_bit value of first pdu
213 * Resend all unacknowledged I PDUs, starting with the NR; send first as
214 * command PDU with P bit equal first_p_bit; if more than one send
215 * subsequent as command PDUs with P bit equal zero (0).
217 void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
219 struct sk_buff *skb;
220 struct llc_pdu_sn *pdu;
221 u16 nbr_unack_pdus;
222 struct llc_sock *llc;
223 u8 howmany_resend = 0;
225 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
226 if (!nbr_unack_pdus)
227 goto out;
229 * Process unack PDUs only if unack queue is not empty; remove
230 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
232 llc = llc_sk(sk);
234 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
235 pdu = llc_pdu_sn_hdr(skb);
236 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
237 llc_pdu_set_pf_bit(skb, first_p_bit);
238 skb_queue_tail(&sk->sk_write_queue, skb);
239 first_p_bit = 0;
240 llc->vS = LLC_I_GET_NS(pdu);
241 howmany_resend++;
243 if (howmany_resend > 0)
244 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
245 /* any PDUs to re-send are queued up; start sending to MAC */
246 llc_conn_send_pdus(sk);
247 out:;
251 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
252 * @sk: active connection.
253 * @nr: NR
254 * @first_f_bit: f_bit value of first pdu.
256 * Resend all unacknowledged I PDUs, starting with the NR; send first as
257 * response PDU with F bit equal first_f_bit; if more than one send
258 * subsequent as response PDUs with F bit equal zero (0).
260 void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
262 struct sk_buff *skb;
263 u16 nbr_unack_pdus;
264 struct llc_sock *llc = llc_sk(sk);
265 u8 howmany_resend = 0;
267 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
268 if (!nbr_unack_pdus)
269 goto out;
271 * Process unack PDUs only if unack queue is not empty; remove
272 * appropriate PDUs, fix them up, and put them on mac_pdu_q
274 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
275 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
277 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
278 llc_pdu_set_pf_bit(skb, first_f_bit);
279 skb_queue_tail(&sk->sk_write_queue, skb);
280 first_f_bit = 0;
281 llc->vS = LLC_I_GET_NS(pdu);
282 howmany_resend++;
284 if (howmany_resend > 0)
285 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
286 /* any PDUs to re-send are queued up; start sending to MAC */
287 llc_conn_send_pdus(sk);
288 out:;
292 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
293 * @sk: active connection
294 * nr: NR
295 * how_many_unacked: size of pdu_unack_q after removing acked pdus
297 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
298 * the number of pdus that removed from queue.
300 int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
302 int pdu_pos, i;
303 struct sk_buff *skb;
304 struct llc_pdu_sn *pdu;
305 int nbr_acked = 0;
306 struct llc_sock *llc = llc_sk(sk);
307 int q_len = skb_queue_len(&llc->pdu_unack_q);
309 if (!q_len)
310 goto out;
311 skb = skb_peek(&llc->pdu_unack_q);
312 pdu = llc_pdu_sn_hdr(skb);
314 /* finding position of last acked pdu in queue */
315 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
316 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
318 for (i = 0; i < pdu_pos && i < q_len; i++) {
319 skb = skb_dequeue(&llc->pdu_unack_q);
320 kfree_skb(skb);
321 nbr_acked++;
323 out:
324 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
325 return nbr_acked;
329 * llc_conn_send_pdus - Sends queued PDUs
330 * @sk: active connection
332 * Sends queued pdus to MAC layer for transmission.
334 static void llc_conn_send_pdus(struct sock *sk)
336 struct sk_buff *skb;
338 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
339 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
341 if (LLC_PDU_TYPE_IS_I(pdu) &&
342 !(skb->dev->flags & IFF_LOOPBACK)) {
343 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
345 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
346 if (!skb2)
347 break;
348 skb = skb2;
350 dev_queue_xmit(skb);
355 * llc_conn_service - finds transition and changes state of connection
356 * @sk: connection
357 * @skb: happened event
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.
363 static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
365 int rc = 1;
366 struct llc_sock *llc = llc_sk(sk);
367 struct llc_conn_state_trans *trans;
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);
381 out:
382 return rc;
386 * llc_qualify_conn_ev - finds transition for event
387 * @sk: connection
388 * @skb: happened event
390 * This function finds transition that matches with happened event.
391 * Returns pointer to found transition on success, %NULL otherwise.
393 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
394 struct sk_buff *skb)
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];
403 /* search thru events for this state until
404 * list exhausted or until no more
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
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
425 return *next_trans;
428 return NULL;
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
437 * Executes actions that is related to happened event. Returns 0 for
438 * success, 1 to indicate failure of at least one action.
440 static int llc_exec_conn_trans_actions(struct sock *sk,
441 struct llc_conn_state_trans *trans,
442 struct sk_buff *skb)
444 int rc = 0;
445 llc_conn_action_t *next_action;
447 for (next_action = trans->ev_actions;
448 next_action && *next_action; next_action++) {
449 int rc2 = (*next_action)(sk, skb);
451 if (rc2 == 2) {
452 rc = rc2;
453 break;
454 } else if (rc2)
455 rc = 1;
457 return rc;
460 static inline bool llc_estab_match(const struct llc_sap *sap,
461 const struct llc_addr *daddr,
462 const struct llc_addr *laddr,
463 const struct sock *sk)
465 struct llc_sock *llc = llc_sk(sk);
467 return llc->laddr.lsap == laddr->lsap &&
468 llc->daddr.lsap == daddr->lsap &&
469 llc_mac_match(llc->laddr.mac, laddr->mac) &&
470 llc_mac_match(llc->daddr.mac, daddr->mac);
474 * __llc_lookup_established - Finds connection for the remote/local sap/mac
475 * @sap: SAP
476 * @daddr: address of remote LLC (MAC + SAP)
477 * @laddr: address of local LLC (MAC + SAP)
479 * Search connection list of the SAP and finds connection using the remote
480 * mac, remote sap, local mac, and local sap. Returns pointer for
481 * connection found, %NULL otherwise.
482 * Caller has to make sure local_bh is disabled.
484 static struct sock *__llc_lookup_established(struct llc_sap *sap,
485 struct llc_addr *daddr,
486 struct llc_addr *laddr)
488 struct sock *rc;
489 struct hlist_nulls_node *node;
490 int slot = llc_sk_laddr_hashfn(sap, laddr);
491 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
493 rcu_read_lock();
494 again:
495 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
496 if (llc_estab_match(sap, daddr, laddr, rc)) {
497 /* Extra checks required by SLAB_DESTROY_BY_RCU */
498 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
499 goto again;
500 if (unlikely(llc_sk(rc)->sap != sap ||
501 !llc_estab_match(sap, daddr, laddr, rc))) {
502 sock_put(rc);
503 continue;
505 goto found;
508 rc = NULL;
510 * if the nulls value we got at the end of this lookup is
511 * not the expected one, we must restart lookup.
512 * We probably met an item that was moved to another chain.
514 if (unlikely(get_nulls_value(node) != slot))
515 goto again;
516 found:
517 rcu_read_unlock();
518 return rc;
521 struct sock *llc_lookup_established(struct llc_sap *sap,
522 struct llc_addr *daddr,
523 struct llc_addr *laddr)
525 struct sock *sk;
527 local_bh_disable();
528 sk = __llc_lookup_established(sap, daddr, laddr);
529 local_bh_enable();
530 return sk;
533 static inline bool llc_listener_match(const struct llc_sap *sap,
534 const struct llc_addr *laddr,
535 const struct sock *sk)
537 struct llc_sock *llc = llc_sk(sk);
539 return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
540 llc->laddr.lsap == laddr->lsap &&
541 llc_mac_match(llc->laddr.mac, laddr->mac);
544 static struct sock *__llc_lookup_listener(struct llc_sap *sap,
545 struct llc_addr *laddr)
547 struct sock *rc;
548 struct hlist_nulls_node *node;
549 int slot = llc_sk_laddr_hashfn(sap, laddr);
550 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
552 rcu_read_lock();
553 again:
554 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
555 if (llc_listener_match(sap, laddr, rc)) {
556 /* Extra checks required by SLAB_DESTROY_BY_RCU */
557 if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
558 goto again;
559 if (unlikely(llc_sk(rc)->sap != sap ||
560 !llc_listener_match(sap, laddr, rc))) {
561 sock_put(rc);
562 continue;
564 goto found;
567 rc = NULL;
569 * if the nulls value we got at the end of this lookup is
570 * not the expected one, we must restart lookup.
571 * We probably met an item that was moved to another chain.
573 if (unlikely(get_nulls_value(node) != slot))
574 goto again;
575 found:
576 rcu_read_unlock();
577 return rc;
581 * llc_lookup_listener - Finds listener for local MAC + SAP
582 * @sap: SAP
583 * @laddr: address of local LLC (MAC + SAP)
585 * Search connection list of the SAP and finds connection listening on
586 * local mac, and local sap. Returns pointer for parent socket found,
587 * %NULL otherwise.
588 * Caller has to make sure local_bh is disabled.
590 static struct sock *llc_lookup_listener(struct llc_sap *sap,
591 struct llc_addr *laddr)
593 static struct llc_addr null_addr;
594 struct sock *rc = __llc_lookup_listener(sap, laddr);
596 if (!rc)
597 rc = __llc_lookup_listener(sap, &null_addr);
599 return rc;
602 static struct sock *__llc_lookup(struct llc_sap *sap,
603 struct llc_addr *daddr,
604 struct llc_addr *laddr)
606 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
608 return sk ? : llc_lookup_listener(sap, laddr);
612 * llc_data_accept_state - designates if in this state data can be sent.
613 * @state: state of connection.
615 * Returns 0 if data can be sent, 1 otherwise.
617 u8 llc_data_accept_state(u8 state)
619 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
620 state != LLC_CONN_STATE_REJ;
624 * llc_find_next_offset - finds offset for next category of transitions
625 * @state: state table.
626 * @offset: start offset.
628 * Finds offset of next category of transitions in transition table.
629 * Returns the start index of next category.
631 static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
633 u16 cnt = 0;
634 struct llc_conn_state_trans **next_trans;
636 for (next_trans = state->transitions + offset;
637 (*next_trans)->ev; next_trans++)
638 ++cnt;
639 return cnt;
643 * llc_build_offset_table - builds offset table of connection
645 * Fills offset table of connection state transition table
646 * (llc_offset_table).
648 void __init llc_build_offset_table(void)
650 struct llc_conn_state *curr_state;
651 int state, ev_type, next_offset;
653 for (state = 0; state < NBR_CONN_STATES; state++) {
654 curr_state = &llc_conn_state_table[state];
655 next_offset = 0;
656 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
657 llc_offset_table[state][ev_type] = next_offset;
658 next_offset += llc_find_next_offset(curr_state,
659 next_offset) + 1;
665 * llc_find_offset - finds start offset of category of transitions
666 * @state: state of connection
667 * @ev_type: type of happened event
669 * Finds start offset of desired category of transitions. Returns the
670 * desired start offset.
672 static int llc_find_offset(int state, int ev_type)
674 int rc = 0;
675 /* at this stage, llc_offset_table[..][2] is not important. it is for
676 * init_pf_cycle and I don't know what is it.
678 switch (ev_type) {
679 case LLC_CONN_EV_TYPE_PRIM:
680 rc = llc_offset_table[state][0]; break;
681 case LLC_CONN_EV_TYPE_PDU:
682 rc = llc_offset_table[state][4]; break;
683 case LLC_CONN_EV_TYPE_SIMPLE:
684 rc = llc_offset_table[state][1]; break;
685 case LLC_CONN_EV_TYPE_P_TMR:
686 case LLC_CONN_EV_TYPE_ACK_TMR:
687 case LLC_CONN_EV_TYPE_REJ_TMR:
688 case LLC_CONN_EV_TYPE_BUSY_TMR:
689 rc = llc_offset_table[state][3]; break;
691 return rc;
695 * llc_sap_add_socket - adds a socket to a SAP
696 * @sap: SAP
697 * @sk: socket
699 * This function adds a socket to the hash tables of a SAP.
701 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
703 struct llc_sock *llc = llc_sk(sk);
704 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
705 struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
707 llc_sap_hold(sap);
708 llc_sk(sk)->sap = sap;
710 spin_lock_bh(&sap->sk_lock);
711 sap->sk_count++;
712 sk_nulls_add_node_rcu(sk, laddr_hb);
713 hlist_add_head(&llc->dev_hash_node, dev_hb);
714 spin_unlock_bh(&sap->sk_lock);
718 * llc_sap_remove_socket - removes a socket from SAP
719 * @sap: SAP
720 * @sk: socket
722 * This function removes a connection from the hash tables of a SAP if
723 * the connection was in this list.
725 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
727 struct llc_sock *llc = llc_sk(sk);
729 spin_lock_bh(&sap->sk_lock);
730 sk_nulls_del_node_init_rcu(sk);
731 hlist_del(&llc->dev_hash_node);
732 sap->sk_count--;
733 spin_unlock_bh(&sap->sk_lock);
734 llc_sap_put(sap);
738 * llc_conn_rcv - sends received pdus to the connection state machine
739 * @sk: current connection structure.
740 * @skb: received frame.
742 * Sends received pdus to the connection state machine.
744 static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
746 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
748 ev->type = LLC_CONN_EV_TYPE_PDU;
749 ev->reason = 0;
750 return llc_conn_state_process(sk, skb);
753 static struct sock *llc_create_incoming_sock(struct sock *sk,
754 struct net_device *dev,
755 struct llc_addr *saddr,
756 struct llc_addr *daddr)
758 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
759 sk->sk_prot);
760 struct llc_sock *newllc, *llc = llc_sk(sk);
762 if (!newsk)
763 goto out;
764 newllc = llc_sk(newsk);
765 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
766 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
767 newllc->dev = dev;
768 dev_hold(dev);
769 llc_sap_add_socket(llc->sap, newsk);
770 llc_sap_hold(llc->sap);
771 out:
772 return newsk;
775 void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
777 struct llc_addr saddr, daddr;
778 struct sock *sk;
780 llc_pdu_decode_sa(skb, saddr.mac);
781 llc_pdu_decode_ssap(skb, &saddr.lsap);
782 llc_pdu_decode_da(skb, daddr.mac);
783 llc_pdu_decode_dsap(skb, &daddr.lsap);
785 sk = __llc_lookup(sap, &saddr, &daddr);
786 if (!sk)
787 goto drop;
789 bh_lock_sock(sk);
791 * This has to be done here and not at the upper layer ->accept
792 * method because of the way the PROCOM state machine works:
793 * it needs to set several state variables (see, for instance,
794 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
795 * the originator of the new connection, and this state has to be
796 * in the newly created struct sock private area. -acme
798 if (unlikely(sk->sk_state == TCP_LISTEN)) {
799 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
800 &saddr, &daddr);
801 if (!newsk)
802 goto drop_unlock;
803 skb_set_owner_r(skb, newsk);
804 } else {
806 * Can't be skb_set_owner_r, this will be done at the
807 * llc_conn_state_process function, later on, when we will use
808 * skb_queue_rcv_skb to send it to upper layers, this is
809 * another trick required to cope with how the PROCOM state
810 * machine works. -acme
812 skb->sk = sk;
814 if (!sock_owned_by_user(sk))
815 llc_conn_rcv(sk, skb);
816 else {
817 dprintk("%s: adding to backlog...\n", __func__);
818 llc_set_backlog_type(skb, LLC_PACKET);
819 if (sk_add_backlog(sk, skb))
820 goto drop_unlock;
822 out:
823 bh_unlock_sock(sk);
824 sock_put(sk);
825 return;
826 drop:
827 kfree_skb(skb);
828 return;
829 drop_unlock:
830 kfree_skb(skb);
831 goto out;
834 #undef LLC_REFCNT_DEBUG
835 #ifdef LLC_REFCNT_DEBUG
836 static atomic_t llc_sock_nr;
837 #endif
840 * llc_backlog_rcv - Processes rx frames and expired timers.
841 * @sk: LLC sock (p8022 connection)
842 * @skb: queued rx frame or event
844 * This function processes frames that has received and timers that has
845 * expired during sending an I pdu (refer to data_req_handler). frames
846 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
847 * callback functions(llc_c_ac.c).
849 static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
851 int rc = 0;
852 struct llc_sock *llc = llc_sk(sk);
854 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
855 if (likely(llc->state > 1)) /* not closed */
856 rc = llc_conn_rcv(sk, skb);
857 else
858 goto out_kfree_skb;
859 } else if (llc_backlog_type(skb) == LLC_EVENT) {
860 /* timer expiration event */
861 if (likely(llc->state > 1)) /* not closed */
862 rc = llc_conn_state_process(sk, skb);
863 else
864 goto out_kfree_skb;
865 } else {
866 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
867 goto out_kfree_skb;
869 out:
870 return rc;
871 out_kfree_skb:
872 kfree_skb(skb);
873 goto out;
877 * llc_sk_init - Initializes a socket with default llc values.
878 * @sk: socket to initialize.
880 * Initializes a socket with default llc values.
882 static void llc_sk_init(struct sock* sk)
884 struct llc_sock *llc = llc_sk(sk);
886 llc->state = LLC_CONN_STATE_ADM;
887 llc->inc_cntr = llc->dec_cntr = 2;
888 llc->dec_step = llc->connect_step = 1;
890 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
891 (unsigned long)sk);
892 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
894 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
895 (unsigned long)sk);
896 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
898 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
899 (unsigned long)sk);
900 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
902 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
903 (unsigned long)sk);
904 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
906 llc->n2 = 2; /* max retransmit */
907 llc->k = 2; /* tx win size, will adjust dynam */
908 llc->rw = 128; /* rx win size (opt and equal to
909 * tx_win of remote LLC) */
910 skb_queue_head_init(&llc->pdu_unack_q);
911 sk->sk_backlog_rcv = llc_backlog_rcv;
915 * llc_sk_alloc - Allocates LLC sock
916 * @family: upper layer protocol family
917 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
919 * Allocates a LLC sock and initializes it. Returns the new LLC sock
920 * or %NULL if there's no memory available for one
922 struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
924 struct sock *sk = sk_alloc(net, family, priority, prot);
926 if (!sk)
927 goto out;
928 llc_sk_init(sk);
929 sock_init_data(NULL, sk);
930 #ifdef LLC_REFCNT_DEBUG
931 atomic_inc(&llc_sock_nr);
932 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
933 __func__, atomic_read(&llc_sock_nr));
934 #endif
935 out:
936 return sk;
940 * llc_sk_free - Frees a LLC socket
941 * @sk - socket to free
943 * Frees a LLC socket
945 void llc_sk_free(struct sock *sk)
947 struct llc_sock *llc = llc_sk(sk);
949 llc->state = LLC_CONN_OUT_OF_SVC;
950 /* Stop all (possibly) running timers */
951 llc_conn_ac_stop_all_timers(sk, NULL);
952 #ifdef DEBUG_LLC_CONN_ALLOC
953 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
954 skb_queue_len(&llc->pdu_unack_q),
955 skb_queue_len(&sk->sk_write_queue));
956 #endif
957 skb_queue_purge(&sk->sk_receive_queue);
958 skb_queue_purge(&sk->sk_write_queue);
959 skb_queue_purge(&llc->pdu_unack_q);
960 #ifdef LLC_REFCNT_DEBUG
961 if (atomic_read(&sk->sk_refcnt) != 1) {
962 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
963 sk, __func__, atomic_read(&sk->sk_refcnt));
964 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
965 atomic_read(&llc_sock_nr));
966 } else {
967 atomic_dec(&llc_sock_nr);
968 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
969 __func__, atomic_read(&llc_sock_nr));
971 #endif
972 sock_put(sk);
976 * llc_sk_reset - resets a connection
977 * @sk: LLC socket to reset
979 * Resets a connection to the out of service state. Stops its timers
980 * and frees any frames in the queues of the connection.
982 void llc_sk_reset(struct sock *sk)
984 struct llc_sock *llc = llc_sk(sk);
986 llc_conn_ac_stop_all_timers(sk, NULL);
987 skb_queue_purge(&sk->sk_write_queue);
988 skb_queue_purge(&llc->pdu_unack_q);
989 llc->remote_busy_flag = 0;
990 llc->cause_flag = 0;
991 llc->retry_count = 0;
992 llc_conn_set_p_flag(sk, 0);
993 llc->f_flag = 0;
994 llc->s_flag = 0;
995 llc->ack_pf = 0;
996 llc->first_pdu_Ns = 0;
997 llc->ack_must_be_send = 0;
998 llc->dec_step = 1;
999 llc->inc_cntr = 2;
1000 llc->dec_cntr = 2;
1001 llc->X = 0;
1002 llc->failed_data_req = 0 ;
1003 llc->last_nr = 0;