1
2
3
4
5
6
7
8
9
10
11
12
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
32static int llc_find_offset(int state, int ev_type);
33static void llc_conn_send_pdus(struct sock *sk);
34static int llc_conn_service(struct sock *sk, struct sk_buff *skb);
35static int llc_exec_conn_trans_actions(struct sock *sk,
36 struct llc_conn_state_trans *trans,
37 struct sk_buff *ev);
38static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
39 struct sk_buff *skb);
40
41
42static int llc_offset_table[NBR_CONN_STATES][NBR_CONN_EV];
43
44int sysctl_llc2_ack_timeout = LLC2_ACK_TIME * HZ;
45int sysctl_llc2_p_timeout = LLC2_P_TIME * HZ;
46int sysctl_llc2_rej_timeout = LLC2_REJ_TIME * HZ;
47int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
48
49
50
51
52
53
54
55
56
57
58
59
60
61int 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 ev->ind_prim = ev->cfm_prim = 0;
68
69
70
71 rc = llc_conn_service(skb->sk, skb);
72 if (unlikely(rc != 0)) {
73 printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
74 goto out_skb_put;
75 }
76
77 switch (ev->ind_prim) {
78 case LLC_DATA_PRIM:
79 skb_get(skb);
80 llc_save_primitive(sk, skb, LLC_DATA_PRIM);
81 if (unlikely(sock_queue_rcv_skb(sk, skb))) {
82
83
84
85 printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
86 __func__);
87 kfree_skb(skb);
88 }
89 break;
90 case LLC_CONN_PRIM:
91
92
93
94
95
96 skb_get(skb);
97 skb_queue_tail(&sk->sk_receive_queue, skb);
98 sk->sk_state_change(sk);
99 break;
100 case LLC_DISC_PRIM:
101 sock_hold(sk);
102 if (sk->sk_type == SOCK_STREAM &&
103 sk->sk_state == TCP_ESTABLISHED) {
104 sk->sk_shutdown = SHUTDOWN_MASK;
105 sk->sk_socket->state = SS_UNCONNECTED;
106 sk->sk_state = TCP_CLOSE;
107 if (!sock_flag(sk, SOCK_DEAD)) {
108 sock_set_flag(sk, SOCK_DEAD);
109 sk->sk_state_change(sk);
110 }
111 }
112 sock_put(sk);
113 break;
114 case LLC_RESET_PRIM:
115
116
117
118
119 printk(KERN_INFO "%s: received a reset ind!\n", __func__);
120 break;
121 default:
122 if (ev->ind_prim)
123 printk(KERN_INFO "%s: received unknown %d prim!\n",
124 __func__, ev->ind_prim);
125
126 break;
127 }
128
129 switch (ev->cfm_prim) {
130 case LLC_DATA_PRIM:
131 if (!llc_data_accept_state(llc->state))
132 sk->sk_write_space(sk);
133 else
134 rc = llc->failed_data_req = 1;
135 break;
136 case LLC_CONN_PRIM:
137 if (sk->sk_type == SOCK_STREAM &&
138 sk->sk_state == TCP_SYN_SENT) {
139 if (ev->status) {
140 sk->sk_socket->state = SS_UNCONNECTED;
141 sk->sk_state = TCP_CLOSE;
142 } else {
143 sk->sk_socket->state = SS_CONNECTED;
144 sk->sk_state = TCP_ESTABLISHED;
145 }
146 sk->sk_state_change(sk);
147 }
148 break;
149 case LLC_DISC_PRIM:
150 sock_hold(sk);
151 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSING) {
152 sk->sk_socket->state = SS_UNCONNECTED;
153 sk->sk_state = TCP_CLOSE;
154 sk->sk_state_change(sk);
155 }
156 sock_put(sk);
157 break;
158 case LLC_RESET_PRIM:
159
160
161
162
163 printk(KERN_INFO "%s: received a reset conf!\n", __func__);
164 break;
165 default:
166 if (ev->cfm_prim)
167 printk(KERN_INFO "%s: received unknown %d prim!\n",
168 __func__, ev->cfm_prim);
169
170 break;
171 }
172out_skb_put:
173 kfree_skb(skb);
174 return rc;
175}
176
177void llc_conn_send_pdu(struct sock *sk, struct sk_buff *skb)
178{
179
180 skb_queue_tail(&sk->sk_write_queue, skb);
181 llc_conn_send_pdus(sk);
182}
183
184
185
186
187
188
189
190
191
192
193void llc_conn_rtn_pdu(struct sock *sk, struct sk_buff *skb)
194{
195 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
196
197 ev->ind_prim = LLC_DATA_PRIM;
198}
199
200
201
202
203
204
205
206
207
208
209
210void llc_conn_resend_i_pdu_as_cmd(struct sock *sk, u8 nr, u8 first_p_bit)
211{
212 struct sk_buff *skb;
213 struct llc_pdu_sn *pdu;
214 u16 nbr_unack_pdus;
215 struct llc_sock *llc;
216 u8 howmany_resend = 0;
217
218 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
219 if (!nbr_unack_pdus)
220 goto out;
221
222
223
224
225 llc = llc_sk(sk);
226
227 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
228 pdu = llc_pdu_sn_hdr(skb);
229 llc_pdu_set_cmd_rsp(skb, LLC_PDU_CMD);
230 llc_pdu_set_pf_bit(skb, first_p_bit);
231 skb_queue_tail(&sk->sk_write_queue, skb);
232 first_p_bit = 0;
233 llc->vS = LLC_I_GET_NS(pdu);
234 howmany_resend++;
235 }
236 if (howmany_resend > 0)
237 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
238
239 llc_conn_send_pdus(sk);
240out:;
241}
242
243
244
245
246
247
248
249
250
251
252
253void llc_conn_resend_i_pdu_as_rsp(struct sock *sk, u8 nr, u8 first_f_bit)
254{
255 struct sk_buff *skb;
256 u16 nbr_unack_pdus;
257 struct llc_sock *llc = llc_sk(sk);
258 u8 howmany_resend = 0;
259
260 llc_conn_remove_acked_pdus(sk, nr, &nbr_unack_pdus);
261 if (!nbr_unack_pdus)
262 goto out;
263
264
265
266
267 while ((skb = skb_dequeue(&llc->pdu_unack_q)) != NULL) {
268 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
269
270 llc_pdu_set_cmd_rsp(skb, LLC_PDU_RSP);
271 llc_pdu_set_pf_bit(skb, first_f_bit);
272 skb_queue_tail(&sk->sk_write_queue, skb);
273 first_f_bit = 0;
274 llc->vS = LLC_I_GET_NS(pdu);
275 howmany_resend++;
276 }
277 if (howmany_resend > 0)
278 llc->vS = (llc->vS + 1) % LLC_2_SEQ_NBR_MODULO;
279
280 llc_conn_send_pdus(sk);
281out:;
282}
283
284
285
286
287
288
289
290
291
292
293int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
294{
295 int pdu_pos, i;
296 struct sk_buff *skb;
297 struct llc_pdu_sn *pdu;
298 int nbr_acked = 0;
299 struct llc_sock *llc = llc_sk(sk);
300 int q_len = skb_queue_len(&llc->pdu_unack_q);
301
302 if (!q_len)
303 goto out;
304 skb = skb_peek(&llc->pdu_unack_q);
305 pdu = llc_pdu_sn_hdr(skb);
306
307
308 pdu_pos = ((int)LLC_2_SEQ_NBR_MODULO + (int)nr -
309 (int)LLC_I_GET_NS(pdu)) % LLC_2_SEQ_NBR_MODULO;
310
311 for (i = 0; i < pdu_pos && i < q_len; i++) {
312 skb = skb_dequeue(&llc->pdu_unack_q);
313 kfree_skb(skb);
314 nbr_acked++;
315 }
316out:
317 *how_many_unacked = skb_queue_len(&llc->pdu_unack_q);
318 return nbr_acked;
319}
320
321
322
323
324
325
326
327static void llc_conn_send_pdus(struct sock *sk)
328{
329 struct sk_buff *skb;
330
331 while ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL) {
332 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
333
334 if (LLC_PDU_TYPE_IS_I(pdu) &&
335 !(skb->dev->flags & IFF_LOOPBACK)) {
336 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
337
338 skb_queue_tail(&llc_sk(sk)->pdu_unack_q, skb);
339 if (!skb2)
340 break;
341 skb = skb2;
342 }
343 dev_queue_xmit(skb);
344 }
345}
346
347
348
349
350
351
352
353
354
355
356static int llc_conn_service(struct sock *sk, struct sk_buff *skb)
357{
358 int rc = 1;
359 struct llc_sock *llc = llc_sk(sk);
360 struct llc_conn_state_trans *trans;
361
362 if (llc->state > NBR_CONN_STATES)
363 goto out;
364 rc = 0;
365 trans = llc_qualify_conn_ev(sk, skb);
366 if (trans) {
367 rc = llc_exec_conn_trans_actions(sk, trans, skb);
368 if (!rc && trans->next_state != NO_STATE_CHANGE) {
369 llc->state = trans->next_state;
370 if (!llc_data_accept_state(llc->state))
371 sk->sk_state_change(sk);
372 }
373 }
374out:
375 return rc;
376}
377
378
379
380
381
382
383
384
385
386static struct llc_conn_state_trans *llc_qualify_conn_ev(struct sock *sk,
387 struct sk_buff *skb)
388{
389 struct llc_conn_state_trans **next_trans;
390 const llc_conn_ev_qfyr_t *next_qualifier;
391 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
392 struct llc_sock *llc = llc_sk(sk);
393 struct llc_conn_state *curr_state =
394 &llc_conn_state_table[llc->state - 1];
395
396
397
398
399 for (next_trans = curr_state->transitions +
400 llc_find_offset(llc->state - 1, ev->type);
401 (*next_trans)->ev; next_trans++) {
402 if (!((*next_trans)->ev)(sk, skb)) {
403
404
405
406
407
408
409 for (next_qualifier = (*next_trans)->ev_qualifiers;
410 next_qualifier && *next_qualifier &&
411 !(*next_qualifier)(sk, skb); next_qualifier++)
412 ;
413 if (!next_qualifier || !*next_qualifier)
414
415
416
417
418 return *next_trans;
419 }
420 }
421 return NULL;
422}
423
424
425
426
427
428
429
430
431
432
433static int llc_exec_conn_trans_actions(struct sock *sk,
434 struct llc_conn_state_trans *trans,
435 struct sk_buff *skb)
436{
437 int rc = 0;
438 const llc_conn_action_t *next_action;
439
440 for (next_action = trans->ev_actions;
441 next_action && *next_action; next_action++) {
442 int rc2 = (*next_action)(sk, skb);
443
444 if (rc2 == 2) {
445 rc = rc2;
446 break;
447 } else if (rc2)
448 rc = 1;
449 }
450 return rc;
451}
452
453static inline bool llc_estab_match(const struct llc_sap *sap,
454 const struct llc_addr *daddr,
455 const struct llc_addr *laddr,
456 const struct sock *sk)
457{
458 struct llc_sock *llc = llc_sk(sk);
459
460 return llc->laddr.lsap == laddr->lsap &&
461 llc->daddr.lsap == daddr->lsap &&
462 ether_addr_equal(llc->laddr.mac, laddr->mac) &&
463 ether_addr_equal(llc->daddr.mac, daddr->mac);
464}
465
466
467
468
469
470
471
472
473
474
475
476
477static struct sock *__llc_lookup_established(struct llc_sap *sap,
478 struct llc_addr *daddr,
479 struct llc_addr *laddr)
480{
481 struct sock *rc;
482 struct hlist_nulls_node *node;
483 int slot = llc_sk_laddr_hashfn(sap, laddr);
484 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
485
486 rcu_read_lock();
487again:
488 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
489 if (llc_estab_match(sap, daddr, laddr, rc)) {
490
491 if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
492 goto again;
493 if (unlikely(llc_sk(rc)->sap != sap ||
494 !llc_estab_match(sap, daddr, laddr, rc))) {
495 sock_put(rc);
496 continue;
497 }
498 goto found;
499 }
500 }
501 rc = NULL;
502
503
504
505
506
507 if (unlikely(get_nulls_value(node) != slot))
508 goto again;
509found:
510 rcu_read_unlock();
511 return rc;
512}
513
514struct sock *llc_lookup_established(struct llc_sap *sap,
515 struct llc_addr *daddr,
516 struct llc_addr *laddr)
517{
518 struct sock *sk;
519
520 local_bh_disable();
521 sk = __llc_lookup_established(sap, daddr, laddr);
522 local_bh_enable();
523 return sk;
524}
525
526static inline bool llc_listener_match(const struct llc_sap *sap,
527 const struct llc_addr *laddr,
528 const struct sock *sk)
529{
530 struct llc_sock *llc = llc_sk(sk);
531
532 return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
533 llc->laddr.lsap == laddr->lsap &&
534 ether_addr_equal(llc->laddr.mac, laddr->mac);
535}
536
537static struct sock *__llc_lookup_listener(struct llc_sap *sap,
538 struct llc_addr *laddr)
539{
540 struct sock *rc;
541 struct hlist_nulls_node *node;
542 int slot = llc_sk_laddr_hashfn(sap, laddr);
543 struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
544
545 rcu_read_lock();
546again:
547 sk_nulls_for_each_rcu(rc, node, laddr_hb) {
548 if (llc_listener_match(sap, laddr, rc)) {
549
550 if (unlikely(!refcount_inc_not_zero(&rc->sk_refcnt)))
551 goto again;
552 if (unlikely(llc_sk(rc)->sap != sap ||
553 !llc_listener_match(sap, laddr, rc))) {
554 sock_put(rc);
555 continue;
556 }
557 goto found;
558 }
559 }
560 rc = NULL;
561
562
563
564
565
566 if (unlikely(get_nulls_value(node) != slot))
567 goto again;
568found:
569 rcu_read_unlock();
570 return rc;
571}
572
573
574
575
576
577
578
579
580
581
582
583static struct sock *llc_lookup_listener(struct llc_sap *sap,
584 struct llc_addr *laddr)
585{
586 static struct llc_addr null_addr;
587 struct sock *rc = __llc_lookup_listener(sap, laddr);
588
589 if (!rc)
590 rc = __llc_lookup_listener(sap, &null_addr);
591
592 return rc;
593}
594
595static struct sock *__llc_lookup(struct llc_sap *sap,
596 struct llc_addr *daddr,
597 struct llc_addr *laddr)
598{
599 struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
600
601 return sk ? : llc_lookup_listener(sap, laddr);
602}
603
604
605
606
607
608
609
610u8 llc_data_accept_state(u8 state)
611{
612 return state != LLC_CONN_STATE_NORMAL && state != LLC_CONN_STATE_BUSY &&
613 state != LLC_CONN_STATE_REJ;
614}
615
616
617
618
619
620
621
622
623
624static u16 __init llc_find_next_offset(struct llc_conn_state *state, u16 offset)
625{
626 u16 cnt = 0;
627 struct llc_conn_state_trans **next_trans;
628
629 for (next_trans = state->transitions + offset;
630 (*next_trans)->ev; next_trans++)
631 ++cnt;
632 return cnt;
633}
634
635
636
637
638
639
640
641void __init llc_build_offset_table(void)
642{
643 struct llc_conn_state *curr_state;
644 int state, ev_type, next_offset;
645
646 for (state = 0; state < NBR_CONN_STATES; state++) {
647 curr_state = &llc_conn_state_table[state];
648 next_offset = 0;
649 for (ev_type = 0; ev_type < NBR_CONN_EV; ev_type++) {
650 llc_offset_table[state][ev_type] = next_offset;
651 next_offset += llc_find_next_offset(curr_state,
652 next_offset) + 1;
653 }
654 }
655}
656
657
658
659
660
661
662
663
664
665static int llc_find_offset(int state, int ev_type)
666{
667 int rc = 0;
668
669
670
671 switch (ev_type) {
672 case LLC_CONN_EV_TYPE_PRIM:
673 rc = llc_offset_table[state][0]; break;
674 case LLC_CONN_EV_TYPE_PDU:
675 rc = llc_offset_table[state][4]; break;
676 case LLC_CONN_EV_TYPE_SIMPLE:
677 rc = llc_offset_table[state][1]; break;
678 case LLC_CONN_EV_TYPE_P_TMR:
679 case LLC_CONN_EV_TYPE_ACK_TMR:
680 case LLC_CONN_EV_TYPE_REJ_TMR:
681 case LLC_CONN_EV_TYPE_BUSY_TMR:
682 rc = llc_offset_table[state][3]; break;
683 }
684 return rc;
685}
686
687
688
689
690
691
692
693
694void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
695{
696 struct llc_sock *llc = llc_sk(sk);
697 struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
698 struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
699
700 llc_sap_hold(sap);
701 llc_sk(sk)->sap = sap;
702
703 spin_lock_bh(&sap->sk_lock);
704 sock_set_flag(sk, SOCK_RCU_FREE);
705 sap->sk_count++;
706 sk_nulls_add_node_rcu(sk, laddr_hb);
707 hlist_add_head(&llc->dev_hash_node, dev_hb);
708 spin_unlock_bh(&sap->sk_lock);
709}
710
711
712
713
714
715
716
717
718
719void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
720{
721 struct llc_sock *llc = llc_sk(sk);
722
723 spin_lock_bh(&sap->sk_lock);
724 sk_nulls_del_node_init_rcu(sk);
725 hlist_del(&llc->dev_hash_node);
726 sap->sk_count--;
727 spin_unlock_bh(&sap->sk_lock);
728 llc_sap_put(sap);
729}
730
731
732
733
734
735
736
737
738static int llc_conn_rcv(struct sock *sk, struct sk_buff *skb)
739{
740 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
741
742 ev->type = LLC_CONN_EV_TYPE_PDU;
743 ev->reason = 0;
744 return llc_conn_state_process(sk, skb);
745}
746
747static struct sock *llc_create_incoming_sock(struct sock *sk,
748 struct net_device *dev,
749 struct llc_addr *saddr,
750 struct llc_addr *daddr)
751{
752 struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
753 sk->sk_prot, 0);
754 struct llc_sock *newllc, *llc = llc_sk(sk);
755
756 if (!newsk)
757 goto out;
758 newllc = llc_sk(newsk);
759 memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
760 memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
761 newllc->dev = dev;
762 dev_hold(dev);
763 llc_sap_add_socket(llc->sap, newsk);
764 llc_sap_hold(llc->sap);
765out:
766 return newsk;
767}
768
769void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
770{
771 struct llc_addr saddr, daddr;
772 struct sock *sk;
773
774 llc_pdu_decode_sa(skb, saddr.mac);
775 llc_pdu_decode_ssap(skb, &saddr.lsap);
776 llc_pdu_decode_da(skb, daddr.mac);
777 llc_pdu_decode_dsap(skb, &daddr.lsap);
778
779 sk = __llc_lookup(sap, &saddr, &daddr);
780 if (!sk)
781 goto drop;
782
783 bh_lock_sock(sk);
784
785
786
787
788
789
790
791
792 if (unlikely(sk->sk_state == TCP_LISTEN)) {
793 struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
794 &saddr, &daddr);
795 if (!newsk)
796 goto drop_unlock;
797 skb_set_owner_r(skb, newsk);
798 } else {
799
800
801
802
803
804
805
806 skb_orphan(skb);
807 sock_hold(sk);
808 skb->sk = sk;
809 skb->destructor = sock_efree;
810 }
811 if (!sock_owned_by_user(sk))
812 llc_conn_rcv(sk, skb);
813 else {
814 dprintk("%s: adding to backlog...\n", __func__);
815 llc_set_backlog_type(skb, LLC_PACKET);
816 if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
817 goto drop_unlock;
818 }
819out:
820 bh_unlock_sock(sk);
821 sock_put(sk);
822 return;
823drop:
824 kfree_skb(skb);
825 return;
826drop_unlock:
827 kfree_skb(skb);
828 goto out;
829}
830
831#undef LLC_REFCNT_DEBUG
832#ifdef LLC_REFCNT_DEBUG
833static atomic_t llc_sock_nr;
834#endif
835
836
837
838
839
840
841
842
843
844
845
846static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
847{
848 int rc = 0;
849 struct llc_sock *llc = llc_sk(sk);
850
851 if (likely(llc_backlog_type(skb) == LLC_PACKET)) {
852 if (likely(llc->state > 1))
853 rc = llc_conn_rcv(sk, skb);
854 else
855 goto out_kfree_skb;
856 } else if (llc_backlog_type(skb) == LLC_EVENT) {
857
858 if (likely(llc->state > 1))
859 rc = llc_conn_state_process(sk, skb);
860 else
861 goto out_kfree_skb;
862 } else {
863 printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
864 goto out_kfree_skb;
865 }
866out:
867 return rc;
868out_kfree_skb:
869 kfree_skb(skb);
870 goto out;
871}
872
873
874
875
876
877
878
879static void llc_sk_init(struct sock *sk)
880{
881 struct llc_sock *llc = llc_sk(sk);
882
883 llc->state = LLC_CONN_STATE_ADM;
884 llc->inc_cntr = llc->dec_cntr = 2;
885 llc->dec_step = llc->connect_step = 1;
886
887 timer_setup(&llc->ack_timer.timer, llc_conn_ack_tmr_cb, 0);
888 llc->ack_timer.expire = sysctl_llc2_ack_timeout;
889
890 timer_setup(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb, 0);
891 llc->pf_cycle_timer.expire = sysctl_llc2_p_timeout;
892
893 timer_setup(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb, 0);
894 llc->rej_sent_timer.expire = sysctl_llc2_rej_timeout;
895
896 timer_setup(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb, 0);
897 llc->busy_state_timer.expire = sysctl_llc2_busy_timeout;
898
899 llc->n2 = 2;
900 llc->k = 2;
901 llc->rw = 128;
902
903 skb_queue_head_init(&llc->pdu_unack_q);
904 sk->sk_backlog_rcv = llc_backlog_rcv;
905}
906
907
908
909
910
911
912
913
914
915struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot, int kern)
916{
917 struct sock *sk = sk_alloc(net, family, priority, prot, kern);
918
919 if (!sk)
920 goto out;
921 llc_sk_init(sk);
922 sock_init_data(NULL, sk);
923#ifdef LLC_REFCNT_DEBUG
924 atomic_inc(&llc_sock_nr);
925 printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
926 __func__, atomic_read(&llc_sock_nr));
927#endif
928out:
929 return sk;
930}
931
932void llc_sk_stop_all_timers(struct sock *sk, bool sync)
933{
934 struct llc_sock *llc = llc_sk(sk);
935
936 if (sync) {
937 del_timer_sync(&llc->pf_cycle_timer.timer);
938 del_timer_sync(&llc->ack_timer.timer);
939 del_timer_sync(&llc->rej_sent_timer.timer);
940 del_timer_sync(&llc->busy_state_timer.timer);
941 } else {
942 del_timer(&llc->pf_cycle_timer.timer);
943 del_timer(&llc->ack_timer.timer);
944 del_timer(&llc->rej_sent_timer.timer);
945 del_timer(&llc->busy_state_timer.timer);
946 }
947
948 llc->ack_must_be_send = 0;
949 llc->ack_pf = 0;
950}
951
952
953
954
955
956
957
958void llc_sk_free(struct sock *sk)
959{
960 struct llc_sock *llc = llc_sk(sk);
961
962 llc->state = LLC_CONN_OUT_OF_SVC;
963
964 llc_sk_stop_all_timers(sk, true);
965#ifdef DEBUG_LLC_CONN_ALLOC
966 printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
967 skb_queue_len(&llc->pdu_unack_q),
968 skb_queue_len(&sk->sk_write_queue));
969#endif
970 skb_queue_purge(&sk->sk_receive_queue);
971 skb_queue_purge(&sk->sk_write_queue);
972 skb_queue_purge(&llc->pdu_unack_q);
973#ifdef LLC_REFCNT_DEBUG
974 if (refcount_read(&sk->sk_refcnt) != 1) {
975 printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
976 sk, __func__, refcount_read(&sk->sk_refcnt));
977 printk(KERN_DEBUG "%d LLC sockets are still alive\n",
978 atomic_read(&llc_sock_nr));
979 } else {
980 atomic_dec(&llc_sock_nr);
981 printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
982 __func__, atomic_read(&llc_sock_nr));
983 }
984#endif
985 sock_put(sk);
986}
987
988
989
990
991
992
993
994
995void llc_sk_reset(struct sock *sk)
996{
997 struct llc_sock *llc = llc_sk(sk);
998
999 llc_conn_ac_stop_all_timers(sk, NULL);
1000 skb_queue_purge(&sk->sk_write_queue);
1001 skb_queue_purge(&llc->pdu_unack_q);
1002 llc->remote_busy_flag = 0;
1003 llc->cause_flag = 0;
1004 llc->retry_count = 0;
1005 llc_conn_set_p_flag(sk, 0);
1006 llc->f_flag = 0;
1007 llc->s_flag = 0;
1008 llc->ack_pf = 0;
1009 llc->first_pdu_Ns = 0;
1010 llc->ack_must_be_send = 0;
1011 llc->dec_step = 1;
1012 llc->inc_cntr = 2;
1013 llc->dec_cntr = 2;
1014 llc->X = 0;
1015 llc->failed_data_req = 0 ;
1016 llc->last_nr = 0;
1017}
1018