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