ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / llc / llc_c_ac.c
1 /*
2  * llc_c_ac.c - actions performed during connection state transition.
3  *
4  * Description:
5  *   Functions in this module are implementation of connection component actions
6  *   Details of actions can be found in IEEE-802.2 standard document.
7  *   All functions have one connection and one event as input argument. All of
8  *   them return 0 On success and 1 otherwise.
9  *
10  * Copyright (c) 1997 by Procom Technology, Inc.
11  *               2001-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
12  *
13  * This program can be redistributed or modified under the terms of the
14  * GNU General Public License as published by the Free Software Foundation.
15  * This program is distributed without any warranty or implied warranty
16  * of merchantability or fitness for a particular purpose.
17  *
18  * See the GNU General Public License for more details.
19  */
20 #include <linux/netdevice.h>
21 #include <net/llc_conn.h>
22 #include <net/llc_sap.h>
23 #include <net/sock.h>
24 #include <net/llc_c_ev.h>
25 #include <net/llc_c_ac.h>
26 #include <net/llc_c_st.h>
27 #include <net/llc_pdu.h>
28 #include <net/llc.h>
29
30 #include "llc_output.h"
31
32 static int llc_conn_ac_inc_vs_by_1(struct sock *sk, struct sk_buff *skb);
33 static void llc_process_tmr_ev(struct sock *sk, struct sk_buff *skb);
34 static int llc_conn_ac_data_confirm(struct sock *sk, struct sk_buff *ev);
35
36 #define INCORRECT 0
37
38 int llc_conn_ac_clear_remote_busy(struct sock *sk, struct sk_buff *skb)
39 {
40         struct llc_opt *llc = llc_sk(sk);
41
42         if (llc->remote_busy_flag) {
43                 u8 nr;
44                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
45
46                 llc->remote_busy_flag = 0;
47                 del_timer(&llc->busy_state_timer.timer);
48                 nr = LLC_I_GET_NR(pdu);
49                 llc_conn_resend_i_pdu_as_cmd(sk, nr, 0);
50         }
51         return 0;
52 }
53
54 int llc_conn_ac_conn_ind(struct sock *sk, struct sk_buff *skb)
55 {
56         int rc = -ENOTCONN;
57         u8 dsap;
58         struct llc_sap *sap;
59
60         llc_pdu_decode_dsap(skb, &dsap);
61         sap = llc_sap_find(dsap);
62         if (sap) {
63                 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
64                 struct llc_opt *llc = llc_sk(sk);
65
66                 llc_pdu_decode_sa(skb, llc->daddr.mac);
67                 llc_pdu_decode_da(skb, llc->laddr.mac);
68                 llc->dev = skb->dev;
69                 ev->ind_prim = LLC_CONN_PRIM;
70                 rc = 0;
71         }
72         return rc;
73 }
74
75 int llc_conn_ac_conn_confirm(struct sock *sk, struct sk_buff *skb)
76 {
77         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
78
79         ev->cfm_prim = LLC_CONN_PRIM;
80         return 0;
81 }
82
83 static int llc_conn_ac_data_confirm(struct sock *sk, struct sk_buff *skb)
84 {
85         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
86
87         ev->cfm_prim = LLC_DATA_PRIM;
88         return 0;
89 }
90
91 int llc_conn_ac_data_ind(struct sock *sk, struct sk_buff *skb)
92 {
93         llc_conn_rtn_pdu(sk, skb);
94         return 0;
95 }
96
97 int llc_conn_ac_disc_ind(struct sock *sk, struct sk_buff *skb)
98 {
99         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
100         u8 reason = 0;
101         int rc = 0;
102
103         if (ev->type == LLC_CONN_EV_TYPE_PDU) {
104                 struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
105
106                 if (LLC_PDU_IS_RSP(pdu) &&
107                     LLC_PDU_TYPE_IS_U(pdu) &&
108                     LLC_U_PDU_RSP(pdu) == LLC_2_PDU_RSP_DM)
109                         reason = LLC_DISC_REASON_RX_DM_RSP_PDU;
110                 else if (LLC_PDU_IS_CMD(pdu) &&
111                            LLC_PDU_TYPE_IS_U(pdu) &&
112                            LLC_U_PDU_CMD(pdu) == LLC_2_PDU_CMD_DISC)
113                         reason = LLC_DISC_REASON_RX_DISC_CMD_PDU;
114         } else if (ev->type == LLC_CONN_EV_TYPE_ACK_TMR)
115                 reason = LLC_DISC_REASON_ACK_TMR_EXP;
116         else {
117                 reason = 0;
118                 rc = -EINVAL;
119         }
120         if (!rc) {
121                 ev->reason   = reason;
122                 ev->ind_prim = LLC_DISC_PRIM;
123         }
124         return rc;
125 }
126
127 int llc_conn_ac_disc_confirm(struct sock *sk, struct sk_buff *skb)
128 {
129         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
130
131         ev->reason   = ev->status;
132         ev->cfm_prim = LLC_DISC_PRIM;
133         return 0;
134 }
135
136 int llc_conn_ac_rst_ind(struct sock *sk, struct sk_buff *skb)
137 {
138         u8 reason = 0;
139         int rc = 1;
140         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
141         struct llc_pdu_un *pdu = llc_pdu_un_hdr(skb);
142         struct llc_opt *llc = llc_sk(sk);
143
144         switch (ev->type) {
145         case LLC_CONN_EV_TYPE_PDU:
146                 if (LLC_PDU_IS_RSP(pdu) &&
147                     LLC_PDU_TYPE_IS_U(pdu) &&
148                     LLC_U_PDU_RSP(pdu) == LLC_2_PDU_RSP_FRMR) {
149                         reason = LLC_RESET_REASON_LOCAL;
150                         rc = 0;
151                 } else if (LLC_PDU_IS_CMD(pdu) &&
152                            LLC_PDU_TYPE_IS_U(pdu) &&
153                            LLC_U_PDU_CMD(pdu) == LLC_2_PDU_CMD_SABME) {
154                         reason = LLC_RESET_REASON_REMOTE;
155                         rc = 0;
156                 } else {
157                         reason = 0;
158                         rc  = 1;
159                 }
160                 break;
161         case LLC_CONN_EV_TYPE_ACK_TMR:
162         case LLC_CONN_EV_TYPE_P_TMR:
163         case LLC_CONN_EV_TYPE_REJ_TMR:
164         case LLC_CONN_EV_TYPE_BUSY_TMR:
165                 if (llc->retry_count > llc->n2) {
166                         reason = LLC_RESET_REASON_LOCAL;
167                         rc = 0;
168                 } else
169                         rc = 1;
170                 break;
171         }
172         if (!rc) {
173                 ev->reason   = reason;
174                 ev->ind_prim = LLC_RESET_PRIM;
175         }
176         return rc;
177 }
178
179 int llc_conn_ac_rst_confirm(struct sock *sk, struct sk_buff *skb)
180 {
181         struct llc_conn_state_ev *ev = llc_conn_ev(skb);
182
183         ev->reason   = 0;
184         ev->cfm_prim = LLC_RESET_PRIM;
185         return 0;
186 }
187
188 int llc_conn_ac_report_status(struct sock *sk, struct sk_buff *skb)
189 {
190         return 0;
191 }
192
193 int llc_conn_ac_clear_remote_busy_if_f_eq_1(struct sock *sk,
194                                             struct sk_buff *skb)
195 {
196         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
197
198         if (LLC_PDU_IS_RSP(pdu) &&
199             LLC_PDU_TYPE_IS_I(pdu) &&
200             LLC_I_PF_IS_1(pdu) && llc_sk(sk)->ack_pf)
201                 llc_conn_ac_clear_remote_busy(sk, skb);
202         return 0;
203 }
204
205 int llc_conn_ac_stop_rej_tmr_if_data_flag_eq_2(struct sock *sk,
206                                                struct sk_buff *skb)
207 {
208         struct llc_opt *llc = llc_sk(sk);
209
210         if (llc->data_flag == 2)
211                 del_timer(&llc->rej_sent_timer.timer);
212         return 0;
213 }
214
215 int llc_conn_ac_send_disc_cmd_p_set_x(struct sock *sk, struct sk_buff *skb)
216 {
217         int rc = -ENOBUFS;
218         struct sk_buff *nskb = llc_alloc_frame();
219
220         if (nskb) {
221                 struct llc_opt *llc = llc_sk(sk);
222                 struct llc_sap *sap = llc->sap;
223
224                 nskb->dev = llc->dev;
225                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
226                                     llc->daddr.lsap, LLC_PDU_CMD);
227                 llc_pdu_init_as_disc_cmd(nskb, 1);
228                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
229                 if (rc)
230                         goto free;
231                 llc_conn_send_pdu(sk, nskb);
232                 llc_conn_ac_set_p_flag_1(sk, skb);
233         }
234 out:
235         return rc;
236 free:
237         kfree_skb(nskb);
238         goto out;
239 }
240
241 int llc_conn_ac_send_dm_rsp_f_set_p(struct sock *sk, struct sk_buff *skb)
242 {
243         int rc = -ENOBUFS;
244         struct sk_buff *nskb = llc_alloc_frame();
245
246         if (nskb) {
247                 struct llc_opt *llc = llc_sk(sk);
248                 struct llc_sap *sap = llc->sap;
249                 u8 f_bit;
250
251                 nskb->dev = llc->dev;
252                 llc_pdu_decode_pf_bit(skb, &f_bit);
253                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
254                                     llc->daddr.lsap, LLC_PDU_RSP);
255                 llc_pdu_init_as_dm_rsp(nskb, f_bit);
256                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
257                 if (rc)
258                         goto free;
259                 llc_conn_send_pdu(sk, nskb);
260         }
261 out:
262         return rc;
263 free:
264         kfree_skb(nskb);
265         goto out;
266 }
267
268 int llc_conn_ac_send_dm_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
269 {
270         int rc = -ENOBUFS;
271         struct sk_buff *nskb = llc_alloc_frame();
272
273         if (nskb) {
274                 struct llc_opt *llc = llc_sk(sk);
275                 struct llc_sap *sap = llc->sap;
276                 u8 f_bit = 1;
277
278                 nskb->dev = llc->dev;
279                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
280                                     llc->daddr.lsap, LLC_PDU_RSP);
281                 llc_pdu_init_as_dm_rsp(nskb, f_bit);
282                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
283                 if (rc)
284                         goto free;
285                 llc_conn_send_pdu(sk, nskb);
286         }
287 out:
288         return rc;
289 free:
290         kfree_skb(nskb);
291         goto out;
292 }
293
294 int llc_conn_ac_send_dm_rsp_f_set_f_flag(struct sock *sk, struct sk_buff *skb)
295 {
296         int rc = -ENOBUFS;
297         struct sk_buff *nskb = llc_alloc_frame();
298
299         if (nskb) {
300                 struct llc_opt *llc = llc_sk(sk);
301                 struct llc_sap *sap = llc->sap;
302                 u8 f_bit = llc->f_flag;
303
304                 nskb->dev = llc->dev;
305                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
306                                     llc->daddr.lsap, LLC_PDU_RSP);
307                 llc_pdu_init_as_dm_rsp(nskb, f_bit);
308                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
309                 if (rc)
310                         goto free;
311                 llc_conn_send_pdu(sk, nskb);
312         }
313 out:
314         return rc;
315 free:
316         kfree_skb(nskb);
317         goto out;
318 }
319
320 int llc_conn_ac_send_frmr_rsp_f_set_x(struct sock *sk, struct sk_buff *skb)
321 {
322         u8 f_bit;
323         int rc = -ENOBUFS;
324         struct sk_buff *nskb;
325         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
326         struct llc_opt *llc = llc_sk(sk);
327
328         llc->rx_pdu_hdr = *((u32 *)pdu);
329         if (LLC_PDU_IS_CMD(pdu))
330                 llc_pdu_decode_pf_bit(skb, &f_bit);
331         else
332                 f_bit = 0;
333         nskb = llc_alloc_frame();
334         if (nskb) {
335                 struct llc_sap *sap = llc->sap;
336
337                 nskb->dev = llc->dev;
338                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
339                                     llc->daddr.lsap, LLC_PDU_RSP);
340                 llc_pdu_init_as_frmr_rsp(nskb, pdu, f_bit, llc->vS,
341                                          llc->vR, INCORRECT);
342                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
343                 if (rc)
344                         goto free;
345                 llc_conn_send_pdu(sk, nskb);
346         }
347 out:
348         return rc;
349 free:
350         kfree_skb(nskb);
351         goto out;
352 }
353
354 int llc_conn_ac_resend_frmr_rsp_f_set_0(struct sock *sk, struct sk_buff *skb)
355 {
356         int rc = -ENOBUFS;
357         struct sk_buff *nskb = llc_alloc_frame();
358
359         if (nskb) {
360                 u8 f_bit = 0;
361                 struct llc_opt *llc = llc_sk(sk);
362                 struct llc_sap *sap = llc->sap;
363                 struct llc_pdu_sn *pdu = (struct llc_pdu_sn *)&llc->rx_pdu_hdr;
364
365                 nskb->dev = llc->dev;
366                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
367                                     llc->daddr.lsap, LLC_PDU_RSP);
368                 llc_pdu_init_as_frmr_rsp(nskb, pdu, f_bit, llc->vS,
369                                          llc->vR, INCORRECT);
370                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
371                 if (rc)
372                         goto free;
373                 llc_conn_send_pdu(sk, nskb);
374         }
375 out:
376         return rc;
377 free:
378         kfree_skb(nskb);
379         goto out;
380 }
381
382 int llc_conn_ac_resend_frmr_rsp_f_set_p(struct sock *sk, struct sk_buff *skb)
383 {
384         u8 f_bit;
385         int rc = -ENOBUFS;
386         struct sk_buff *nskb;
387
388         llc_pdu_decode_pf_bit(skb, &f_bit);
389         nskb = llc_alloc_frame();
390         if (nskb) {
391                 struct llc_opt *llc = llc_sk(sk);
392                 struct llc_sap *sap = llc->sap;
393                 struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
394
395                 nskb->dev = llc->dev;
396                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
397                                     llc->daddr.lsap, LLC_PDU_RSP);
398                 llc_pdu_init_as_frmr_rsp(nskb, pdu, f_bit, llc->vS,
399                                          llc->vR, INCORRECT);
400                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
401                 if (rc)
402                         goto free;
403                 llc_conn_send_pdu(sk, nskb);
404         }
405 out:
406         return rc;
407 free:
408         kfree_skb(nskb);
409         goto out;
410 }
411
412 int llc_conn_ac_send_i_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
413 {
414         int rc;
415         struct llc_opt *llc = llc_sk(sk);
416         struct llc_sap *sap = llc->sap;
417
418         llc_pdu_header_init(skb, LLC_PDU_TYPE_I, sap->laddr.lsap,
419                             llc->daddr.lsap, LLC_PDU_CMD);
420         llc_pdu_init_as_i_cmd(skb, 1, llc->vS, llc->vR);
421         rc = llc_mac_hdr_init(skb, llc->dev->dev_addr, llc->daddr.mac);
422         if (!rc) {
423                 llc_conn_send_pdu(sk, skb);
424                 llc_conn_ac_inc_vs_by_1(sk, skb);
425         }
426         return rc;
427 }
428
429 int llc_conn_ac_send_i_cmd_p_set_0(struct sock *sk, struct sk_buff *skb)
430 {
431         int rc;
432         struct llc_opt *llc = llc_sk(sk);
433         struct llc_sap *sap = llc->sap;
434
435         llc_pdu_header_init(skb, LLC_PDU_TYPE_I, sap->laddr.lsap,
436                             llc->daddr.lsap, LLC_PDU_CMD);
437         llc_pdu_init_as_i_cmd(skb, 0, llc->vS, llc->vR);
438         rc = llc_mac_hdr_init(skb, llc->dev->dev_addr, llc->daddr.mac);
439         if (!rc) {
440                 llc_conn_send_pdu(sk, skb);
441                 llc_conn_ac_inc_vs_by_1(sk, skb);
442         }
443         return rc;
444 }
445
446 int llc_conn_ac_resend_i_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
447 {
448         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
449         u8 nr = LLC_I_GET_NR(pdu);
450
451         llc_conn_resend_i_pdu_as_cmd(sk, nr, 1);
452         return 0;
453 }
454
455 int llc_conn_ac_resend_i_cmd_p_set_1_or_send_rr(struct sock *sk,
456                                                 struct sk_buff *skb)
457 {
458         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
459         u8 nr = LLC_I_GET_NR(pdu);
460         int rc = llc_conn_ac_send_rr_cmd_p_set_1(sk, skb);
461
462         if (!rc)
463                 llc_conn_resend_i_pdu_as_cmd(sk, nr, 0);
464         return rc;
465 }
466
467 int llc_conn_ac_send_i_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
468 {
469         int rc;
470         struct llc_opt *llc = llc_sk(sk);
471         struct llc_sap *sap = llc->sap;
472
473         llc_pdu_header_init(skb, LLC_PDU_TYPE_I, sap->laddr.lsap,
474                             llc->daddr.lsap, LLC_PDU_CMD);
475         llc_pdu_init_as_i_cmd(skb, 0, llc->vS, llc->vR);
476         rc = llc_mac_hdr_init(skb, llc->dev->dev_addr, llc->daddr.mac);
477         if (!rc) {
478                 llc_conn_send_pdu(sk, skb);
479                 llc_conn_ac_inc_vs_by_1(sk, skb);
480         }
481         return 0;
482 }
483
484 int llc_conn_ac_resend_i_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
485 {
486         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
487         u8 nr = LLC_I_GET_NR(pdu);
488
489         llc_conn_resend_i_pdu_as_cmd(sk, nr, 0);
490         return 0;
491 }
492
493 int llc_conn_ac_resend_i_xxx_x_set_0_or_send_rr(struct sock *sk,
494                                                 struct sk_buff *skb)
495 {
496         u8 nr;
497         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
498         int rc = -ENOBUFS;
499         struct sk_buff *nskb = llc_alloc_frame();
500
501         if (nskb) {
502                 struct llc_opt *llc = llc_sk(sk);
503                 struct llc_sap *sap = llc->sap;
504
505                 nskb->dev = llc->dev;
506                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
507                                     llc->daddr.lsap, LLC_PDU_RSP);
508                 llc_pdu_init_as_rr_rsp(nskb, 0, llc->vR);
509                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
510                 if (!rc)
511                         llc_conn_send_pdu(sk, nskb);
512                 else
513                         kfree_skb(skb);
514         }
515         if (rc) {
516                 nr = LLC_I_GET_NR(pdu);
517                 rc = 0;
518                 llc_conn_resend_i_pdu_as_cmd(sk, nr, 0);
519         }
520         return rc;
521 }
522
523 int llc_conn_ac_resend_i_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
524 {
525         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
526         u8 nr = LLC_I_GET_NR(pdu);
527
528         llc_conn_resend_i_pdu_as_rsp(sk, nr, 1);
529         return 0;
530 }
531
532 int llc_conn_ac_send_rej_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
533 {
534         int rc = -ENOBUFS;
535         struct sk_buff *nskb = llc_alloc_frame();
536
537         if (nskb) {
538                 struct llc_opt *llc = llc_sk(sk);
539                 struct llc_sap *sap = llc->sap;
540
541                 nskb->dev = llc->dev;
542                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
543                                     llc->daddr.lsap, LLC_PDU_CMD);
544                 llc_pdu_init_as_rej_cmd(nskb, 1, llc->vR);
545                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
546                 if (rc)
547                         goto free;
548                 llc_conn_send_pdu(sk, nskb);
549         }
550 out:
551         return rc;
552 free:
553         kfree_skb(nskb);
554         goto out;
555 }
556
557 int llc_conn_ac_send_rej_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
558 {
559         int rc = -ENOBUFS;
560         struct sk_buff *nskb = llc_alloc_frame();
561
562         if (nskb) {
563                 u8 f_bit = 1;
564                 struct llc_opt *llc = llc_sk(sk);
565                 struct llc_sap *sap = llc->sap;
566
567                 nskb->dev = llc->dev;
568                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
569                                     llc->daddr.lsap, LLC_PDU_RSP);
570                 llc_pdu_init_as_rej_rsp(nskb, f_bit, llc->vR);
571                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
572                 if (rc)
573                         goto free;
574                 llc_conn_send_pdu(sk, nskb);
575         }
576 out:
577         return rc;
578 free:
579         kfree_skb(nskb);
580         goto out;
581 }
582
583 int llc_conn_ac_send_rej_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
584 {
585         int rc = -ENOBUFS;
586         struct sk_buff *nskb = llc_alloc_frame();
587
588         if (nskb) {
589                 struct llc_opt *llc = llc_sk(sk);
590                 struct llc_sap *sap = llc->sap;
591                 u8 f_bit = 0;
592
593                 nskb->dev = llc->dev;
594                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
595                                     llc->daddr.lsap, LLC_PDU_RSP);
596                 llc_pdu_init_as_rej_rsp(nskb, f_bit, llc->vR);
597                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
598                 if (rc)
599                         goto free;
600                 llc_conn_send_pdu(sk, nskb);
601         }
602 out:
603         return rc;
604 free:
605         kfree_skb(nskb);
606         goto out;
607 }
608
609 int llc_conn_ac_send_rnr_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
610 {
611         int rc = -ENOBUFS;
612         struct sk_buff *nskb = llc_alloc_frame();
613
614         if (nskb) {
615                 struct llc_opt *llc = llc_sk(sk);
616                 struct llc_sap *sap = llc->sap;
617
618                 nskb->dev = llc->dev;
619                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
620                                     llc->daddr.lsap, LLC_PDU_CMD);
621                 llc_pdu_init_as_rnr_cmd(nskb, 1, llc->vR);
622                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
623                 if (rc)
624                         goto free;
625                 llc_conn_send_pdu(sk, nskb);
626         }
627 out:
628         return rc;
629 free:
630         kfree_skb(nskb);
631         goto out;
632 }
633
634 int llc_conn_ac_send_rnr_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
635 {
636         int rc = -ENOBUFS;
637         struct sk_buff *nskb = llc_alloc_frame();
638
639         if (nskb) {
640                 struct llc_opt *llc = llc_sk(sk);
641                 struct llc_sap *sap = llc->sap;
642                 u8 f_bit = 1;
643
644                 nskb->dev = llc->dev;
645                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
646                                     llc->daddr.lsap, LLC_PDU_RSP);
647                 llc_pdu_init_as_rnr_rsp(nskb, f_bit, llc->vR);
648                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
649                 if (rc)
650                         goto free;
651                 llc_conn_send_pdu(sk, nskb);
652         }
653 out:
654         return rc;
655 free:
656         kfree_skb(nskb);
657         goto out;
658 }
659
660 int llc_conn_ac_send_rnr_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
661 {
662         int rc = -ENOBUFS;
663         struct sk_buff *nskb = llc_alloc_frame();
664
665         if (nskb) {
666                 u8 f_bit = 0;
667                 struct llc_opt *llc = llc_sk(sk);
668                 struct llc_sap *sap = llc->sap;
669
670                 nskb->dev = llc->dev;
671                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
672                                     llc->daddr.lsap, LLC_PDU_RSP);
673                 llc_pdu_init_as_rnr_rsp(nskb, f_bit, llc->vR);
674                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
675                 if (rc)
676                         goto free;
677                 llc_conn_send_pdu(sk, nskb);
678         }
679 out:
680         return rc;
681 free:
682         kfree_skb(nskb);
683         goto out;
684 }
685
686 int llc_conn_ac_set_remote_busy(struct sock *sk, struct sk_buff *skb)
687 {
688         struct llc_opt *llc = llc_sk(sk);
689
690         if (!llc->remote_busy_flag) {
691                 llc->remote_busy_flag = 1;
692                 mod_timer(&llc->busy_state_timer.timer,
693                          jiffies + llc->busy_state_timer.expire * HZ);
694         }
695         return 0;
696 }
697
698 int llc_conn_ac_opt_send_rnr_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
699 {
700         int rc = -ENOBUFS;
701         struct sk_buff *nskb = llc_alloc_frame();
702
703         if (nskb) {
704                 struct llc_opt *llc = llc_sk(sk);
705                 struct llc_sap *sap = llc->sap;
706
707                 nskb->dev = llc->dev;
708                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
709                                     llc->daddr.lsap, LLC_PDU_RSP);
710                 llc_pdu_init_as_rnr_rsp(nskb, 0, llc->vR);
711                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
712                 if (rc)
713                         goto free;
714                 llc_conn_send_pdu(sk, nskb);
715         }
716 out:
717         return rc;
718 free:
719         kfree_skb(nskb);
720         goto out;
721 }
722
723 int llc_conn_ac_send_rr_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
724 {
725         int rc = -ENOBUFS;
726         struct sk_buff *nskb = llc_alloc_frame();
727
728         if (nskb) {
729                 struct llc_opt *llc = llc_sk(sk);
730                 struct llc_sap *sap = llc->sap;
731
732                 nskb->dev = llc->dev;
733                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
734                                     llc->daddr.lsap, LLC_PDU_CMD);
735                 llc_pdu_init_as_rr_cmd(nskb, 1, llc->vR);
736                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
737                 if (rc)
738                         goto free;
739                 llc_conn_send_pdu(sk, nskb);
740         }
741 out:
742         return rc;
743 free:
744         kfree_skb(nskb);
745         goto out;
746 }
747
748 int llc_conn_ac_send_ack_cmd_p_set_1(struct sock *sk, struct sk_buff *skb)
749 {
750         int rc = -ENOBUFS;
751         struct sk_buff *nskb = llc_alloc_frame();
752
753         if (nskb) {
754                 struct llc_opt *llc = llc_sk(sk);
755                 struct llc_sap *sap = llc->sap;
756
757                 nskb->dev = llc->dev;
758                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
759                                     llc->daddr.lsap, LLC_PDU_CMD);
760                 llc_pdu_init_as_rr_cmd(nskb, 1, llc->vR);
761                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
762                 if (rc)
763                         goto free;
764                 llc_conn_send_pdu(sk, nskb);
765         }
766 out:
767         return rc;
768 free:
769         kfree_skb(nskb);
770         goto out;
771 }
772
773 int llc_conn_ac_send_rr_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
774 {
775         int rc = -ENOBUFS;
776         struct sk_buff *nskb = llc_alloc_frame();
777
778         if (nskb) {
779                 struct llc_opt *llc = llc_sk(sk);
780                 struct llc_sap *sap = llc->sap;
781                 u8 f_bit = 1;
782
783                 nskb->dev = llc->dev;
784                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
785                                     llc->daddr.lsap, LLC_PDU_RSP);
786                 llc_pdu_init_as_rr_rsp(nskb, f_bit, llc->vR);
787                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
788                 if (rc)
789                         goto free;
790                 llc_conn_send_pdu(sk, nskb);
791         }
792 out:
793         return rc;
794 free:
795         kfree_skb(nskb);
796         goto out;
797 }
798
799 int llc_conn_ac_send_ack_rsp_f_set_1(struct sock *sk, struct sk_buff *skb)
800 {
801         int rc = -ENOBUFS;
802         struct sk_buff *nskb = llc_alloc_frame();
803
804         if (nskb) {
805                 struct llc_opt *llc = llc_sk(sk);
806                 struct llc_sap *sap = llc->sap;
807                 u8 f_bit = 1;
808
809                 nskb->dev = llc->dev;
810                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
811                                     llc->daddr.lsap, LLC_PDU_RSP);
812                 llc_pdu_init_as_rr_rsp(nskb, f_bit, llc->vR);
813                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
814                 if (rc)
815                         goto free;
816                 llc_conn_send_pdu(sk, nskb);
817         }
818 out:
819         return rc;
820 free:
821         kfree_skb(nskb);
822         goto out;
823 }
824
825 int llc_conn_ac_send_rr_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
826 {
827         int rc = -ENOBUFS;
828         struct sk_buff *nskb = llc_alloc_frame();
829
830         if (nskb) {
831                 struct llc_opt *llc = llc_sk(sk);
832                 struct llc_sap *sap = llc->sap;
833
834                 nskb->dev = llc->dev;
835                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
836                                     llc->daddr.lsap, LLC_PDU_RSP);
837                 llc_pdu_init_as_rr_rsp(nskb, 0, llc->vR);
838                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
839                 if (rc)
840                         goto free;
841                 llc_conn_send_pdu(sk, nskb);
842         }
843 out:
844         return rc;
845 free:
846         kfree_skb(nskb);
847         goto out;
848 }
849
850 int llc_conn_ac_send_ack_xxx_x_set_0(struct sock *sk, struct sk_buff *skb)
851 {
852         int rc = -ENOBUFS;
853         struct sk_buff *nskb = llc_alloc_frame();
854
855         if (nskb) {
856                 struct llc_opt *llc = llc_sk(sk);
857                 struct llc_sap *sap = llc->sap;
858
859                 nskb->dev = llc->dev;
860                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
861                                     llc->daddr.lsap, LLC_PDU_RSP);
862                 llc_pdu_init_as_rr_rsp(nskb, 0, llc->vR);
863                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
864                 if (rc)
865                         goto free;
866                 llc_conn_send_pdu(sk, nskb);
867         }
868 out:
869         return rc;
870 free:
871         kfree_skb(nskb);
872         goto out;
873 }
874
875 void llc_conn_set_p_flag(struct sock *sk, u8 value)
876 {
877         int state_changed = llc_sk(sk)->p_flag && !value;
878
879         llc_sk(sk)->p_flag = value;
880
881         if (state_changed)
882                 sk->sk_state_change(sk);
883 }
884
885 int llc_conn_ac_send_sabme_cmd_p_set_x(struct sock *sk, struct sk_buff *skb)
886 {
887         int rc = -ENOBUFS;
888         struct sk_buff *nskb = llc_alloc_frame();
889         struct llc_opt *llc = llc_sk(sk);
890
891         if (nskb) {
892                 struct llc_sap *sap = llc->sap;
893                 u8 *dmac = llc->daddr.mac;
894
895                 if (llc->dev->flags & IFF_LOOPBACK)
896                         dmac = llc->dev->dev_addr;
897                 nskb->dev = llc->dev;
898                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
899                                     llc->daddr.lsap, LLC_PDU_CMD);
900                 llc_pdu_init_as_sabme_cmd(nskb, 1);
901                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, dmac);
902                 if (rc)
903                         goto free;
904                 llc_conn_send_pdu(sk, nskb);
905                 llc_conn_set_p_flag(sk, 1);
906         }
907 out:
908         return rc;
909 free:
910         kfree_skb(nskb);
911         goto out;
912 }
913
914 int llc_conn_ac_send_ua_rsp_f_set_f_flag(struct sock *sk, struct sk_buff *skb)
915 {
916         int rc = -ENOBUFS;
917         struct sk_buff *nskb = llc_alloc_frame();
918
919         if (nskb) {
920                 struct llc_opt *llc = llc_sk(sk);
921                 struct llc_sap *sap = llc->sap;
922
923                 nskb->dev = llc->dev;
924                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
925                                     llc->daddr.lsap, LLC_PDU_RSP);
926                 llc_pdu_init_as_ua_rsp(nskb, llc->f_flag);
927                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
928                 if (rc)
929                         goto free;
930                 llc_conn_send_pdu(sk, nskb);
931         }
932 out:
933         return rc;
934 free:
935         kfree_skb(nskb);
936         goto out;
937 }
938
939 int llc_conn_ac_send_ua_rsp_f_set_p(struct sock *sk, struct sk_buff *skb)
940 {
941         u8 f_bit;
942         int rc = -ENOBUFS;
943         struct sk_buff *nskb = llc_alloc_frame();
944
945         llc_pdu_decode_pf_bit(skb, &f_bit);
946         if (nskb) {
947                 struct llc_opt *llc = llc_sk(sk);
948                 struct llc_sap *sap = llc->sap;
949
950                 nskb->dev = llc->dev;
951                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_U, sap->laddr.lsap,
952                                     llc->daddr.lsap, LLC_PDU_RSP);
953                 llc_pdu_init_as_ua_rsp(nskb, f_bit);
954                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
955                 if (rc)
956                         goto free;
957                 llc_conn_send_pdu(sk, nskb);
958         }
959 out:
960         return rc;
961 free:
962         kfree_skb(nskb);
963         goto out;
964 }
965
966 int llc_conn_ac_set_s_flag_0(struct sock *sk, struct sk_buff *skb)
967 {
968         llc_sk(sk)->s_flag = 0;
969         return 0;
970 }
971
972 int llc_conn_ac_set_s_flag_1(struct sock *sk, struct sk_buff *skb)
973 {
974         llc_sk(sk)->s_flag = 1;
975         return 0;
976 }
977
978 int llc_conn_ac_start_p_timer(struct sock *sk, struct sk_buff *skb)
979 {
980         struct llc_opt *llc = llc_sk(sk);
981
982         llc_conn_set_p_flag(sk, 1);
983         mod_timer(&llc->pf_cycle_timer.timer,
984                   jiffies + llc->pf_cycle_timer.expire * HZ);
985         return 0;
986 }
987
988 /**
989  *      llc_conn_ac_send_ack_if_needed - check if ack is needed
990  *      @sk: current connection structure
991  *      @skb: current event
992  *
993  *      Checks number of received PDUs which have not been acknowledged, yet,
994  *      If number of them reaches to "npta"(Number of PDUs To Acknowledge) then
995  *      sends an RR response as acknowledgement for them.  Returns 0 for
996  *      success, 1 otherwise.
997  */
998 int llc_conn_ac_send_ack_if_needed(struct sock *sk, struct sk_buff *skb)
999 {
1000         u8 pf_bit;
1001         struct llc_opt *llc = llc_sk(sk);
1002
1003         llc_pdu_decode_pf_bit(skb, &pf_bit);
1004         llc->ack_pf |= pf_bit & 1;
1005         if (!llc->ack_must_be_send) {
1006                 llc->first_pdu_Ns = llc->vR;
1007                 llc->ack_must_be_send = 1;
1008                 llc->ack_pf = pf_bit & 1;
1009         }
1010         if (((llc->vR - llc->first_pdu_Ns + 129) % 128) >= llc->npta) {
1011                 llc_conn_ac_send_rr_rsp_f_set_ackpf(sk, skb);
1012                 llc->ack_must_be_send   = 0;
1013                 llc->ack_pf             = 0;
1014                 llc_conn_ac_inc_npta_value(sk, skb);
1015         }
1016         return 0;
1017 }
1018
1019 /**
1020  *      llc_conn_ac_rst_sendack_flag - resets ack_must_be_send flag
1021  *      @sk: current connection structure
1022  *      @skb: current event
1023  *
1024  *      This action resets ack_must_be_send flag of given connection, this flag
1025  *      indicates if there is any PDU which has not been acknowledged yet.
1026  *      Returns 0 for success, 1 otherwise.
1027  */
1028 int llc_conn_ac_rst_sendack_flag(struct sock *sk, struct sk_buff *skb)
1029 {
1030         llc_sk(sk)->ack_must_be_send = llc_sk(sk)->ack_pf = 0;
1031         return 0;
1032 }
1033
1034 /**
1035  *      llc_conn_ac_send_i_rsp_f_set_ackpf - acknowledge received PDUs
1036  *      @sk: current connection structure
1037  *      @skb: current event
1038  *
1039  *      Sends an I response PDU with f-bit set to ack_pf flag as acknowledge to
1040  *      all received PDUs which have not been acknowledged, yet. ack_pf flag is
1041  *      set to one if one PDU with p-bit set to one is received.  Returns 0 for
1042  *      success, 1 otherwise.
1043  */
1044 int llc_conn_ac_send_i_rsp_f_set_ackpf(struct sock *sk, struct sk_buff *skb)
1045 {
1046         int rc;
1047         struct llc_opt *llc = llc_sk(sk);
1048         struct llc_sap *sap = llc->sap;
1049
1050         llc_pdu_header_init(skb, LLC_PDU_TYPE_I, sap->laddr.lsap,
1051                             llc->daddr.lsap, LLC_PDU_RSP);
1052         llc_pdu_init_as_i_cmd(skb, llc->ack_pf, llc->vS, llc->vR);
1053         rc = llc_mac_hdr_init(skb, llc->dev->dev_addr, llc->daddr.mac);
1054         if (!rc) {
1055                 llc_conn_send_pdu(sk, skb);
1056                 llc_conn_ac_inc_vs_by_1(sk, skb);
1057         }
1058         return rc;
1059 }
1060
1061 /**
1062  *      llc_conn_ac_send_i_as_ack - sends an I-format PDU to acknowledge rx PDUs
1063  *      @sk: current connection structure.
1064  *      @skb: current event.
1065  *
1066  *      This action sends an I-format PDU as acknowledge to received PDUs which
1067  *      have not been acknowledged, yet, if there is any. By using of this
1068  *      action number of acknowledgements decreases, this technic is called
1069  *      piggy backing. Returns 0 for success, 1 otherwise.
1070  */
1071 int llc_conn_ac_send_i_as_ack(struct sock *sk, struct sk_buff *skb)
1072 {
1073         struct llc_opt *llc = llc_sk(sk);
1074
1075         if (llc->ack_must_be_send) {
1076                 llc_conn_ac_send_i_rsp_f_set_ackpf(sk, skb);
1077                 llc->ack_must_be_send = 0 ;
1078                 llc->ack_pf = 0;
1079         } else
1080                 llc_conn_ac_send_i_cmd_p_set_0(sk, skb);
1081         return 0;
1082 }
1083
1084 /**
1085  *      llc_conn_ac_send_rr_rsp_f_set_ackpf - ack all rx PDUs not yet acked
1086  *      @sk: current connection structure.
1087  *      @skb: current event.
1088  *
1089  *      This action sends an RR response with f-bit set to ack_pf flag as
1090  *      acknowledge to all received PDUs which have not been acknowledged, yet,
1091  *      if there is any. ack_pf flag indicates if a PDU has been received with
1092  *      p-bit set to one. Returns 0 for success, 1 otherwise.
1093  */
1094 int llc_conn_ac_send_rr_rsp_f_set_ackpf(struct sock *sk, struct sk_buff *skb)
1095 {
1096         int rc = -ENOBUFS;
1097         struct sk_buff *nskb = llc_alloc_frame();
1098
1099         if (nskb) {
1100                 struct llc_opt *llc = llc_sk(sk);
1101                 struct llc_sap *sap = llc->sap;
1102
1103                 nskb->dev = llc->dev;
1104                 llc_pdu_header_init(nskb, LLC_PDU_TYPE_S, sap->laddr.lsap,
1105                                     llc->daddr.lsap, LLC_PDU_RSP);
1106                 llc_pdu_init_as_rr_rsp(nskb, llc->ack_pf, llc->vR);
1107                 rc = llc_mac_hdr_init(nskb, llc->dev->dev_addr, llc->daddr.mac);
1108                 if (rc)
1109                         goto free;
1110                 llc_conn_send_pdu(sk, nskb);
1111         }
1112 out:
1113         return rc;
1114 free:
1115         kfree_skb(nskb);
1116         goto out;
1117 }
1118
1119 /**
1120  *      llc_conn_ac_inc_npta_value - tries to make value of npta greater
1121  *      @sk: current connection structure.
1122  *      @skb: current event.
1123  *
1124  *      After "inc_cntr" times calling of this action, "npta" increase by one.
1125  *      this action tries to make vale of "npta" greater as possible; number of
1126  *      acknowledgements decreases by increasing of "npta". Returns 0 for
1127  *      success, 1 otherwise.
1128  */
1129 int llc_conn_ac_inc_npta_value(struct sock *sk, struct sk_buff *skb)
1130 {
1131         struct llc_opt *llc = llc_sk(sk);
1132
1133         if (!llc->inc_cntr) {
1134                 llc->dec_step = 0;
1135                 llc->dec_cntr = llc->inc_cntr = 2;
1136                 ++llc->npta;
1137                 if (llc->npta > 127)
1138                         llc->npta = 127 ;
1139         } else
1140                 --llc->inc_cntr;
1141         return 0;
1142 }
1143
1144 /**
1145  *      llc_conn_ac_adjust_npta_by_rr - decreases "npta" by one
1146  *      @sk: current connection structure.
1147  *      @skb: current event.
1148  *
1149  *      After receiving "dec_cntr" times RR command, this action decreases
1150  *      "npta" by one. Returns 0 for success, 1 otherwise.
1151  */
1152 int llc_conn_ac_adjust_npta_by_rr(struct sock *sk, struct sk_buff *skb)
1153 {
1154         struct llc_opt *llc = llc_sk(sk);
1155
1156         if (!llc->connect_step && !llc->remote_busy_flag) {
1157                 if (!llc->dec_step) {
1158                         if (!llc->dec_cntr) {
1159                                 llc->inc_cntr = llc->dec_cntr = 2;
1160                                 if (llc->npta > 0)
1161                                         llc->npta = llc->npta - 1;
1162                         } else
1163                                 llc->dec_cntr -=1;
1164                 }
1165         } else
1166                 llc->connect_step = 0 ;
1167         return 0;
1168 }
1169
1170 /**
1171  *      llc_conn_ac_adjust_npta_by_rnr - decreases "npta" by one
1172  *      @sk: current connection structure.
1173  *      @skb: current event.
1174  *
1175  *      After receiving "dec_cntr" times RNR command, this action decreases
1176  *      "npta" by one. Returns 0 for success, 1 otherwise.
1177  */
1178 int llc_conn_ac_adjust_npta_by_rnr(struct sock *sk, struct sk_buff *skb)
1179 {
1180         struct llc_opt *llc = llc_sk(sk);
1181
1182         if (llc->remote_busy_flag)
1183                 if (!llc->dec_step) {
1184                         if (!llc->dec_cntr) {
1185                                 llc->inc_cntr = llc->dec_cntr = 2;
1186                                 if (llc->npta > 0)
1187                                         --llc->npta;
1188                         } else
1189                                 --llc->dec_cntr;
1190                 }
1191         return 0;
1192 }
1193
1194 /**
1195  *      llc_conn_ac_dec_tx_win_size - decreases tx window size
1196  *      @sk: current connection structure.
1197  *      @skb: current event.
1198  *
1199  *      After receiving of a REJ command or response, transmit window size is
1200  *      decreased by number of PDUs which are outstanding yet. Returns 0 for
1201  *      success, 1 otherwise.
1202  */
1203 int llc_conn_ac_dec_tx_win_size(struct sock *sk, struct sk_buff *skb)
1204 {
1205         struct llc_opt *llc = llc_sk(sk);
1206         u8 unacked_pdu = skb_queue_len(&llc->pdu_unack_q);
1207
1208         llc->k -= unacked_pdu;
1209         if (llc->k < 2)
1210                 llc->k = 2;
1211         return 0;
1212 }
1213
1214 /**
1215  *      llc_conn_ac_inc_tx_win_size - tx window size is inc by 1
1216  *      @sk: current connection structure.
1217  *      @skb: current event.
1218  *
1219  *      After receiving an RR response with f-bit set to one, transmit window
1220  *      size is increased by one. Returns 0 for success, 1 otherwise.
1221  */
1222 int llc_conn_ac_inc_tx_win_size(struct sock *sk, struct sk_buff *skb)
1223 {
1224         struct llc_opt *llc = llc_sk(sk);
1225
1226         llc->k += 1;
1227         if (llc->k > 128)
1228                 llc->k = 128 ;
1229         return 0;
1230 }
1231
1232 int llc_conn_ac_stop_all_timers(struct sock *sk, struct sk_buff *skb)
1233 {
1234         struct llc_opt *llc = llc_sk(sk);
1235
1236         del_timer(&llc->pf_cycle_timer.timer);
1237         del_timer(&llc->ack_timer.timer);
1238         del_timer(&llc->rej_sent_timer.timer);
1239         del_timer(&llc->busy_state_timer.timer);
1240         llc->ack_must_be_send = 0;
1241         llc->ack_pf = 0;
1242         return 0;
1243 }
1244
1245 int llc_conn_ac_stop_other_timers(struct sock *sk, struct sk_buff *skb)
1246 {
1247         struct llc_opt *llc = llc_sk(sk);
1248
1249         del_timer(&llc->rej_sent_timer.timer);
1250         del_timer(&llc->pf_cycle_timer.timer);
1251         del_timer(&llc->busy_state_timer.timer);
1252         llc->ack_must_be_send = 0;
1253         llc->ack_pf = 0;
1254         return 0;
1255 }
1256
1257 int llc_conn_ac_start_ack_timer(struct sock *sk, struct sk_buff *skb)
1258 {
1259         struct llc_opt *llc = llc_sk(sk);
1260
1261         mod_timer(&llc->ack_timer.timer, jiffies + llc->ack_timer.expire * HZ);
1262         return 0;
1263 }
1264
1265 int llc_conn_ac_start_rej_timer(struct sock *sk, struct sk_buff *skb)
1266 {
1267         struct llc_opt *llc = llc_sk(sk);
1268
1269         mod_timer(&llc->rej_sent_timer.timer,
1270                   jiffies + llc->rej_sent_timer.expire * HZ);
1271         return 0;
1272 }
1273
1274 int llc_conn_ac_start_ack_tmr_if_not_running(struct sock *sk,
1275                                              struct sk_buff *skb)
1276 {
1277         struct llc_opt *llc = llc_sk(sk);
1278
1279         if (!timer_pending(&llc->ack_timer.timer))
1280                 mod_timer(&llc->ack_timer.timer,
1281                           jiffies + llc->ack_timer.expire * HZ);
1282         return 0;
1283 }
1284
1285 int llc_conn_ac_stop_ack_timer(struct sock *sk, struct sk_buff *skb)
1286 {
1287         del_timer(&llc_sk(sk)->ack_timer.timer);
1288         return 0;
1289 }
1290
1291 int llc_conn_ac_stop_p_timer(struct sock *sk, struct sk_buff *skb)
1292 {
1293         struct llc_opt *llc = llc_sk(sk);
1294
1295         del_timer(&llc->pf_cycle_timer.timer);
1296         llc_conn_set_p_flag(sk, 0);
1297         return 0;
1298 }
1299
1300 int llc_conn_ac_stop_rej_timer(struct sock *sk, struct sk_buff *skb)
1301 {
1302         del_timer(&llc_sk(sk)->rej_sent_timer.timer);
1303         return 0;
1304 }
1305
1306 int llc_conn_ac_upd_nr_received(struct sock *sk, struct sk_buff *skb)
1307 {
1308         int acked;
1309         u16 unacked = 0;
1310         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
1311         struct llc_opt *llc = llc_sk(sk);
1312
1313         llc->last_nr = PDU_SUPV_GET_Nr(pdu);
1314         acked = llc_conn_remove_acked_pdus(sk, llc->last_nr, &unacked);
1315         /* On loopback we don't queue I frames in unack_pdu_q queue. */
1316         if (acked > 0 || (llc->dev->flags & IFF_LOOPBACK)) {
1317                 llc->retry_count = 0;
1318                 del_timer(&llc->ack_timer.timer);
1319                 if (llc->failed_data_req) {
1320                         /* already, we did not accept data from upper layer
1321                          * (tx_window full or unacceptable state). Now, we
1322                          * can send data and must inform to upper layer.
1323                          */
1324                         llc->failed_data_req = 0;
1325                         llc_conn_ac_data_confirm(sk, skb);
1326                 }
1327                 if (unacked)
1328                         mod_timer(&llc->ack_timer.timer,
1329                                   jiffies + llc->ack_timer.expire * HZ);
1330         } else if (llc->failed_data_req) {
1331                 u8 f_bit;
1332
1333                 llc_pdu_decode_pf_bit(skb, &f_bit);
1334                 if (f_bit == 1) {
1335                         llc->failed_data_req = 0;
1336                         llc_conn_ac_data_confirm(sk, skb);
1337                 }
1338         }
1339         return 0;
1340 }
1341
1342 int llc_conn_ac_upd_p_flag(struct sock *sk, struct sk_buff *skb)
1343 {
1344         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
1345
1346         if (LLC_PDU_IS_RSP(pdu)) {
1347                 u8 f_bit;
1348
1349                 llc_pdu_decode_pf_bit(skb, &f_bit);
1350                 if (f_bit) {
1351                         llc_conn_set_p_flag(sk, 0);
1352                         llc_conn_ac_stop_p_timer(sk, skb);
1353                 }
1354         }
1355         return 0;
1356 }
1357
1358 int llc_conn_ac_set_data_flag_2(struct sock *sk, struct sk_buff *skb)
1359 {
1360         llc_sk(sk)->data_flag = 2;
1361         return 0;
1362 }
1363
1364 int llc_conn_ac_set_data_flag_0(struct sock *sk, struct sk_buff *skb)
1365 {
1366         llc_sk(sk)->data_flag = 0;
1367         return 0;
1368 }
1369
1370 int llc_conn_ac_set_data_flag_1(struct sock *sk, struct sk_buff *skb)
1371 {
1372         llc_sk(sk)->data_flag = 1;
1373         return 0;
1374 }
1375
1376 int llc_conn_ac_set_data_flag_1_if_data_flag_eq_0(struct sock *sk,
1377                                                   struct sk_buff *skb)
1378 {
1379         if (!llc_sk(sk)->data_flag)
1380                 llc_sk(sk)->data_flag = 1;
1381         return 0;
1382 }
1383
1384 int llc_conn_ac_set_p_flag_0(struct sock *sk, struct sk_buff *skb)
1385 {
1386         llc_conn_set_p_flag(sk, 0);
1387         return 0;
1388 }
1389
1390 int llc_conn_ac_set_p_flag_1(struct sock *sk, struct sk_buff *skb)
1391 {
1392         llc_conn_set_p_flag(sk, 1);
1393         return 0;
1394 }
1395
1396 int llc_conn_ac_set_remote_busy_0(struct sock *sk, struct sk_buff *skb)
1397 {
1398         llc_sk(sk)->remote_busy_flag = 0;
1399         return 0;
1400 }
1401
1402 int llc_conn_ac_set_cause_flag_0(struct sock *sk, struct sk_buff *skb)
1403 {
1404         llc_sk(sk)->cause_flag = 0;
1405         return 0;
1406 }
1407
1408 int llc_conn_ac_set_cause_flag_1(struct sock *sk, struct sk_buff *skb)
1409 {
1410         llc_sk(sk)->cause_flag = 1;
1411         return 0;
1412 }
1413
1414 int llc_conn_ac_set_retry_cnt_0(struct sock *sk, struct sk_buff *skb)
1415 {
1416         llc_sk(sk)->retry_count = 0;
1417         return 0;
1418 }
1419
1420 int llc_conn_ac_inc_retry_cnt_by_1(struct sock *sk, struct sk_buff *skb)
1421 {
1422         llc_sk(sk)->retry_count++;
1423         return 0;
1424 }
1425
1426 int llc_conn_ac_set_vr_0(struct sock *sk, struct sk_buff *skb)
1427 {
1428         llc_sk(sk)->vR = 0;
1429         return 0;
1430 }
1431
1432 int llc_conn_ac_inc_vr_by_1(struct sock *sk, struct sk_buff *skb)
1433 {
1434         llc_sk(sk)->vR = PDU_GET_NEXT_Vr(llc_sk(sk)->vR);
1435         return 0;
1436 }
1437
1438 int llc_conn_ac_set_vs_0(struct sock *sk, struct sk_buff *skb)
1439 {
1440         llc_sk(sk)->vS = 0;
1441         return 0;
1442 }
1443
1444 int llc_conn_ac_set_vs_nr(struct sock *sk, struct sk_buff *skb)
1445 {
1446         llc_sk(sk)->vS = llc_sk(sk)->last_nr;
1447         return 0;
1448 }
1449
1450 int llc_conn_ac_inc_vs_by_1(struct sock *sk, struct sk_buff *skb)
1451 {
1452         llc_sk(sk)->vS = (llc_sk(sk)->vS + 1) % 128;
1453         return 0;
1454 }
1455
1456 int llc_conn_ac_set_f_flag_p(struct sock *sk, struct sk_buff *skb)
1457 {
1458         llc_pdu_decode_pf_bit(skb, &llc_sk(sk)->f_flag);
1459         return 0;
1460 }
1461
1462 void llc_conn_pf_cycle_tmr_cb(unsigned long timeout_data)
1463 {
1464         struct sock *sk = (struct sock *)timeout_data;
1465         struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
1466
1467         bh_lock_sock(sk);
1468         if (skb) {
1469                 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
1470
1471                 skb->sk  = sk;
1472                 ev->type = LLC_CONN_EV_TYPE_P_TMR;
1473                 llc_process_tmr_ev(sk, skb);
1474         }
1475         bh_unlock_sock(sk);
1476 }
1477
1478 void llc_conn_busy_tmr_cb(unsigned long timeout_data)
1479 {
1480         struct sock *sk = (struct sock *)timeout_data;
1481         struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
1482
1483         bh_lock_sock(sk);
1484         if (skb) {
1485                 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
1486
1487                 skb->sk  = sk;
1488                 ev->type = LLC_CONN_EV_TYPE_BUSY_TMR;
1489                 llc_process_tmr_ev(sk, skb);
1490         }
1491         bh_unlock_sock(sk);
1492 }
1493
1494 void llc_conn_ack_tmr_cb(unsigned long timeout_data)
1495 {
1496         struct sock* sk = (struct sock *)timeout_data;
1497         struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
1498
1499         bh_lock_sock(sk);
1500         if (skb) {
1501                 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
1502
1503                 skb->sk  = sk;
1504                 ev->type = LLC_CONN_EV_TYPE_ACK_TMR;
1505                 llc_process_tmr_ev(sk, skb);
1506         }
1507         bh_unlock_sock(sk);
1508 }
1509
1510 void llc_conn_rej_tmr_cb(unsigned long timeout_data)
1511 {
1512         struct sock *sk = (struct sock *)timeout_data;
1513         struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
1514
1515         bh_lock_sock(sk);
1516         if (skb) {
1517                 struct llc_conn_state_ev *ev = llc_conn_ev(skb);
1518
1519                 skb->sk  = sk;
1520                 ev->type = LLC_CONN_EV_TYPE_REJ_TMR;
1521                 llc_process_tmr_ev(sk, skb);
1522         }
1523         bh_unlock_sock(sk);
1524 }
1525
1526 int llc_conn_ac_rst_vs(struct sock *sk, struct sk_buff *skb)
1527 {
1528         llc_sk(sk)->X = llc_sk(sk)->vS;
1529         llc_conn_ac_set_vs_nr(sk, skb);
1530         return 0;
1531 }
1532
1533 int llc_conn_ac_upd_vs(struct sock *sk, struct sk_buff *skb)
1534 {
1535         struct llc_pdu_sn *pdu = llc_pdu_sn_hdr(skb);
1536         u8 nr = PDU_SUPV_GET_Nr(pdu);
1537
1538         if (llc_circular_between(llc_sk(sk)->vS, nr, llc_sk(sk)->X))
1539                 llc_conn_ac_set_vs_nr(sk, skb);
1540         return 0;
1541 }
1542
1543 /*
1544  * Non-standard actions; these not contained in IEEE specification; for
1545  * our own usage
1546  */
1547 /**
1548  *      llc_conn_disc - removes connection from SAP list and frees it
1549  *      @sk: closed connection
1550  *      @skb: occurred event
1551  */
1552 int llc_conn_disc(struct sock *sk, struct sk_buff *skb)
1553 {
1554         /* FIXME: this thing seems to want to die */
1555         return 0;
1556 }
1557
1558 /**
1559  *      llc_conn_reset - resets connection
1560  *      @sk : reseting connection.
1561  *      @skb: occurred event.
1562  *
1563  *      Stop all timers, empty all queues and reset all flags.
1564  */
1565 int llc_conn_reset(struct sock *sk, struct sk_buff *skb)
1566 {
1567         llc_sk_reset(sk);
1568         return 0;
1569 }
1570
1571 /**
1572  *      llc_circular_between - designates that b is between a and c or not
1573  *      @a: lower bound
1574  *      @b: element to see if is between a and b
1575  *      @c: upper bound
1576  *
1577  *      This function designates that b is between a and c or not (for example,
1578  *      0 is between 127 and 1). Returns 1 if b is between a and c, 0
1579  *      otherwise.
1580  */
1581 u8 llc_circular_between(u8 a, u8 b, u8 c)
1582 {
1583         b = b - a;
1584         c = c - a;
1585         return b <= c;
1586 }
1587
1588 /**
1589  *      llc_process_tmr_ev - timer backend
1590  *      @sk: active connection
1591  *      @skb: occurred event
1592  *
1593  *      This function is called from timer callback functions. When connection
1594  *      is busy (during sending a data frame) timer expiration event must be
1595  *      queued. Otherwise this event can be sent to connection state machine.
1596  *      Queued events will process by llc_backlog_rcv function after sending
1597  *      data frame.
1598  */
1599 static void llc_process_tmr_ev(struct sock *sk, struct sk_buff *skb)
1600 {
1601         if (llc_sk(sk)->state == LLC_CONN_OUT_OF_SVC) {
1602                 printk(KERN_WARNING "%s: timer called on closed connection\n",
1603                        __FUNCTION__);
1604                 kfree_skb(skb);
1605         } else {
1606                 if (!sock_owned_by_user(sk))
1607                         llc_conn_state_process(sk, skb);
1608                 else {
1609                         llc_set_backlog_type(skb, LLC_EVENT);
1610                         sk_add_backlog(sk, skb);
1611                 }
1612         }
1613 }