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