llc: fix another potential sk_buff leak in llc_ui_sendmsg()
[platform/kernel/linux-rpi.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 <linux/slab.h>
17 #include <net/llc_sap.h>
18 #include <net/llc_conn.h>
19 #include <net/sock.h>
20 #include <net/tcp_states.h>
21 #include <net/llc_c_ev.h>
22 #include <net/llc_c_ac.h>
23 #include <net/llc_c_st.h>
24 #include <net/llc_pdu.h>
25
26 #if 0
27 #define dprintk(args...) printk(KERN_DEBUG args)
28 #else
29 #define dprintk(args...)
30 #endif
31
32 static int llc_find_offset(int state, int ev_type);
33 static void llc_conn_send_pdus(struct sock *sk);
34 static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
35 static int llc_exec_conn_trans_actions(struct sock *sk,
36                                        struct llc_conn_state_trans *trans,
37                                        struct sk_buff *ev);
38 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
39                                                         struct sk_buff *skb);
40
41 /* Offset table on connection states transition diagram */
42 static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
43
44 int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
45 int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
46 int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
47 int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
48
49 /**
50  *      llc_conn_state_process - sends event to connection state machine
51  *      @sk: connection
52  *      @skb: occurred event
53  *
54  *      Sends an event to connection state machine. After processing event
55  *      (executing it's actions and changing state), upper layer will be
56  *      indicated or confirmed, if needed. Returns 0 for success, 1 for
57  *      failure. The socket lock has to be held before calling this function.
58  *
59  *      This function always consumes a reference to the skb.
60  */
61 int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
62 {
63         int rc;
64         struct llc_sock *llc = llc_sk(skb->sk);
65         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
66
67         /*
68          * We have to hold the skb, because llc_conn_service will kfree it in
69          * the sending path and we need to look at the skb->cb, where we encode
70          * llc_conn_state_ev.
71          */
72         skb_get(skb);
73         ev->ind_prim = ev->cfm_prim = 0;
74         /*
75          * Send event to state machine
76          */
77         rc = llc_conn_service(skb->sk, skb);
78         if (unlikely(rc != 0)) {
79                 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
80                 goto out_kfree_skb;
81         }
82
83         if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
84                 /* indicate or confirm not required */
85                 if (!skb->next)
86                         goto out_kfree_skb;
87                 goto out_skb_put;
88         }
89
90         if (unlikely(ev->ind_prim && ev->cfm_prim)) /* Paranoia */
91                 skb_get(skb);
92
93         switch (ev->ind_prim) {
94         case LLC_DATA_PRIM:
95                 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
96                 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
97                         /*
98                          * shouldn't happen
99                          */
100                         printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
101                                __func__);
102                         kfree_skb(skb);
103                 }
104                 break;
105         case LLC_CONN_PRIM:
106                 /*
107                  * Can't be sock_queue_rcv_skb, because we have to leave the
108                  * skb->sk pointing to the newly created struct sock in
109                  * llc_conn_handler. -acme
110                  */
111                 skb_queue_tail(&sk->sk_receive_queue, skb);
112                 sk->sk_state_change(sk);
113                 break;
114         case LLC_DISC_PRIM:
115                 sock_hold(sk);
116                 if (sk->sk_type == SOCK_STREAM &&
117                     sk->sk_state == TCP_ESTABLISHED) {
118                         sk->sk_shutdown       = SHUTDOWN_MASK;
119                         sk->sk_socket->state  = SS_UNCONNECTED;
120                         sk->sk_state          = TCP_CLOSE;
121                         if (!sock_flag(sk, SOCK_DEAD)) {
122                                 sock_set_flag(sk, SOCK_DEAD);
123                                 sk->sk_state_change(sk);
124                         }
125                 }
126                 kfree_skb(skb);
127                 sock_put(sk);
128                 break;
129         case LLC_RESET_PRIM:
130                 /*
131                  * FIXME:
132                  * RESET is not being notified to upper layers for now
133                  */
134                 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
135                 kfree_skb(skb);
136                 break;
137         default:
138                 if (ev->ind_prim) {
139                         printk(KERN_INFO "%s: received unknown %d prim!\n",
140                                 __func__, ev->ind_prim);
141                         kfree_skb(skb);
142                 }
143                 /* No indication */
144                 break;
145         }
146
147         switch (ev->cfm_prim) {
148         case LLC_DATA_PRIM:
149                 if (!llc_data_accept_state(llc->state))
150                         sk->sk_write_space(sk);
151                 else
152                         rc = llc->failed_data_req = 1;
153                 break;
154         case LLC_CONN_PRIM:
155                 if (sk->sk_type == SOCK_STREAM &&
156                     sk->sk_state == TCP_SYN_SENT) {
157                         if (ev->status) {
158                                 sk->sk_socket->state = SS_UNCONNECTED;
159                                 sk->sk_state         = TCP_CLOSE;
160                         } else {
161                                 sk->sk_socket->state = SS_CONNECTED;
162                                 sk->sk_state         = TCP_ESTABLISHED;
163                         }
164                         sk->sk_state_change(sk);
165                 }
166                 break;
167         case LLC_DISC_PRIM:
168                 sock_hold(sk);
169                 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
170                         sk->sk_socket->state = SS_UNCONNECTED;
171                         sk->sk_state         = TCP_CLOSE;
172                         sk->sk_state_change(sk);
173                 }
174                 sock_put(sk);
175                 break;
176         case LLC_RESET_PRIM:
177                 /*
178                  * FIXME:
179                  * RESET is not being notified to upper layers for now
180                  */
181                 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
182                 break;
183         default:
184                 if (ev->cfm_prim) {
185                         printk(KERN_INFO "%s: received unknown %d prim!\n",
186                                         __func__, ev->cfm_prim);
187                         break;
188                 }
189                 goto out_skb_put; /* No confirmation */
190         }
191 out_kfree_skb:
192         kfree_skb(skb);
193 out_skb_put:
194         kfree_skb(skb);
195         return rc;
196 }
197
198 void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
199 {
200         /* queue PDU to send to MAC layer */
201         skb_queue_tail(&sk->sk_write_queue, skb);
202         llc_conn_send_pdus(sk);
203 }
204
205 /**
206  *      llc_conn_rtn_pdu - sends received data pdu to upper layer
207  *      @sk: Active connection
208  *      @skb: Received data frame
209  *
210  *      Sends received data pdu to upper layer (by using indicate function).
211  *      Prepares service parameters (prim and prim_data). calling indication
212  *      function will be done in llc_conn_state_process.
213  */
214 void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
215 {
216         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
217
218         ev->ind_prim = LLC_DATA_PRIM;
219 }
220
221 /**
222  *      llc_conn_resend_i_pdu_as_cmd - resend all all unacknowledged I PDUs
223  *      @sk: active connection
224  *      @nr: NR
225  *      @first_p_bit: p_bit value of first pdu
226  *
227  *      Resend all unacknowledged I PDUs, starting with the NR; send first as
228  *      command PDU with P bit equal first_p_bit; if more than one send
229  *      subsequent as command PDUs with P bit equal zero (0).
230  */
231 void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
232 {
233         struct sk_buff *skb;
234         struct llc_pdu_sn *pdu;
235         u16 nbr_unack_pdus;
236         struct llc_sock *llc;
237         u8 howmany_resend = 0;
238
239         llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
240         if (!nbr_unack_pdus)
241                 goto out;
242         /*
243          * Process unack PDUs only if unack queue is not empty; remove
244          * appropriate PDUs, fix them up, and put them on mac_pdu_q.
245          */
246         llc = llc_sk(sk);
247
248         while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
249                 pdu = llc_pdu_sn_hdr(skb);
250                 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
251                 llc_pdu_set_pf_bit(skb, first_p_bit);
252                 skb_queue_tail(&sk->sk_write_queue, skb);
253                 first_p_bit = 0;
254                 llc->vS = LLC_I_GET_NS(pdu);
255                 howmany_resend++;
256         }
257         if (howmany_resend > 0)
258                 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
259         /* any PDUs to re-send are queued up; start sending to MAC */
260         llc_conn_send_pdus(sk);
261 out:;
262 }
263
264 /**
265  *      llc_conn_resend_i_pdu_as_rsp - Resend all unacknowledged I PDUs
266  *      @sk: active connection.
267  *      @nr: NR
268  *      @first_f_bit: f_bit value of first pdu.
269  *
270  *      Resend all unacknowledged I PDUs, starting with the NR; send first as
271  *      response PDU with F bit equal first_f_bit; if more than one send
272  *      subsequent as response PDUs with F bit equal zero (0).
273  */
274 void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
275 {
276         struct sk_buff *skb;
277         u16 nbr_unack_pdus;
278         struct llc_sock *llc = llc_sk(sk);
279         u8 howmany_resend = 0;
280
281         llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
282         if (!nbr_unack_pdus)
283                 goto out;
284         /*
285          * Process unack PDUs only if unack queue is not empty; remove
286          * appropriate PDUs, fix them up, and put them on mac_pdu_q
287          */
288         while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
289                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
290
291                 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
292                 llc_pdu_set_pf_bit(skb, first_f_bit);
293                 skb_queue_tail(&sk->sk_write_queue, skb);
294                 first_f_bit = 0;
295                 llc->vS = LLC_I_GET_NS(pdu);
296                 howmany_resend++;
297         }
298         if (howmany_resend > 0)
299                 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
300         /* any PDUs to re-send are queued up; start sending to MAC */
301         llc_conn_send_pdus(sk);
302 out:;
303 }
304
305 /**
306  *      llc_conn_remove_acked_pdus - Removes acknowledged pdus from tx queue
307  *      @sk: active connection
308  *      nr: NR
309  *      how_many_unacked: size of pdu_unack_q after removing acked pdus
310  *
311  *      Removes acknowledged pdus from transmit queue (pdu_unack_q). Returns
312  *      the number of pdus that removed from queue.
313  */
314 int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
315 {
316         int pdu_pos, i;
317         struct sk_buff *skb;
318         struct llc_pdu_sn *pdu;
319         int nbr_acked = 0;
320         struct llc_sock *llc = llc_sk(sk);
321         int q_len = skb_queue_len(&llc->pdu_unack_q);
322
323         if (!q_len)
324                 goto out;
325         skb = skb_peek(&llc->pdu_unack_q);
326         pdu = llc_pdu_sn_hdr(skb);
327
328         /* finding position of last acked pdu in queue */
329         pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
330                         (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
331
332         for (i = 0; i < pdu_pos && i < q_len; i++) {
333                 skb = skb_dequeue(&llc->pdu_unack_q);
334                 kfree_skb(skb);
335                 nbr_acked++;
336         }
337 out:
338         *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
339         return nbr_acked;
340 }
341
342 /**
343  *      llc_conn_send_pdus - Sends queued PDUs
344  *      @sk: active connection
345  *
346  *      Sends queued pdus to MAC layer for transmission.
347  */
348 static void llc_conn_send_pdus(struct sock *sk)
349 {
350         struct sk_buff *skb;
351
352         while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
353                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
354
355                 if (LLC_PDU_TYPE_IS_I(pdu) &&
356                     !(skb->dev->flags & IFF_LOOPBACK)) {
357                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
358
359                         skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
360                         if (!skb2)
361                                 break;
362                         skb = skb2;
363                 }
364                 dev_queue_xmit(skb);
365         }
366 }
367
368 /**
369  *      llc_conn_service - finds transition and changes state of connection
370  *      @sk: connection
371  *      @skb: happened event
372  *
373  *      This function finds transition that matches with happened event, then
374  *      executes related actions and finally changes state of connection.
375  *      Returns 0 for success, 1 for failure.
376  */
377 static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
378 {
379         int rc = 1;
380         struct llc_sock *llc = llc_sk(sk);
381         struct llc_conn_state_trans *trans;
382
383         if (llc->state > NBR_CONN_STATES)
384                 goto out;
385         rc = 0;
386         trans = llc_qualify_conn_ev(sk, skb);
387         if (trans) {
388                 rc = llc_exec_conn_trans_actions(sk, trans, skb);
389                 if (!rc && trans->next_state != NO_STATE_CHANGE) {
390                         llc->state = trans->next_state;
391                         if (!llc_data_accept_state(llc->state))
392                                 sk->sk_state_change(sk);
393                 }
394         }
395 out:
396         return rc;
397 }
398
399 /**
400  *      llc_qualify_conn_ev - finds transition for event
401  *      @sk: connection
402  *      @skb: happened event
403  *
404  *      This function finds transition that matches with happened event.
405  *      Returns pointer to found transition on success, %NULL otherwise.
406  */
407 static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
408                                                         struct sk_buff *skb)
409 {
410         struct llc_conn_state_trans **next_trans;
411         const llc_conn_ev_qfyr_t *next_qualifier;
412         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
413         struct llc_sock *llc = llc_sk(sk);
414         struct llc_conn_state *curr_state =
415                                         &llc_conn_state_table[llc->state - 1];
416
417         /* search thru events for this state until
418          * list exhausted or until no more
419          */
420         for (next_trans = curr_state->transitions +
421                 llc_find_offset(llc->state - 1, ev->type);
422              (*next_trans)->ev; next_trans++) {
423                 if (!((*next_trans)->ev)(sk, skb)) {
424                         /* got POSSIBLE event match; the event may require
425                          * qualification based on the values of a number of
426                          * state flags; if all qualifications are met (i.e.,
427                          * if all qualifying functions return success, or 0,
428                          * then this is THE event we're looking for
429                          */
430                         for (next_qualifier = (*next_trans)->ev_qualifiers;
431                              next_qualifier && *next_qualifier &&
432                              !(*next_qualifier)(sk, skb); next_qualifier++)
433                                 /* nothing */;
434                         if (!next_qualifier || !*next_qualifier)
435                                 /* all qualifiers executed successfully; this is
436                                  * our transition; return it so we can perform
437                                  * the associated actions & change the state
438                                  */
439                                 return *next_trans;
440                 }
441         }
442         return NULL;
443 }
444
445 /**
446  *      llc_exec_conn_trans_actions - executes related actions
447  *      @sk: connection
448  *      @trans: transition that it's actions must be performed
449  *      @skb: event
450  *
451  *      Executes actions that is related to happened event. Returns 0 for
452  *      success, 1 to indicate failure of at least one action.
453  */
454 static int llc_exec_conn_trans_actions(struct sock *sk,
455                                        struct llc_conn_state_trans *trans,
456                                        struct sk_buff *skb)
457 {
458         int rc = 0;
459         const llc_conn_action_t *next_action;
460
461         for (next_action = trans->ev_actions;
462              next_action && *next_action; next_action++) {
463                 int rc2 = (*next_action)(sk, skb);
464
465                 if (rc2 == 2) {
466                         rc = rc2;
467                         break;
468                 } else if (rc2)
469                         rc = 1;
470         }
471         return rc;
472 }
473
474 static inline bool llc_estab_match(const struct llc_sap *sap,
475                                    const struct llc_addr *daddr,
476                                    const struct llc_addr *laddr,
477                                    const struct sock *sk)
478 {
479         struct llc_sock *llc = llc_sk(sk);
480
481         return llc->laddr.lsap == laddr->lsap &&
482                 llc->daddr.lsap == daddr->lsap &&
483                 ether_addr_equal(llc->laddr.mac, laddr->mac) &&
484                 ether_addr_equal(llc->daddr.mac, daddr->mac);
485 }
486
487 /**
488  *      __llc_lookup_established - Finds connection for the remote/local sap/mac
489  *      @sap: SAP
490  *      @daddr: address of remote LLC (MAC + SAP)
491  *      @laddr: address of local LLC (MAC + SAP)
492  *
493  *      Search connection list of the SAP and finds connection using the remote
494  *      mac, remote sap, local mac, and local sap. Returns pointer for
495  *      connection found, %NULL otherwise.
496  *      Caller has to make sure local_bh is disabled.
497  */
498 static struct sock *__llc_lookup_established(struct llc_sap *sap,
499                                              struct llc_addr *daddr,
500                                              struct llc_addr *laddr)
501 {
502         struct sock *rc;
503         struct hlist_nulls_node *node;
504         int slot = llc_sk_laddr_hashfn(sap, laddr);
505         struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
506
507         rcu_read_lock();
508 again:
509         sk_nulls_for_each_rcu(rc, node, laddr_hb) {
510                 if (llc_estab_match(sap, daddr, laddr, rc)) {
511                         /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
512                         if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
513                                 goto again;
514                         if (unlikely(llc_sk(rc)->sap != sap ||
515                                      !llc_estab_match(sap, daddr, laddr, rc))) {
516                                 sock_put(rc);
517                                 continue;
518                         }
519                         goto found;
520                 }
521         }
522         rc = NULL;
523         /*
524          * if the nulls value we got at the end of this lookup is
525          * not the expected one, we must restart lookup.
526          * We probably met an item that was moved to another chain.
527          */
528         if (unlikely(get_nulls_value(node) != slot))
529                 goto again;
530 found:
531         rcu_read_unlock();
532         return rc;
533 }
534
535 struct sock *llc_lookup_established(struct llc_sap *sap,
536                                     struct llc_addr *daddr,
537                                     struct llc_addr *laddr)
538 {
539         struct sock *sk;
540
541         local_bh_disable();
542         sk = __llc_lookup_established(sap, daddr, laddr);
543         local_bh_enable();
544         return sk;
545 }
546
547 static inline bool llc_listener_match(const struct llc_sap *sap,
548                                       const struct llc_addr *laddr,
549                                       const struct sock *sk)
550 {
551         struct llc_sock *llc = llc_sk(sk);
552
553         return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
554                 llc->laddr.lsap == laddr->lsap &&
555                 ether_addr_equal(llc->laddr.mac, laddr->mac);
556 }
557
558 static struct sock *__llc_lookup_listener(struct llc_sap *sap,
559                                           struct llc_addr *laddr)
560 {
561         struct sock *rc;
562         struct hlist_nulls_node *node;
563         int slot = llc_sk_laddr_hashfn(sap, laddr);
564         struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
565
566         rcu_read_lock();
567 again:
568         sk_nulls_for_each_rcu(rc, node, laddr_hb) {
569                 if (llc_listener_match(sap, laddr, rc)) {
570                         /* Extra checks required by SLAB_TYPESAFE_BY_RCU */
571                         if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
572                                 goto again;
573                         if (unlikely(llc_sk(rc)->sap != sap ||
574                                      !llc_listener_match(sap, laddr, rc))) {
575                                 sock_put(rc);
576                                 continue;
577                         }
578                         goto found;
579                 }
580         }
581         rc = NULL;
582         /*
583          * if the nulls value we got at the end of this lookup is
584          * not the expected one, we must restart lookup.
585          * We probably met an item that was moved to another chain.
586          */
587         if (unlikely(get_nulls_value(node) != slot))
588                 goto again;
589 found:
590         rcu_read_unlock();
591         return rc;
592 }
593
594 /**
595  *      llc_lookup_listener - Finds listener for local MAC + SAP
596  *      @sap: SAP
597  *      @laddr: address of local LLC (MAC + SAP)
598  *
599  *      Search connection list of the SAP and finds connection listening on
600  *      local mac, and local sap. Returns pointer for parent socket found,
601  *      %NULL otherwise.
602  *      Caller has to make sure local_bh is disabled.
603  */
604 static struct sock *llc_lookup_listener(struct llc_sap *sap,
605                                         struct llc_addr *laddr)
606 {
607         static struct llc_addr null_addr;
608         struct sock *rc = __llc_lookup_listener(sap, laddr);
609
610         if (!rc)
611                 rc = __llc_lookup_listener(sap, &null_addr);
612
613         return rc;
614 }
615
616 static struct sock *__llc_lookup(struct llc_sap *sap,
617                                  struct llc_addr *daddr,
618                                  struct llc_addr *laddr)
619 {
620         struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
621
622         return sk ? : llc_lookup_listener(sap, laddr);
623 }
624
625 /**
626  *      llc_data_accept_state - designates if in this state data can be sent.
627  *      @state: state of connection.
628  *
629  *      Returns 0 if data can be sent, 1 otherwise.
630  */
631 u8 llc_data_accept_state(u8 state)
632 {
633         return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
634                state != LLC_CONN_STATE_REJ;
635 }
636
637 /**
638  *      llc_find_next_offset - finds offset for next category of transitions
639  *      @state: state table.
640  *      @offset: start offset.
641  *
642  *      Finds offset of next category of transitions in transition table.
643  *      Returns the start index of next category.
644  */
645 static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
646 {
647         u16 cnt = 0;
648         struct llc_conn_state_trans **next_trans;
649
650         for (next_trans = state->transitions + offset;
651              (*next_trans)->ev; next_trans++)
652                 ++cnt;
653         return cnt;
654 }
655
656 /**
657  *      llc_build_offset_table - builds offset table of connection
658  *
659  *      Fills offset table of connection state transition table
660  *      (llc_offset_table).
661  */
662 void __init llc_build_offset_table(void)
663 {
664         struct llc_conn_state *curr_state;
665         int state, ev_type, next_offset;
666
667         for (state = 0; state < NBR_CONN_STATES; state++) {
668                 curr_state = &llc_conn_state_table[state];
669                 next_offset = 0;
670                 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
671                         llc_offset_table[state][ev_type] = next_offset;
672                         next_offset += llc_find_next_offset(curr_state,
673                                                             next_offset) + 1;
674                 }
675         }
676 }
677
678 /**
679  *      llc_find_offset - finds start offset of category of transitions
680  *      @state: state of connection
681  *      @ev_type: type of happened event
682  *
683  *      Finds start offset of desired category of transitions. Returns the
684  *      desired start offset.
685  */
686 static int llc_find_offset(int state, int ev_type)
687 {
688         int rc = 0;
689         /* at this stage, llc_offset_table[..][2] is not important. it is for
690          * init_pf_cycle and I don't know what is it.
691          */
692         switch (ev_type) {
693         case LLC_CONN_EV_TYPE_PRIM:
694                 rc = llc_offset_table[state][0]; break;
695         case LLC_CONN_EV_TYPE_PDU:
696                 rc = llc_offset_table[state][4]; break;
697         case LLC_CONN_EV_TYPE_SIMPLE:
698                 rc = llc_offset_table[state][1]; break;
699         case LLC_CONN_EV_TYPE_P_TMR:
700         case LLC_CONN_EV_TYPE_ACK_TMR:
701         case LLC_CONN_EV_TYPE_REJ_TMR:
702         case LLC_CONN_EV_TYPE_BUSY_TMR:
703                 rc = llc_offset_table[state][3]; break;
704         }
705         return rc;
706 }
707
708 /**
709  *      llc_sap_add_socket - adds a socket to a SAP
710  *      @sap: SAP
711  *      @sk: socket
712  *
713  *      This function adds a socket to the hash tables of a SAP.
714  */
715 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
716 {
717         struct llc_sock *llc = llc_sk(sk);
718         struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
719         struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
720
721         llc_sap_hold(sap);
722         llc_sk(sk)->sap = sap;
723
724         spin_lock_bh(&sap->sk_lock);
725         sock_set_flag(sk, SOCK_RCU_FREE);
726         sap->sk_count++;
727         sk_nulls_add_node_rcu(sk, laddr_hb);
728         hlist_add_head(&llc->dev_hash_node, dev_hb);
729         spin_unlock_bh(&sap->sk_lock);
730 }
731
732 /**
733  *      llc_sap_remove_socket - removes a socket from SAP
734  *      @sap: SAP
735  *      @sk: socket
736  *
737  *      This function removes a connection from the hash tables of a SAP if
738  *      the connection was in this list.
739  */
740 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
741 {
742         struct llc_sock *llc = llc_sk(sk);
743
744         spin_lock_bh(&sap->sk_lock);
745         sk_nulls_del_node_init_rcu(sk);
746         hlist_del(&llc->dev_hash_node);
747         sap->sk_count--;
748         spin_unlock_bh(&sap->sk_lock);
749         llc_sap_put(sap);
750 }
751
752 /**
753  *      llc_conn_rcv - sends received pdus to the connection state machine
754  *      @sk: current connection structure.
755  *      @skb: received frame.
756  *
757  *      Sends received pdus to the connection state machine.
758  */
759 static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
760 {
761         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
762
763         ev->type   = LLC_CONN_EV_TYPE_PDU;
764         ev->reason = 0;
765         return llc_conn_state_process(sk, skb);
766 }
767
768 static struct sock *llc_create_incoming_sock(struct sock *sk,
769                                              struct net_device *dev,
770                                              struct llc_addr *saddr,
771                                              struct llc_addr *daddr)
772 {
773         struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
774                                           sk->sk_prot, 0);
775         struct llc_sock *newllc, *llc = llc_sk(sk);
776
777         if (!newsk)
778                 goto out;
779         newllc = llc_sk(newsk);
780         memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
781         memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
782         newllc->dev = dev;
783         dev_hold(dev);
784         llc_sap_add_socket(llc->sap, newsk);
785         llc_sap_hold(llc->sap);
786 out:
787         return newsk;
788 }
789
790 void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
791 {
792         struct llc_addr saddr, daddr;
793         struct sock *sk;
794
795         llc_pdu_decode_sa(skb, saddr.mac);
796         llc_pdu_decode_ssap(skb, &saddr.lsap);
797         llc_pdu_decode_da(skb, daddr.mac);
798         llc_pdu_decode_dsap(skb, &daddr.lsap);
799
800         sk = __llc_lookup(sap, &saddr, &daddr);
801         if (!sk)
802                 goto drop;
803
804         bh_lock_sock(sk);
805         /*
806          * This has to be done here and not at the upper layer ->accept
807          * method because of the way the PROCOM state machine works:
808          * it needs to set several state variables (see, for instance,
809          * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
810          * the originator of the new connection, and this state has to be
811          * in the newly created struct sock private area. -acme
812          */
813         if (unlikely(sk->sk_state == TCP_LISTEN)) {
814                 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
815                                                               &saddr, &daddr);
816                 if (!newsk)
817                         goto drop_unlock;
818                 skb_set_owner_r(skb, newsk);
819         } else {
820                 /*
821                  * Can't be skb_set_owner_r, this will be done at the
822                  * llc_conn_state_process function, later on, when we will use
823                  * skb_queue_rcv_skb to send it to upper layers, this is
824                  * another trick required to cope with how the PROCOM state
825                  * machine works. -acme
826                  */
827                 skb_orphan(skb);
828                 sock_hold(sk);
829                 skb->sk = sk;
830                 skb->destructor = sock_efree;
831         }
832         if (!sock_owned_by_user(sk))
833                 llc_conn_rcv(sk, skb);
834         else {
835                 dprintk("%s: adding to backlog...\n", __func__);
836                 llc_set_backlog_type(skb, LLC_PACKET);
837                 if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
838                         goto drop_unlock;
839         }
840 out:
841         bh_unlock_sock(sk);
842         sock_put(sk);
843         return;
844 drop:
845         kfree_skb(skb);
846         return;
847 drop_unlock:
848         kfree_skb(skb);
849         goto out;
850 }
851
852 #undef LLC_REFCNT_DEBUG
853 #ifdef LLC_REFCNT_DEBUG
854 static atomic_t llc_sock_nr;
855 #endif
856
857 /**
858  *      llc_backlog_rcv - Processes rx frames and expired timers.
859  *      @sk: LLC sock (p8022 connection)
860  *      @skb: queued rx frame or event
861  *
862  *      This function processes frames that has received and timers that has
863  *      expired during sending an I pdu (refer to data_req_handler).  frames
864  *      queue by llc_rcv function (llc_mac.c) and timers queue by timer
865  *      callback functions(llc_c_ac.c).
866  */
867 static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
868 {
869         int rc = 0;
870         struct llc_sock *llc = llc_sk(sk);
871
872         if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
873                 if (likely(llc->state > 1)) /* not closed */
874                         rc = llc_conn_rcv(sk, skb);
875                 else
876                         goto out_kfree_skb;
877         } else if (llc_backlog_type(skb) == LLC_EVENT) {
878                 /* timer expiration event */
879                 if (likely(llc->state > 1))  /* not closed */
880                         rc = llc_conn_state_process(sk, skb);
881                 else
882                         goto out_kfree_skb;
883         } else {
884                 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
885                 goto out_kfree_skb;
886         }
887 out:
888         return rc;
889 out_kfree_skb:
890         kfree_skb(skb);
891         goto out;
892 }
893
894 /**
895  *     llc_sk_init - Initializes a socket with default llc values.
896  *     @sk: socket to initialize.
897  *
898  *     Initializes a socket with default llc values.
899  */
900 static void llc_sk_init(struct sock *sk)
901 {
902         struct llc_sock *llc = llc_sk(sk);
903
904         llc->state    = LLC_CONN_STATE_ADM;
905         llc->inc_cntr = llc->dec_cntr = 2;
906         llc->dec_step = llc->connect_step = 1;
907
908         timer_setup(&llc->ack_timer.timer, llc_conn_ack_tmr_cb, 0);
909         llc->ack_timer.expire         = sysctl_llc2_ack_timeout;
910
911         timer_setup(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb, 0);
912         llc->pf_cycle_timer.expire         = sysctl_llc2_p_timeout;
913
914         timer_setup(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb, 0);
915         llc->rej_sent_timer.expire         = sysctl_llc2_rej_timeout;
916
917         timer_setup(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb, 0);
918         llc->busy_state_timer.expire         = sysctl_llc2_busy_timeout;
919
920         llc->n2 = 2;   /* max retransmit */
921         llc->k  = 2;   /* tx win size, will adjust dynam */
922         llc->rw = 128; /* rx win size (opt and equal to
923                         * tx_win of remote LLC) */
924         skb_queue_head_init(&llc->pdu_unack_q);
925         sk->sk_backlog_rcv = llc_backlog_rcv;
926 }
927
928 /**
929  *      llc_sk_alloc - Allocates LLC sock
930  *      @family: upper layer protocol family
931  *      @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
932  *
933  *      Allocates a LLC sock and initializes it. Returns the new LLC sock
934  *      or %NULL if there's no memory available for one
935  */
936 struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
937 {
938         struct sock *sk = sk_alloc(net, family, priority, prot, kern);
939
940         if (!sk)
941                 goto out;
942         llc_sk_init(sk);
943         sock_init_data(NULL, sk);
944 #ifdef LLC_REFCNT_DEBUG
945         atomic_inc(&llc_sock_nr);
946         printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
947                 __func__, atomic_read(&llc_sock_nr));
948 #endif
949 out:
950         return sk;
951 }
952
953 void llc_sk_stop_all_timers(struct sock *sk, bool sync)
954 {
955         struct llc_sock *llc = llc_sk(sk);
956
957         if (sync) {
958                 del_timer_sync(&llc->pf_cycle_timer.timer);
959                 del_timer_sync(&llc->ack_timer.timer);
960                 del_timer_sync(&llc->rej_sent_timer.timer);
961                 del_timer_sync(&llc->busy_state_timer.timer);
962         } else {
963                 del_timer(&llc->pf_cycle_timer.timer);
964                 del_timer(&llc->ack_timer.timer);
965                 del_timer(&llc->rej_sent_timer.timer);
966                 del_timer(&llc->busy_state_timer.timer);
967         }
968
969         llc->ack_must_be_send = 0;
970         llc->ack_pf = 0;
971 }
972
973 /**
974  *      llc_sk_free - Frees a LLC socket
975  *      @sk - socket to free
976  *
977  *      Frees a LLC socket
978  */
979 void llc_sk_free(struct sock *sk)
980 {
981         struct llc_sock *llc = llc_sk(sk);
982
983         llc->state = LLC_CONN_OUT_OF_SVC;
984         /* Stop all (possibly) running timers */
985         llc_sk_stop_all_timers(sk, true);
986 #ifdef DEBUG_LLC_CONN_ALLOC
987         printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
988                 skb_queue_len(&llc->pdu_unack_q),
989                 skb_queue_len(&sk->sk_write_queue));
990 #endif
991         skb_queue_purge(&sk->sk_receive_queue);
992         skb_queue_purge(&sk->sk_write_queue);
993         skb_queue_purge(&llc->pdu_unack_q);
994 #ifdef LLC_REFCNT_DEBUG
995         if (refcount_read(&sk->sk_refcnt) != 1) {
996                 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
997                         sk, __func__, refcount_read(&sk->sk_refcnt));
998                 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
999                         atomic_read(&llc_sock_nr));
1000         } else {
1001                 atomic_dec(&llc_sock_nr);
1002                 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
1003                         __func__, atomic_read(&llc_sock_nr));
1004         }
1005 #endif
1006         sock_put(sk);
1007 }
1008
1009 /**
1010  *      llc_sk_reset - resets a connection
1011  *      @sk: LLC socket to reset
1012  *
1013  *      Resets a connection to the out of service state. Stops its timers
1014  *      and frees any frames in the queues of the connection.
1015  */
1016 void llc_sk_reset(struct sock *sk)
1017 {
1018         struct llc_sock *llc = llc_sk(sk);
1019
1020         llc_conn_ac_stop_all_timers(sk, NULL);
1021         skb_queue_purge(&sk->sk_write_queue);
1022         skb_queue_purge(&llc->pdu_unack_q);
1023         llc->remote_busy_flag   = 0;
1024         llc->cause_flag         = 0;
1025         llc->retry_count        = 0;
1026         llc_conn_set_p_flag(sk, 0);
1027         llc->f_flag             = 0;
1028         llc->s_flag             = 0;
1029         llc->ack_pf             = 0;
1030         llc->first_pdu_Ns       = 0;
1031         llc->ack_must_be_send   = 0;
1032         llc->dec_step           = 1;
1033         llc->inc_cntr           = 2;
1034         llc->dec_cntr           = 2;
1035         llc->X                  = 0;
1036         llc->failed_data_req    = 0 ;
1037         llc->last_nr            = 0;
1038 }