Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6/mini2440.git] / net / llc / llc_conn.c
blob3477624a4906ec3fc65957a1eb1567f0864153f4
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 <net/llc_sap.h>
17 #include <net/llc_conn.h>
18 #include <net/sock.h>
19 #include <net/tcp_states.h>
20 #include <net/llc_c_ev.h>
21 #include <net/llc_c_ac.h>
22 #include <net/llc_c_st.h>
23 #include <net/llc_pdu.h>
25 #if 0
26 #define dprintk(args...) printk(KERN_DEBUG args)
27 #else
28 #define dprintk(args...)
29 #endif
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,
36 struct sk_buff *ev);
37 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
38 struct sk_buff *skb);
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;
48 /**
49 * llc_conn_state_process - sends event to connection state machine
50 * @sk: connection
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)
60 int rc;
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
67 * llc_conn_state_ev.
69 skb_get(skb);
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__);
77 goto out_kfree_skb;
80 if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
81 /* indicate or confirm not required */
82 /* XXX this is not very pretty, perhaps we should store
83 * XXX indicate/confirm-needed state in the llc_conn_state_ev
84 * XXX control block of the SKB instead? -DaveM
86 if (!skb->next)
87 goto out_kfree_skb;
88 goto out_skb_put;
91 if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
92 skb_get(skb);
94 switch (ev->ind_prim) {
95 case LLC_DATA_PRIM:
96 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
97 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
99 * shouldn't happen
101 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
102 __func__);
103 kfree_skb(skb);
105 break;
106 case LLC_CONN_PRIM:
108 * Can't be sock_queue_rcv_skb, because we have to leave the
109 * skb->sk pointing to the newly created struct sock in
110 * llc_conn_handler. -acme
112 skb_queue_tail(&sk->sk_receive_queue, skb);
113 sk->sk_state_change(sk);
114 break;
115 case LLC_DISC_PRIM:
116 sock_hold(sk);
117 if (sk->sk_type == SOCK_STREAM &&
118 sk->sk_state == TCP_ESTABLISHED) {
119 sk->sk_shutdown = SHUTDOWN_MASK;
120 sk->sk_socket->state = SS_UNCONNECTED;
121 sk->sk_state = TCP_CLOSE;
122 if (!sock_flag(sk, SOCK_DEAD)) {
123 sock_set_flag(sk, SOCK_DEAD);
124 sk->sk_state_change(sk);
127 kfree_skb(skb);
128 sock_put(sk);
129 break;
130 case LLC_RESET_PRIM:
132 * FIXME:
133 * RESET is not being notified to upper layers for now
135 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
136 kfree_skb(skb);
137 break;
138 default:
139 if (ev->ind_prim) {
140 printk(KERN_INFO "%s: received unknown %d prim!\n",
141 __func__, ev->ind_prim);
142 kfree_skb(skb);
144 /* No indication */
145 break;
148 switch (ev->cfm_prim) {
149 case LLC_DATA_PRIM:
150 if (!llc_data_accept_state(llc->state))
151 sk->sk_write_space(sk);
152 else
153 rc = llc->failed_data_req = 1;
154 break;
155 case LLC_CONN_PRIM:
156 if (sk->sk_type == SOCK_STREAM &&
157 sk->sk_state == TCP_SYN_SENT) {
158 if (ev->status) {
159 sk->sk_socket->state = SS_UNCONNECTED;
160 sk->sk_state = TCP_CLOSE;
161 } else {
162 sk->sk_socket->state = SS_CONNECTED;
163 sk->sk_state = TCP_ESTABLISHED;
165 sk->sk_state_change(sk);
167 break;
168 case LLC_DISC_PRIM:
169 sock_hold(sk);
170 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
171 sk->sk_socket->state = SS_UNCONNECTED;
172 sk->sk_state = TCP_CLOSE;
173 sk->sk_state_change(sk);
175 sock_put(sk);
176 break;
177 case LLC_RESET_PRIM:
179 * FIXME:
180 * RESET is not being notified to upper layers for now
182 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
183 break;
184 default:
185 if (ev->cfm_prim) {
186 printk(KERN_INFO "%s: received unknown %d prim!\n",
187 __func__, ev->cfm_prim);
188 break;
190 goto out_skb_put; /* No confirmation */
192 out_kfree_skb:
193 kfree_skb(skb);
194 out_skb_put:
195 kfree_skb(skb);
196 return rc;
199 void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
201 /* queue PDU to send to MAC layer */
202 skb_queue_tail(&sk->sk_write_queue, skb);
203 llc_conn_send_pdus(sk);
207 * llc_conn_rtn_pdu - sends received data pdu to upper layer
208 * @sk: Active connection
209 * @skb: Received data frame
211 * Sends received data pdu to upper layer (by using indicate function).
212 * Prepares service parameters (prim and prim_data). calling indication
213 * function will be done in llc_conn_state_process.
215 void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
217 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
219 ev->ind_prim = LLC_DATA_PRIM;
223 * llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
224 * @sk: active connection
225 * @nr: NR
226 * @first_p_bit: p_bit value of first pdu
228 * Resend all unacknowledged I PDUs, starting with the NR; send first as
229 * command PDU with P bit equal first_p_bit; if more than one send
230 * subsequent as command PDUs with P bit equal zero (0).
232 void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
234 struct sk_buff *skb;
235 struct llc_pdu_sn *pdu;
236 u16 nbr_unack_pdus;
237 struct llc_sock *llc;
238 u8 howmany_resend = 0;
240 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
241 if (!nbr_unack_pdus)
242 goto out;
244 * Process unack PDUs only if unack queue is not empty; remove
245 * appropriate PDUs, fix them up, and put them on mac_pdu_q.
247 llc = llc_sk(sk);
249 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
250 pdu = llc_pdu_sn_hdr(skb);
251 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
252 llc_pdu_set_pf_bit(skb, first_p_bit);
253 skb_queue_tail(&sk->sk_write_queue, skb);
254 first_p_bit = 0;
255 llc->vS = LLC_I_GET_NS(pdu);
256 howmany_resend++;
258 if (howmany_resend > 0)
259 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
260 /* any PDUs to re-send are queued up; start sending to MAC */
261 llc_conn_send_pdus(sk);
262 out:;
266 * llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
267 * @sk: active connection.
268 * @nr: NR
269 * @first_f_bit: f_bit value of first pdu.
271 * Resend all unacknowledged I PDUs, starting with the NR; send first as
272 * response PDU with F bit equal first_f_bit; if more than one send
273 * subsequent as response PDUs with F bit equal zero (0).
275 void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
277 struct sk_buff *skb;
278 u16 nbr_unack_pdus;
279 struct llc_sock *llc = llc_sk(sk);
280 u8 howmany_resend = 0;
282 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
283 if (!nbr_unack_pdus)
284 goto out;
286 * Process unack PDUs only if unack queue is not empty; remove
287 * appropriate PDUs, fix them up, and put them on mac_pdu_q
289 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
290 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
292 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
293 llc_pdu_set_pf_bit(skb, first_f_bit);
294 skb_queue_tail(&sk->sk_write_queue, skb);
295 first_f_bit = 0;
296 llc->vS = LLC_I_GET_NS(pdu);
297 howmany_resend++;
299 if (howmany_resend > 0)
300 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
301 /* any PDUs to re-send are queued up; start sending to MAC */
302 llc_conn_send_pdus(sk);
303 out:;
307 * llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
308 * @sk: active connection
309 * nr: NR
310 * how_many_unacked: size of pdu_unack_q after removing acked pdus
312 * Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
313 * the number of pdus that removed from queue.
315 int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
317 int pdu_pos, i;
318 struct sk_buff *skb;
319 struct llc_pdu_sn *pdu;
320 int nbr_acked = 0;
321 struct llc_sock *llc = llc_sk(sk);
322 int q_len = skb_queue_len(&llc->pdu_unack_q);
324 if (!q_len)
325 goto out;
326 skb = skb_peek(&llc->pdu_unack_q);
327 pdu = llc_pdu_sn_hdr(skb);
329 /* finding position of last acked pdu in queue */
330 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
331 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
333 for (i = 0; i < pdu_pos && i < q_len; i++) {
334 skb = skb_dequeue(&llc->pdu_unack_q);
335 kfree_skb(skb);
336 nbr_acked++;
338 out:
339 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
340 return nbr_acked;
344 * llc_conn_send_pdus - Sends queued PDUs
345 * @sk: active connection
347 * Sends queued pdus to MAC layer for transmission.
349 static void llc_conn_send_pdus(struct sock *sk)
351 struct sk_buff *skb;
353 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
354 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
356 if (LLC_PDU_TYPE_IS_I(pdu) &&
357 !(skb->dev->flags & IFF_LOOPBACK)) {
358 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
360 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
361 if (!skb2)
362 break;
363 skb = skb2;
365 dev_queue_xmit(skb);
370 * llc_conn_service - finds transition and changes state of connection
371 * @sk: connection
372 * @skb: happened event
374 * This function finds transition that matches with happened event, then
375 * executes related actions and finally changes state of connection.
376 * Returns 0 for success, 1 for failure.
378 static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
380 int rc = 1;
381 struct llc_sock *llc = llc_sk(sk);
382 struct llc_conn_state_trans *trans;
384 if (llc->state > NBR_CONN_STATES)
385 goto out;
386 rc = 0;
387 trans = llc_qualify_conn_ev(sk, skb);
388 if (trans) {
389 rc = llc_exec_conn_trans_actions(sk, trans, skb);
390 if (!rc && trans->next_state != NO_STATE_CHANGE) {
391 llc->state = trans->next_state;
392 if (!llc_data_accept_state(llc->state))
393 sk->sk_state_change(sk);
396 out:
397 return rc;
401 * llc_qualify_conn_ev - finds transition for event
402 * @sk: connection
403 * @skb: happened event
405 * This function finds transition that matches with happened event.
406 * Returns pointer to found transition on success, %NULL otherwise.
408 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
409 struct sk_buff *skb)
411 struct llc_conn_state_trans **next_trans;
412 llc_conn_ev_qfyr_t *next_qualifier;
413 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
414 struct llc_sock *llc = llc_sk(sk);
415 struct llc_conn_state *curr_state =
416 &llc_conn_state_table[llc->state - 1];
418 /* search thru events for this state until
419 * list exhausted or until no more
421 for (next_trans = curr_state->transitions +
422 llc_find_offset(llc->state - 1, ev->type);
423 (*next_trans)->ev; next_trans++) {
424 if (!((*next_trans)->ev)(sk, skb)) {
425 /* got POSSIBLE event match; the event may require
426 * qualification based on the values of a number of
427 * state flags; if all qualifications are met (i.e.,
428 * if all qualifying functions return success, or 0,
429 * then this is THE event we're looking for
431 for (next_qualifier = (*next_trans)->ev_qualifiers;
432 next_qualifier && *next_qualifier &&
433 !(*next_qualifier)(sk, skb); next_qualifier++)
434 /* nothing */;
435 if (!next_qualifier || !*next_qualifier)
436 /* all qualifiers executed successfully; this is
437 * our transition; return it so we can perform
438 * the associated actions & change the state
440 return *next_trans;
443 return NULL;
447 * llc_exec_conn_trans_actions - executes related actions
448 * @sk: connection
449 * @trans: transition that it's actions must be performed
450 * @skb: event
452 * Executes actions that is related to happened event. Returns 0 for
453 * success, 1 to indicate failure of at least one action.
455 static int llc_exec_conn_trans_actions(struct sock *sk,
456 struct llc_conn_state_trans *trans,
457 struct sk_buff *skb)
459 int rc = 0;
460 llc_conn_action_t *next_action;
462 for (next_action = trans->ev_actions;
463 next_action && *next_action; next_action++) {
464 int rc2 = (*next_action)(sk, skb);
466 if (rc2 == 2) {
467 rc = rc2;
468 break;
469 } else if (rc2)
470 rc = 1;
472 return rc;
476 * __llc_lookup_established - Finds connection for the remote/local sap/mac
477 * @sap: SAP
478 * @daddr: address of remote LLC (MAC + SAP)
479 * @laddr: address of local LLC (MAC + SAP)
481 * Search connection list of the SAP and finds connection using the remote
482 * mac, remote sap, local mac, and local sap. Returns pointer for
483 * connection found, %NULL otherwise.
484 * Caller has to make sure local_bh is disabled.
486 static struct sock *__llc_lookup_established(struct llc_sap *sap,
487 struct llc_addr *daddr,
488 struct llc_addr *laddr)
490 struct sock *rc;
491 struct hlist_node *node;
493 read_lock(&sap->sk_list.lock);
494 sk_for_each(rc, node, &sap->sk_list.list) {
495 struct llc_sock *llc = llc_sk(rc);
497 if (llc->laddr.lsap == laddr->lsap &&
498 llc->daddr.lsap == daddr->lsap &&
499 llc_mac_match(llc->laddr.mac, laddr->mac) &&
500 llc_mac_match(llc->daddr.mac, daddr->mac)) {
501 sock_hold(rc);
502 goto found;
505 rc = NULL;
506 found:
507 read_unlock(&sap->sk_list.lock);
508 return rc;
511 struct sock *llc_lookup_established(struct llc_sap *sap,
512 struct llc_addr *daddr,
513 struct llc_addr *laddr)
515 struct sock *sk;
517 local_bh_disable();
518 sk = __llc_lookup_established(sap, daddr, laddr);
519 local_bh_enable();
520 return sk;
524 * llc_lookup_listener - Finds listener for local MAC + SAP
525 * @sap: SAP
526 * @laddr: address of local LLC (MAC + SAP)
528 * Search connection list of the SAP and finds connection listening on
529 * local mac, and local sap. Returns pointer for parent socket found,
530 * %NULL otherwise.
531 * Caller has to make sure local_bh is disabled.
533 static struct sock *llc_lookup_listener(struct llc_sap *sap,
534 struct llc_addr *laddr)
536 struct sock *rc;
537 struct hlist_node *node;
539 read_lock(&sap->sk_list.lock);
540 sk_for_each(rc, node, &sap->sk_list.list) {
541 struct llc_sock *llc = llc_sk(rc);
543 if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
544 llc->laddr.lsap == laddr->lsap &&
545 (llc_mac_match(llc->laddr.mac, laddr->mac) ||
546 llc_mac_null(llc->laddr.mac))) {
547 sock_hold(rc);
548 goto found;
551 rc = NULL;
552 found:
553 read_unlock(&sap->sk_list.lock);
554 return rc;
557 static struct sock *__llc_lookup(struct llc_sap *sap,
558 struct llc_addr *daddr,
559 struct llc_addr *laddr)
561 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
563 return sk ? : llc_lookup_listener(sap, laddr);
567 * llc_data_accept_state - designates if in this state data can be sent.
568 * @state: state of connection.
570 * Returns 0 if data can be sent, 1 otherwise.
572 u8 llc_data_accept_state(u8 state)
574 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
575 state != LLC_CONN_STATE_REJ;
579 * llc_find_next_offset - finds offset for next category of transitions
580 * @state: state table.
581 * @offset: start offset.
583 * Finds offset of next category of transitions in transition table.
584 * Returns the start index of next category.
586 static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
588 u16 cnt = 0;
589 struct llc_conn_state_trans **next_trans;
591 for (next_trans = state->transitions + offset;
592 (*next_trans)->ev; next_trans++)
593 ++cnt;
594 return cnt;
598 * llc_build_offset_table - builds offset table of connection
600 * Fills offset table of connection state transition table
601 * (llc_offset_table).
603 void __init llc_build_offset_table(void)
605 struct llc_conn_state *curr_state;
606 int state, ev_type, next_offset;
608 for (state = 0; state < NBR_CONN_STATES; state++) {
609 curr_state = &llc_conn_state_table[state];
610 next_offset = 0;
611 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
612 llc_offset_table[state][ev_type] = next_offset;
613 next_offset += llc_find_next_offset(curr_state,
614 next_offset) + 1;
620 * llc_find_offset - finds start offset of category of transitions
621 * @state: state of connection
622 * @ev_type: type of happened event
624 * Finds start offset of desired category of transitions. Returns the
625 * desired start offset.
627 static int llc_find_offset(int state, int ev_type)
629 int rc = 0;
630 /* at this stage, llc_offset_table[..][2] is not important. it is for
631 * init_pf_cycle and I don't know what is it.
633 switch (ev_type) {
634 case LLC_CONN_EV_TYPE_PRIM:
635 rc = llc_offset_table[state][0]; break;
636 case LLC_CONN_EV_TYPE_PDU:
637 rc = llc_offset_table[state][4]; break;
638 case LLC_CONN_EV_TYPE_SIMPLE:
639 rc = llc_offset_table[state][1]; break;
640 case LLC_CONN_EV_TYPE_P_TMR:
641 case LLC_CONN_EV_TYPE_ACK_TMR:
642 case LLC_CONN_EV_TYPE_REJ_TMR:
643 case LLC_CONN_EV_TYPE_BUSY_TMR:
644 rc = llc_offset_table[state][3]; break;
646 return rc;
650 * llc_sap_add_socket - adds a socket to a SAP
651 * @sap: SAP
652 * @sk: socket
654 * This function adds a socket to sk_list of a SAP.
656 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
658 llc_sap_hold(sap);
659 write_lock_bh(&sap->sk_list.lock);
660 llc_sk(sk)->sap = sap;
661 sk_add_node(sk, &sap->sk_list.list);
662 write_unlock_bh(&sap->sk_list.lock);
666 * llc_sap_remove_socket - removes a socket from SAP
667 * @sap: SAP
668 * @sk: socket
670 * This function removes a connection from sk_list.list of a SAP if
671 * the connection was in this list.
673 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
675 write_lock_bh(&sap->sk_list.lock);
676 sk_del_node_init(sk);
677 write_unlock_bh(&sap->sk_list.lock);
678 llc_sap_put(sap);
682 * llc_conn_rcv - sends received pdus to the connection state machine
683 * @sk: current connection structure.
684 * @skb: received frame.
686 * Sends received pdus to the connection state machine.
688 static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
690 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
692 ev->type = LLC_CONN_EV_TYPE_PDU;
693 ev->reason = 0;
694 return llc_conn_state_process(sk, skb);
697 static struct sock *llc_create_incoming_sock(struct sock *sk,
698 struct net_device *dev,
699 struct llc_addr *saddr,
700 struct llc_addr *daddr)
702 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
703 sk->sk_prot);
704 struct llc_sock *newllc, *llc = llc_sk(sk);
706 if (!newsk)
707 goto out;
708 newllc = llc_sk(newsk);
709 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
710 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
711 newllc->dev = dev;
712 dev_hold(dev);
713 llc_sap_add_socket(llc->sap, newsk);
714 llc_sap_hold(llc->sap);
715 out:
716 return newsk;
719 void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
721 struct llc_addr saddr, daddr;
722 struct sock *sk;
724 llc_pdu_decode_sa(skb, saddr.mac);
725 llc_pdu_decode_ssap(skb, &saddr.lsap);
726 llc_pdu_decode_da(skb, daddr.mac);
727 llc_pdu_decode_dsap(skb, &daddr.lsap);
729 sk = __llc_lookup(sap, &saddr, &daddr);
730 if (!sk)
731 goto drop;
733 bh_lock_sock(sk);
735 * This has to be done here and not at the upper layer ->accept
736 * method because of the way the PROCOM state machine works:
737 * it needs to set several state variables (see, for instance,
738 * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
739 * the originator of the new connection, and this state has to be
740 * in the newly created struct sock private area. -acme
742 if (unlikely(sk->sk_state == TCP_LISTEN)) {
743 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
744 &saddr, &daddr);
745 if (!newsk)
746 goto drop_unlock;
747 skb_set_owner_r(skb, newsk);
748 } else {
750 * Can't be skb_set_owner_r, this will be done at the
751 * llc_conn_state_process function, later on, when we will use
752 * skb_queue_rcv_skb to send it to upper layers, this is
753 * another trick required to cope with how the PROCOM state
754 * machine works. -acme
756 skb->sk = sk;
758 if (!sock_owned_by_user(sk))
759 llc_conn_rcv(sk, skb);
760 else {
761 dprintk("%s: adding to backlog...\n", __func__);
762 llc_set_backlog_type(skb, LLC_PACKET);
763 sk_add_backlog(sk, skb);
765 out:
766 bh_unlock_sock(sk);
767 sock_put(sk);
768 return;
769 drop:
770 kfree_skb(skb);
771 return;
772 drop_unlock:
773 kfree_skb(skb);
774 goto out;
777 #undef LLC_REFCNT_DEBUG
778 #ifdef LLC_REFCNT_DEBUG
779 static atomic_t llc_sock_nr;
780 #endif
783 * llc_backlog_rcv - Processes rx frames and expired timers.
784 * @sk: LLC sock (p8022 connection)
785 * @skb: queued rx frame or event
787 * This function processes frames that has received and timers that has
788 * expired during sending an I pdu (refer to data_req_handler). frames
789 * queue by llc_rcv function (llc_mac.c) and timers queue by timer
790 * callback functions(llc_c_ac.c).
792 static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
794 int rc = 0;
795 struct llc_sock *llc = llc_sk(sk);
797 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
798 if (likely(llc->state > 1)) /* not closed */
799 rc = llc_conn_rcv(sk, skb);
800 else
801 goto out_kfree_skb;
802 } else if (llc_backlog_type(skb) == LLC_EVENT) {
803 /* timer expiration event */
804 if (likely(llc->state > 1)) /* not closed */
805 rc = llc_conn_state_process(sk, skb);
806 else
807 goto out_kfree_skb;
808 } else {
809 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
810 goto out_kfree_skb;
812 out:
813 return rc;
814 out_kfree_skb:
815 kfree_skb(skb);
816 goto out;
820 * llc_sk_init - Initializes a socket with default llc values.
821 * @sk: socket to initialize.
823 * Initializes a socket with default llc values.
825 static void llc_sk_init(struct sock* sk)
827 struct llc_sock *llc = llc_sk(sk);
829 llc->state = LLC_CONN_STATE_ADM;
830 llc->inc_cntr = llc->dec_cntr = 2;
831 llc->dec_step = llc->connect_step = 1;
833 setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
834 (unsigned long)sk);
835 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
837 setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
838 (unsigned long)sk);
839 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
841 setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
842 (unsigned long)sk);
843 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
845 setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
846 (unsigned long)sk);
847 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
849 llc->n2 = 2; /* max retransmit */
850 llc->k = 2; /* tx win size, will adjust dynam */
851 llc->rw = 128; /* rx win size (opt and equal to
852 * tx_win of remote LLC) */
853 skb_queue_head_init(&llc->pdu_unack_q);
854 sk->sk_backlog_rcv = llc_backlog_rcv;
858 * llc_sk_alloc - Allocates LLC sock
859 * @family: upper layer protocol family
860 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
862 * Allocates a LLC sock and initializes it. Returns the new LLC sock
863 * or %NULL if there's no memory available for one
865 struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
867 struct sock *sk = sk_alloc(net, family, priority, prot);
869 if (!sk)
870 goto out;
871 llc_sk_init(sk);
872 sock_init_data(NULL, sk);
873 #ifdef LLC_REFCNT_DEBUG
874 atomic_inc(&llc_sock_nr);
875 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
876 __func__, atomic_read(&llc_sock_nr));
877 #endif
878 out:
879 return sk;
883 * llc_sk_free - Frees a LLC socket
884 * @sk - socket to free
886 * Frees a LLC socket
888 void llc_sk_free(struct sock *sk)
890 struct llc_sock *llc = llc_sk(sk);
892 llc->state = LLC_CONN_OUT_OF_SVC;
893 /* Stop all (possibly) running timers */
894 llc_conn_ac_stop_all_timers(sk, NULL);
895 #ifdef DEBUG_LLC_CONN_ALLOC
896 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
897 skb_queue_len(&llc->pdu_unack_q),
898 skb_queue_len(&sk->sk_write_queue));
899 #endif
900 skb_queue_purge(&sk->sk_receive_queue);
901 skb_queue_purge(&sk->sk_write_queue);
902 skb_queue_purge(&llc->pdu_unack_q);
903 #ifdef LLC_REFCNT_DEBUG
904 if (atomic_read(&sk->sk_refcnt) != 1) {
905 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
906 sk, __func__, atomic_read(&sk->sk_refcnt));
907 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
908 atomic_read(&llc_sock_nr));
909 } else {
910 atomic_dec(&llc_sock_nr);
911 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
912 __func__, atomic_read(&llc_sock_nr));
914 #endif
915 sock_put(sk);
919 * llc_sk_reset - resets a connection
920 * @sk: LLC socket to reset
922 * Resets a connection to the out of service state. Stops its timers
923 * and frees any frames in the queues of the connection.
925 void llc_sk_reset(struct sock *sk)
927 struct llc_sock *llc = llc_sk(sk);
929 llc_conn_ac_stop_all_timers(sk, NULL);
930 skb_queue_purge(&sk->sk_write_queue);
931 skb_queue_purge(&llc->pdu_unack_q);
932 llc->remote_busy_flag = 0;
933 llc->cause_flag = 0;
934 llc->retry_count = 0;
935 llc_conn_set_p_flag(sk, 0);
936 llc->f_flag = 0;
937 llc->s_flag = 0;
938 llc->ack_pf = 0;
939 llc->first_pdu_Ns = 0;
940 llc->ack_must_be_send = 0;
941 llc->dec_step = 1;
942 llc->inc_cntr = 2;
943 llc->dec_cntr = 2;
944 llc->X = 0;
945 llc->failed_data_req = 0 ;
946 llc->last_nr = 0;