[LLC]: Add sysctl support for the LLC timeouts
[linux-3.10.git] / net / llc / llc_conn.c
1 /*
2  * llc_conn.c - Driver routines for connection component.
3  *
4  * Copyright (c) 1997 by Procom Technology, Inc.
5  *               2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6  *
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.
11  *
12  * See the GNU General Public License for more details.
13  */
14
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>
24
25 #if 0
26 #define dprintk(args...) printk(KERN_DEBUG args)
27 #else
28 #define dprintk(args...)
29 #endif
30
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);
39
40 /* Offset table on connection states transition diagram */
41 static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
42
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;
47
48 /**
49  *      llc_conn_state_process - sends event to connection state machine
50  *      @sk: connection
51  *      @skb: occurred event
52  *
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.
57  */
58 int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
59 {
60         int rc;
61         struct llc_sock *llc = llc_sk(sk);
62         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
63
64         /*
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.
68          */
69         skb_get(skb);
70         ev->ind_prim = ev->cfm_prim = 0;
71         rc = llc_conn_service(sk, skb); /* sending event to state machine */
72         if (unlikely(rc != 0)) {
73                 printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
74                 goto out_kfree_skb;
75         }
76
77         if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
78                 /* indicate or confirm not required */
79                 /* XXX this is not very pretty, perhaps we should store
80                  * XXX indicate/confirm-needed state in the llc_conn_state_ev
81                  * XXX control block of the SKB instead? -DaveM
82                  */
83                 if (!skb->next)
84                         goto out_kfree_skb;
85                 goto out_skb_put;
86         }
87
88         if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
89                 skb_get(skb);
90
91         switch (ev->ind_prim) {
92         case LLC_DATA_PRIM:
93                 llc_save_primitive(skb, LLC_DATA_PRIM);
94                 if (sock_queue_rcv_skb(sk, skb)) {
95                         /*
96                          * shouldn't happen
97                          */
98                         printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
99                                __FUNCTION__);
100                         kfree_skb(skb);
101                 }
102                 break;
103         case LLC_CONN_PRIM: {
104                 struct sock *parent = skb->sk;
105
106                 skb->sk = sk;
107                 skb_queue_tail(&parent->sk_receive_queue, skb);
108                 sk->sk_state_change(parent);
109         }
110                 break;
111         case LLC_DISC_PRIM:
112                 sock_hold(sk);
113                 if (sk->sk_type == SOCK_STREAM &&
114                     sk->sk_state == TCP_ESTABLISHED) {
115                         sk->sk_shutdown       = SHUTDOWN_MASK;
116                         sk->sk_socket->state  = SS_UNCONNECTED;
117                         sk->sk_state          = TCP_CLOSE;
118                         if (!sock_flag(sk, SOCK_DEAD)) {
119                                 sk->sk_state_change(sk);
120                                 sock_set_flag(sk, SOCK_DEAD);
121                         }
122                 }
123                 kfree_skb(skb);
124                 sock_put(sk);
125                 break;
126         case LLC_RESET_PRIM:
127                 /*
128                  * FIXME:
129                  * RESET is not being notified to upper layers for now
130                  */
131                 printk(KERN_INFO "%s: received a reset ind!\n", __FUNCTION__);
132                 kfree_skb(skb);
133                 break;
134         default:
135                 if (ev->ind_prim) {
136                         printk(KERN_INFO "%s: received unknown %d prim!\n",
137                                 __FUNCTION__, ev->ind_prim);
138                         kfree_skb(skb);
139                 }
140                 /* No indication */
141                 break;
142         }
143
144         switch (ev->cfm_prim) {
145         case LLC_DATA_PRIM:
146                 if (!llc_data_accept_state(llc->state))
147                         sk->sk_write_space(sk);
148                 else
149                         rc = llc->failed_data_req = 1;
150                 break;
151         case LLC_CONN_PRIM:
152                 if (sk->sk_type == SOCK_STREAM &&
153                     sk->sk_state == TCP_SYN_SENT) {
154                         if (ev->status) {
155                                 sk->sk_socket->state = SS_UNCONNECTED;
156                                 sk->sk_state         = TCP_CLOSE;
157                         } else {
158                                 sk->sk_socket->state = SS_CONNECTED;
159                                 sk->sk_state         = TCP_ESTABLISHED;
160                         }
161                         sk->sk_state_change(sk);
162                 }
163                 break;
164         case LLC_DISC_PRIM:
165                 sock_hold(sk);
166                 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
167                         sk->sk_socket->state = SS_UNCONNECTED;
168                         sk->sk_state         = TCP_CLOSE;
169                         sk->sk_state_change(sk);
170                 }
171                 sock_put(sk);
172                 break;
173         case LLC_RESET_PRIM:
174                 /*
175                  * FIXME:
176                  * RESET is not being notified to upper layers for now
177                  */
178                 printk(KERN_INFO "%s: received a reset conf!\n", __FUNCTION__);
179                 break;
180         default:
181                 if (ev->cfm_prim) {
182                         printk(KERN_INFO "%s: received unknown %d prim!\n",
183                                         __FUNCTION__, ev->cfm_prim);
184                         break;
185                 }
186                 goto out_skb_put; /* No confirmation */
187         }
188 out_kfree_skb:
189         kfree_skb(skb);
190 out_skb_put:
191         kfree_skb(skb);
192         return rc;
193 }
194
195 void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
196 {
197         /* queue PDU to send to MAC layer */
198         skb_queue_tail(&sk->sk_write_queue, skb);
199         llc_conn_send_pdus(sk);
200 }
201
202 /**
203  *      llc_conn_rtn_pdu - sends received data pdu to upper layer
204  *      @sk: Active connection
205  *      @skb: Received data frame
206  *
207  *      Sends received data pdu to upper layer (by using indicate function).
208  *      Prepares service parameters (prim and prim_data). calling indication
209  *      function will be done in llc_conn_state_process.
210  */
211 void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
212 {
213         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
214
215         ev->ind_prim = LLC_DATA_PRIM;
216 }
217
218 /**
219  *      llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
220  *      @sk: active connection
221  *      @nr: NR
222  *      @first_p_bit: p_bit value of first pdu
223  *
224  *      Resend all unacknowledged I PDUs, starting with the NR; send first as
225  *      command PDU with P bit equal first_p_bit; if more than one send
226  *      subsequent as command PDUs with P bit equal zero (0).
227  */
228 void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
229 {
230         struct sk_buff *skb;
231         struct llc_pdu_sn *pdu;
232         u16 nbr_unack_pdus;
233         struct llc_sock *llc;
234         u8 howmany_resend = 0;
235
236         llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
237         if (!nbr_unack_pdus)
238                 goto out;
239         /*
240          * Process unack PDUs only if unack queue is not empty; remove
241          * appropriate PDUs, fix them up, and put them on mac_pdu_q.
242          */
243         llc = llc_sk(sk);
244
245         while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
246                 pdu = llc_pdu_sn_hdr(skb);
247                 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
248                 llc_pdu_set_pf_bit(skb, first_p_bit);
249                 skb_queue_tail(&sk->sk_write_queue, skb);
250                 first_p_bit = 0;
251                 llc->vS = LLC_I_GET_NS(pdu);
252                 howmany_resend++;
253         }
254         if (howmany_resend > 0)
255                 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
256         /* any PDUs to re-send are queued up; start sending to MAC */
257         llc_conn_send_pdus(sk);
258 out:;
259 }
260
261 /**
262  *      llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
263  *      @sk: active connection.
264  *      @nr: NR
265  *      @first_f_bit: f_bit value of first pdu.
266  *
267  *      Resend all unacknowledged I PDUs, starting with the NR; send first as
268  *      response PDU with F bit equal first_f_bit; if more than one send
269  *      subsequent as response PDUs with F bit equal zero (0).
270  */
271 void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
272 {
273         struct sk_buff *skb;
274         u16 nbr_unack_pdus;
275         struct llc_sock *llc = llc_sk(sk);
276         u8 howmany_resend = 0;
277
278         llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
279         if (!nbr_unack_pdus)
280                 goto out;
281         /*
282          * Process unack PDUs only if unack queue is not empty; remove
283          * appropriate PDUs, fix them up, and put them on mac_pdu_q
284          */
285         while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
286                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
287
288                 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
289                 llc_pdu_set_pf_bit(skb, first_f_bit);
290                 skb_queue_tail(&sk->sk_write_queue, skb);
291                 first_f_bit = 0;
292                 llc->vS = LLC_I_GET_NS(pdu);
293                 howmany_resend++;
294         }
295         if (howmany_resend > 0)
296                 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
297         /* any PDUs to re-send are queued up; start sending to MAC */
298         llc_conn_send_pdus(sk);
299 out:;
300 }
301
302 /**
303  *      llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
304  *      @sk: active connection
305  *      nr: NR
306  *      how_many_unacked: size of pdu_unack_q after removing acked pdus
307  *
308  *      Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
309  *      the number of pdus that removed from queue.
310  */
311 int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
312 {
313         int pdu_pos, i;
314         struct sk_buff *skb;
315         struct llc_pdu_sn *pdu;
316         int nbr_acked = 0;
317         struct llc_sock *llc = llc_sk(sk);
318         int q_len = skb_queue_len(&llc->pdu_unack_q);
319
320         if (!q_len)
321                 goto out;
322         skb = skb_peek(&llc->pdu_unack_q);
323         pdu = llc_pdu_sn_hdr(skb);
324
325         /* finding position of last acked pdu in queue */
326         pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
327                         (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
328
329         for (i = 0; i < pdu_pos && i < q_len; i++) {
330                 skb = skb_dequeue(&llc->pdu_unack_q);
331                 if (skb)
332                         kfree_skb(skb);
333                 nbr_acked++;
334         }
335 out:
336         *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
337         return nbr_acked;
338 }
339
340 /**
341  *      llc_conn_send_pdus - Sends queued PDUs
342  *      @sk: active connection
343  *
344  *      Sends queued pdus to MAC layer for transmission.
345  */
346 static void llc_conn_send_pdus(struct sock *sk)
347 {
348         struct sk_buff *skb;
349
350         while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
351                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
352
353                 if (LLC_PDU_TYPE_IS_I(pdu) &&
354                     !(skb->dev->flags & IFF_LOOPBACK)) {
355                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
356
357                         skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
358                         if (!skb2)
359                                 break;
360                         skb = skb2;
361                 }
362                 dev_queue_xmit(skb);
363         }
364 }
365
366 /**
367  *      llc_conn_service - finds transition and changes state of connection
368  *      @sk: connection
369  *      @skb: happened event
370  *
371  *      This function finds transition that matches with happened event, then
372  *      executes related actions and finally changes state of connection.
373  *      Returns 0 for success, 1 for failure.
374  */
375 static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
376 {
377         int rc = 1;
378         struct llc_sock *llc = llc_sk(sk);
379         struct llc_conn_state_trans *trans;
380
381         if (llc->state > NBR_CONN_STATES)
382                 goto out;
383         rc = 0;
384         trans = llc_qualify_conn_ev(sk, skb);
385         if (trans) {
386                 rc = llc_exec_conn_trans_actions(sk, trans, skb);
387                 if (!rc && trans->next_state != NO_STATE_CHANGE) {
388                         llc->state = trans->next_state;
389                         if (!llc_data_accept_state(llc->state))
390                                 sk->sk_state_change(sk);
391                 }
392         }
393 out:
394         return rc;
395 }
396
397 /**
398  *      llc_qualify_conn_ev - finds transition for event
399  *      @sk: connection
400  *      @skb: happened event
401  *
402  *      This function finds transition that matches with happened event.
403  *      Returns pointer to found transition on success, %NULL otherwise.
404  */
405 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
406                                                         struct sk_buff *skb)
407 {
408         struct llc_conn_state_trans **next_trans;
409         llc_conn_ev_qfyr_t *next_qualifier;
410         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
411         struct llc_sock *llc = llc_sk(sk);
412         struct llc_conn_state *curr_state =
413                                         &llc_conn_state_table[llc->state - 1];
414
415         /* search thru events for this state until
416          * list exhausted or until no more
417          */
418         for (next_trans = curr_state->transitions +
419                 llc_find_offset(llc->state - 1, ev->type);
420              (*next_trans)->ev; next_trans++) {
421                 if (!((*next_trans)->ev)(sk, skb)) {
422                         /* got POSSIBLE event match; the event may require
423                          * qualification based on the values of a number of
424                          * state flags; if all qualifications are met (i.e.,
425                          * if all qualifying functions return success, or 0,
426                          * then this is THE event we're looking for
427                          */
428                         for (next_qualifier = (*next_trans)->ev_qualifiers;
429                              next_qualifier && *next_qualifier &&
430                              !(*next_qualifier)(sk, skb); next_qualifier++)
431                                 /* nothing */;
432                         if (!next_qualifier || !*next_qualifier)
433                                 /* all qualifiers executed successfully; this is
434                                  * our transition; return it so we can perform
435                                  * the associated actions & change the state
436                                  */
437                                 return *next_trans;
438                 }
439         }
440         return NULL;
441 }
442
443 /**
444  *      llc_exec_conn_trans_actions - executes related actions
445  *      @sk: connection
446  *      @trans: transition that it's actions must be performed
447  *      @skb: event
448  *
449  *      Executes actions that is related to happened event. Returns 0 for
450  *      success, 1 to indicate failure of at least one action.
451  */
452 static int llc_exec_conn_trans_actions(struct sock *sk,
453                                        struct llc_conn_state_trans *trans,
454                                        struct sk_buff *skb)
455 {
456         int rc = 0;
457         llc_conn_action_t *next_action;
458
459         for (next_action = trans->ev_actions;
460              next_action && *next_action; next_action++) {
461                 int rc2 = (*next_action)(sk, skb);
462
463                 if (rc2 == 2) {
464                         rc = rc2;
465                         break;
466                 } else if (rc2)
467                         rc = 1;
468         }
469         return rc;
470 }
471
472 /**
473  *      llc_lookup_established - Finds connection for the remote/local sap/mac
474  *      @sap: SAP
475  *      @daddr: address of remote LLC (MAC + SAP)
476  *      @laddr: address of local LLC (MAC + SAP)
477  *
478  *      Search connection list of the SAP and finds connection using the remote
479  *      mac, remote sap, local mac, and local sap. Returns pointer for
480  *      connection found, %NULL otherwise.
481  */
482 struct sock *llc_lookup_established(struct llc_sap *sap, struct llc_addr *daddr,
483                                     struct llc_addr *laddr)
484 {
485         struct sock *rc;
486         struct hlist_node *node;
487
488         read_lock_bh(&sap->sk_list.lock);
489         sk_for_each(rc, node, &sap->sk_list.list) {
490                 struct llc_sock *llc = llc_sk(rc);
491
492                 if (llc->laddr.lsap == laddr->lsap &&
493                     llc->daddr.lsap == daddr->lsap &&
494                     llc_mac_match(llc->laddr.mac, laddr->mac) &&
495                     llc_mac_match(llc->daddr.mac, daddr->mac)) {
496                         sock_hold(rc);
497                         goto found;
498                 }
499         }
500         rc = NULL;
501 found:
502         read_unlock_bh(&sap->sk_list.lock);
503         return rc;
504 }
505
506 /**
507  *      llc_lookup_listener - Finds listener for local MAC + SAP
508  *      @sap: SAP
509  *      @laddr: address of local LLC (MAC + SAP)
510  *
511  *      Search connection list of the SAP and finds connection listening on
512  *      local mac, and local sap. Returns pointer for parent socket found,
513  *      %NULL otherwise.
514  */
515 static struct sock *llc_lookup_listener(struct llc_sap *sap,
516                                         struct llc_addr *laddr)
517 {
518         struct sock *rc;
519         struct hlist_node *node;
520
521         read_lock_bh(&sap->sk_list.lock);
522         sk_for_each(rc, node, &sap->sk_list.list) {
523                 struct llc_sock *llc = llc_sk(rc);
524
525                 if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
526                     llc->laddr.lsap == laddr->lsap &&
527                     (llc_mac_match(llc->laddr.mac, laddr->mac) ||
528                      llc_mac_null(llc->laddr.mac))) {
529                         sock_hold(rc);
530                         goto found;
531                 }
532         }
533         rc = NULL;
534 found:
535         read_unlock_bh(&sap->sk_list.lock);
536         return rc;
537 }
538
539 /**
540  *      llc_data_accept_state - designates if in this state data can be sent.
541  *      @state: state of connection.
542  *
543  *      Returns 0 if data can be sent, 1 otherwise.
544  */
545 u8 llc_data_accept_state(u8 state)
546 {
547         return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
548                state != LLC_CONN_STATE_REJ;
549 }
550
551 /**
552  *      llc_find_next_offset - finds offset for next category of transitions
553  *      @state: state table.
554  *      @offset: start offset.
555  *
556  *      Finds offset of next category of transitions in transition table.
557  *      Returns the start index of next category.
558  */
559 static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
560 {
561         u16 cnt = 0;
562         struct llc_conn_state_trans **next_trans;
563
564         for (next_trans = state->transitions + offset;
565              (*next_trans)->ev; next_trans++)
566                 ++cnt;
567         return cnt;
568 }
569
570 /**
571  *      llc_build_offset_table - builds offset table of connection
572  *
573  *      Fills offset table of connection state transition table
574  *      (llc_offset_table).
575  */
576 void __init llc_build_offset_table(void)
577 {
578         struct llc_conn_state *curr_state;
579         int state, ev_type, next_offset;
580
581         for (state = 0; state < NBR_CONN_STATES; state++) {
582                 curr_state = &llc_conn_state_table[state];
583                 next_offset = 0;
584                 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
585                         llc_offset_table[state][ev_type] = next_offset;
586                         next_offset += llc_find_next_offset(curr_state,
587                                                             next_offset) + 1;
588                 }
589         }
590 }
591
592 /**
593  *      llc_find_offset - finds start offset of category of transitions
594  *      @state: state of connection
595  *      @ev_type: type of happened event
596  *
597  *      Finds start offset of desired category of transitions. Returns the
598  *      desired start offset.
599  */
600 static int llc_find_offset(int state, int ev_type)
601 {
602         int rc = 0;
603         /* at this stage, llc_offset_table[..][2] is not important. it is for
604          * init_pf_cycle and I don't know what is it.
605          */
606         switch (ev_type) {
607         case LLC_CONN_EV_TYPE_PRIM:
608                 rc = llc_offset_table[state][0]; break;
609         case LLC_CONN_EV_TYPE_PDU:
610                 rc = llc_offset_table[state][4]; break;
611         case LLC_CONN_EV_TYPE_SIMPLE:
612                 rc = llc_offset_table[state][1]; break;
613         case LLC_CONN_EV_TYPE_P_TMR:
614         case LLC_CONN_EV_TYPE_ACK_TMR:
615         case LLC_CONN_EV_TYPE_REJ_TMR:
616         case LLC_CONN_EV_TYPE_BUSY_TMR:
617                 rc = llc_offset_table[state][3]; break;
618         }
619         return rc;
620 }
621
622 /**
623  *      llc_sap_add_socket - adds a socket to a SAP
624  *      @sap: SAP
625  *      @sk: socket
626  *
627  *      This function adds a socket to sk_list of a SAP.
628  */
629 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
630 {
631         write_lock_bh(&sap->sk_list.lock);
632         llc_sk(sk)->sap = sap;
633         sk_add_node(sk, &sap->sk_list.list);
634         write_unlock_bh(&sap->sk_list.lock);
635 }
636
637 /**
638  *      llc_sap_remove_socket - removes a socket from SAP
639  *      @sap: SAP
640  *      @sk: socket
641  *
642  *      This function removes a connection from sk_list.list of a SAP if
643  *      the connection was in this list.
644  */
645 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
646 {
647         write_lock_bh(&sap->sk_list.lock);
648         sk_del_node_init(sk);
649         write_unlock_bh(&sap->sk_list.lock);
650 }
651
652 /**
653  *      llc_conn_rcv - sends received pdus to the connection state machine
654  *      @sk: current connection structure.
655  *      @skb: received frame.
656  *
657  *      Sends received pdus to the connection state machine.
658  */
659 static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
660 {
661         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
662         struct llc_sock *llc = llc_sk(sk);
663
664         if (!llc->dev)
665                 llc->dev = skb->dev;
666         ev->type   = LLC_CONN_EV_TYPE_PDU;
667         ev->reason = 0;
668         return llc_conn_state_process(sk, skb);
669 }
670
671 void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
672 {
673         struct llc_addr saddr, daddr;
674         struct sock *sk;
675
676         llc_pdu_decode_sa(skb, saddr.mac);
677         llc_pdu_decode_ssap(skb, &saddr.lsap);
678         llc_pdu_decode_da(skb, daddr.mac);
679         llc_pdu_decode_dsap(skb, &daddr.lsap);
680
681         sk = llc_lookup_established(sap, &saddr, &daddr);
682         if (!sk) {
683                 /*
684                  * Didn't find an active connection; verify if there
685                  * is a listening socket for this llc addr
686                  */
687                 struct llc_sock *llc;
688                 struct sock *parent = llc_lookup_listener(sap, &daddr);
689
690                 if (!parent) {
691                         dprintk("llc_lookup_listener failed!\n");
692                         goto drop;
693                 }
694
695                 sk = llc_sk_alloc(parent->sk_family, GFP_ATOMIC, parent->sk_prot);
696                 if (!sk) {
697                         sock_put(parent);
698                         goto drop;
699                 }
700                 llc = llc_sk(sk);
701                 memcpy(&llc->laddr, &daddr, sizeof(llc->laddr));
702                 memcpy(&llc->daddr, &saddr, sizeof(llc->daddr));
703                 llc_sap_add_socket(sap, sk);
704                 sock_hold(sk);
705                 sock_put(parent);
706                 skb->sk = parent;
707         } else
708                 skb->sk = sk;
709         bh_lock_sock(sk);
710         if (!sock_owned_by_user(sk))
711                 llc_conn_rcv(sk, skb);
712         else {
713                 dprintk("%s: adding to backlog...\n", __FUNCTION__);
714                 llc_set_backlog_type(skb, LLC_PACKET);
715                 sk_add_backlog(sk, skb);
716         }
717         bh_unlock_sock(sk);
718         sock_put(sk);
719         return;
720 drop:
721         kfree_skb(skb);
722 }
723
724 #undef LLC_REFCNT_DEBUG
725 #ifdef LLC_REFCNT_DEBUG
726 static atomic_t llc_sock_nr;
727 #endif
728
729 /**
730  *      llc_release_sockets - releases all sockets in a sap
731  *      @sap: sap to release its sockets
732  *
733  *      Releases all connections of a sap. Returns 0 if all actions complete
734  *      successfully, nonzero otherwise
735  */
736 int llc_release_sockets(struct llc_sap *sap)
737 {
738         int rc = 0;
739         struct sock *sk;
740         struct hlist_node *node;
741
742         write_lock_bh(&sap->sk_list.lock);
743
744         sk_for_each(sk, node, &sap->sk_list.list) {
745                 llc_sk(sk)->state = LLC_CONN_STATE_TEMP;
746
747                 if (llc_send_disc(sk))
748                         rc = 1;
749         }
750
751         write_unlock_bh(&sap->sk_list.lock);
752         return rc;
753 }
754
755 /**
756  *      llc_backlog_rcv - Processes rx frames and expired timers.
757  *      @sk: LLC sock (p8022 connection)
758  *      @skb: queued rx frame or event
759  *
760  *      This function processes frames that has received and timers that has
761  *      expired during sending an I pdu (refer to data_req_handler).  frames
762  *      queue by llc_rcv function (llc_mac.c) and timers queue by timer
763  *      callback functions(llc_c_ac.c).
764  */
765 static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
766 {
767         int rc = 0;
768         struct llc_sock *llc = llc_sk(sk);
769
770         if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
771                 if (likely(llc->state > 1)) /* not closed */
772                         rc = llc_conn_rcv(sk, skb);
773                 else
774                         goto out_kfree_skb;
775         } else if (llc_backlog_type(skb) == LLC_EVENT) {
776                 /* timer expiration event */
777                 if (likely(llc->state > 1))  /* not closed */
778                         rc = llc_conn_state_process(sk, skb);
779                 else
780                         goto out_kfree_skb;
781         } else {
782                 printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
783                 goto out_kfree_skb;
784         }
785 out:
786         return rc;
787 out_kfree_skb:
788         kfree_skb(skb);
789         goto out;
790 }
791
792 /**
793  *     llc_sk_init - Initializes a socket with default llc values.
794  *     @sk: socket to initialize.
795  *
796  *     Initializes a socket with default llc values.
797  */
798 static void llc_sk_init(struct sock* sk)
799 {
800         struct llc_sock *llc = llc_sk(sk);
801
802         llc->state    = LLC_CONN_STATE_ADM;
803         llc->inc_cntr = llc->dec_cntr = 2;
804         llc->dec_step = llc->connect_step = 1;
805
806         init_timer(&llc->ack_timer.timer);
807         llc->ack_timer.expire         = sysctl_llc2_ack_timeout;
808         llc->ack_timer.timer.data     = (unsigned long)sk;
809         llc->ack_timer.timer.function = llc_conn_ack_tmr_cb;
810
811         init_timer(&llc->pf_cycle_timer.timer);
812         llc->pf_cycle_timer.expire         = sysctl_llc2_p_timeout;
813         llc->pf_cycle_timer.timer.data     = (unsigned long)sk;
814         llc->pf_cycle_timer.timer.function = llc_conn_pf_cycle_tmr_cb;
815
816         init_timer(&llc->rej_sent_timer.timer);
817         llc->rej_sent_timer.expire         = sysctl_llc2_rej_timeout;
818         llc->rej_sent_timer.timer.data     = (unsigned long)sk;
819         llc->rej_sent_timer.timer.function = llc_conn_rej_tmr_cb;
820
821         init_timer(&llc->busy_state_timer.timer);
822         llc->busy_state_timer.expire         = sysctl_llc2_busy_timeout;
823         llc->busy_state_timer.timer.data     = (unsigned long)sk;
824         llc->busy_state_timer.timer.function = llc_conn_busy_tmr_cb;
825
826         llc->n2 = 2;   /* max retransmit */
827         llc->k  = 2;   /* tx win size, will adjust dynam */
828         llc->rw = 128; /* rx win size (opt and equal to
829                         * tx_win of remote LLC) */
830         skb_queue_head_init(&llc->pdu_unack_q);
831         sk->sk_backlog_rcv = llc_backlog_rcv;
832 }
833
834 /**
835  *      llc_sk_alloc - Allocates LLC sock
836  *      @family: upper layer protocol family
837  *      @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
838  *
839  *      Allocates a LLC sock and initializes it. Returns the new LLC sock
840  *      or %NULL if there's no memory available for one
841  */
842 struct sock *llc_sk_alloc(int family, int priority, struct proto *prot)
843 {
844         struct sock *sk = sk_alloc(family, priority, prot, 1);
845
846         if (!sk)
847                 goto out;
848         llc_sk_init(sk);
849         sock_init_data(NULL, sk);
850 #ifdef LLC_REFCNT_DEBUG
851         atomic_inc(&llc_sock_nr);
852         printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
853                 __FUNCTION__, atomic_read(&llc_sock_nr));
854 #endif
855 out:
856         return sk;
857 }
858
859 /**
860  *      llc_sk_free - Frees a LLC socket
861  *      @sk - socket to free
862  *
863  *      Frees a LLC socket
864  */
865 void llc_sk_free(struct sock *sk)
866 {
867         struct llc_sock *llc = llc_sk(sk);
868
869         llc->state = LLC_CONN_OUT_OF_SVC;
870         /* Stop all (possibly) running timers */
871         llc_conn_ac_stop_all_timers(sk, NULL);
872 #ifdef DEBUG_LLC_CONN_ALLOC
873         printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
874                 skb_queue_len(&llc->pdu_unack_q),
875                 skb_queue_len(&sk->sk_write_queue));
876 #endif
877         skb_queue_purge(&sk->sk_receive_queue);
878         skb_queue_purge(&sk->sk_write_queue);
879         skb_queue_purge(&llc->pdu_unack_q);
880 #ifdef LLC_REFCNT_DEBUG
881         if (atomic_read(&sk->sk_refcnt) != 1) {
882                 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
883                         sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
884                 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
885                         atomic_read(&llc_sock_nr));
886         } else {
887                 atomic_dec(&llc_sock_nr);
888                 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
889                         __FUNCTION__, atomic_read(&llc_sock_nr));
890         }
891 #endif
892         sock_put(sk);
893 }
894
895 /**
896  *      llc_sk_reset - resets a connection
897  *      @sk: LLC socket to reset
898  *
899  *      Resets a connection to the out of service state. Stops its timers
900  *      and frees any frames in the queues of the connection.
901  */
902 void llc_sk_reset(struct sock *sk)
903 {
904         struct llc_sock *llc = llc_sk(sk);
905
906         llc_conn_ac_stop_all_timers(sk, NULL);
907         skb_queue_purge(&sk->sk_write_queue);
908         skb_queue_purge(&llc->pdu_unack_q);
909         llc->remote_busy_flag   = 0;
910         llc->cause_flag         = 0;
911         llc->retry_count        = 0;
912         llc_conn_set_p_flag(sk, 0);
913         llc->f_flag             = 0;
914         llc->s_flag             = 0;
915         llc->ack_pf             = 0;
916         llc->first_pdu_Ns       = 0;
917         llc->ack_must_be_send   = 0;
918         llc->dec_step           = 1;
919         llc->inc_cntr           = 2;
920         llc->dec_cntr           = 2;
921         llc->X                  = 0;
922         llc->failed_data_req    = 0 ;
923         llc->last_nr            = 0;
924 }