import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / ax25 / ax25_ds_subr.c
1 /*
2  *      AX.25 release 037
3  *
4  *      This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *      This module:
7  *              This module is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  *      Most of this code is based on the SDL diagrams published in the 7th
13  *      ARRL Computer Networking Conference papers. The diagrams have mistakes
14  *      in them, but are mostly correct. Before you modify the code could you
15  *      read the SDL diagrams as the code is not obvious and probably very
16  *      easy to break;
17  *
18  *      History
19  *      AX.25 036       Jonathan(G4KLX) Cloned from ax25_out.c and ax25_subr.c.
20  *                      Joerg(DL1BKE)   Changed ax25_ds_enquiry_response(),
21  *                                      fixed ax25_dama_on() and ax25_dama_off().
22  *      AX.25 037       Jonathan(G4KLX) New timer architecture.
23  */
24
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/socket.h>
28 #include <linux/in.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/string.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
35 #include <net/ax25.h>
36 #include <linux/inet.h>
37 #include <linux/netdevice.h>
38 #include <linux/skbuff.h>
39 #include <net/sock.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <linux/fcntl.h>
43 #include <linux/mm.h>
44 #include <linux/interrupt.h>
45
46 void ax25_ds_nr_error_recovery(ax25_cb *ax25)
47 {
48         ax25_ds_establish_data_link(ax25);
49 }
50
51 /*
52  *      dl1bke 960114: transmit I frames on DAMA poll
53  */
54 void ax25_ds_enquiry_response(ax25_cb *ax25)
55 {
56         ax25_cb *ax25o;
57
58         /* Please note that neither DK4EG´s nor DG2FEF´s
59          * DAMA spec mention the following behaviour as seen
60          * with TheFirmware:
61          *
62          *      DB0ACH->DL1BKE <RR C P R0> [DAMA]
63          *      DL1BKE->DB0ACH <I NR=0 NS=0>
64          *      DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
65          *      DL1BKE->DB0ACH <RR R F R0>
66          *
67          * The Flexnet DAMA Master implementation apparently
68          * insists on the "proper" AX.25 behaviour:
69          *
70          *      DB0ACH->DL1BKE <RR C P R0> [DAMA]
71          *      DL1BKE->DB0ACH <RR R F R0>
72          *      DL1BKE->DB0ACH <I NR=0 NS=0>
73          *      DL1BKE-7->DB0PRA-6 DB0ACH <I C S3 R5>
74          *
75          * Flexnet refuses to send us *any* I frame if we send
76          * a REJ in case AX25_COND_REJECT is set. It is superfluous in 
77          * this mode anyway (a RR or RNR invokes the retransmission).
78          * Is this a Flexnet bug?
79          */
80
81         ax25_std_enquiry_response(ax25);
82
83         if (!(ax25->condition & AX25_COND_PEER_RX_BUSY)) {
84                 ax25_requeue_frames(ax25);
85                 ax25_kick(ax25);
86         }
87
88         if (ax25->state == AX25_STATE_1 || ax25->state == AX25_STATE_2 || skb_peek(&ax25->ack_queue) != NULL)
89                 ax25_ds_t1_timeout(ax25);
90         else
91                 ax25->n2count = 0;
92
93         ax25_start_t3timer(ax25);
94         ax25_ds_set_timer(ax25->ax25_dev);
95
96         for (ax25o = ax25_list; ax25o != NULL; ax25o = ax25o->next) {
97                 if (ax25o == ax25)
98                         continue;
99
100                 if (ax25o->ax25_dev != ax25->ax25_dev)
101                         continue;
102
103                 if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2) {
104                         ax25_ds_t1_timeout(ax25o);
105                         continue;
106                 }
107
108                 if (!(ax25o->condition & AX25_COND_PEER_RX_BUSY) && ax25o->state == AX25_STATE_3) {
109                         ax25_requeue_frames(ax25o);
110                         ax25_kick(ax25o);
111                 }
112
113                 if (ax25o->state == AX25_STATE_1 || ax25o->state == AX25_STATE_2 || skb_peek(&ax25o->ack_queue) != NULL)
114                         ax25_ds_t1_timeout(ax25o);
115
116                 /* do not start T3 for listening sockets (tnx DD8NE) */
117
118                 if (ax25o->state != AX25_STATE_0)
119                         ax25_start_t3timer(ax25o);
120         }
121 }
122
123 void ax25_ds_establish_data_link(ax25_cb *ax25)
124 {
125         ax25->condition &= AX25_COND_DAMA_MODE;
126         ax25->n2count    = 0;
127         ax25_calculate_t1(ax25);
128         ax25_start_t1timer(ax25);
129         ax25_stop_t2timer(ax25);
130         ax25_start_t3timer(ax25);
131 }
132
133 /*
134  *      :::FIXME:::
135  *      This is a kludge. Not all drivers recognize kiss commands. 
136  *      We need a driver level  request to switch duplex mode, that does 
137  *      either SCC changing, PI config or KISS as required. Currently
138  *      this request isn't reliable.
139  */
140 static void ax25_kiss_cmd(ax25_dev *ax25_dev, unsigned char cmd, unsigned char param)
141 {
142         struct sk_buff *skb;
143         unsigned char *p;
144
145         if (ax25_dev->dev == NULL)
146                 return;
147
148         if ((skb = alloc_skb(2, GFP_ATOMIC)) == NULL)
149                 return;
150
151         skb->nh.raw = skb->data;
152         p = skb_put(skb, 2);
153
154         *p++ = cmd;
155         *p++ = param;
156
157         skb->dev      = ax25_dev->dev;
158         skb->protocol = htons(ETH_P_AX25);
159
160         dev_queue_xmit(skb);
161 }
162
163 /*
164  *      A nasty problem arises if we count the number of DAMA connections
165  *      wrong, especially when connections on the device already existed
166  *      and our network node (or the sysop) decides to turn on DAMA Master
167  *      mode. We thus flag the 'real' slave connections with 
168  *      ax25->dama_slave=1 and look on every disconnect if still slave
169  *      connections exist.
170  */
171 static int ax25_check_dama_slave(ax25_dev *ax25_dev)
172 {
173         ax25_cb *ax25;
174
175         for (ax25 = ax25_list; ax25 != NULL ; ax25 = ax25->next)
176                 if (ax25->ax25_dev == ax25_dev && (ax25->condition & AX25_COND_DAMA_MODE) && ax25->state > AX25_STATE_1)
177                         return 1;
178
179         return 0;
180 }
181
182 void ax25_dev_dama_on(ax25_dev *ax25_dev)
183 {
184         if (ax25_dev == NULL)
185                 return;
186
187         if (ax25_dev->dama.slave == 0)
188                 ax25_kiss_cmd(ax25_dev, 5, 1);
189
190         ax25_dev->dama.slave = 1;
191         ax25_ds_set_timer(ax25_dev);
192 }
193
194 void ax25_dev_dama_off(ax25_dev *ax25_dev)
195 {
196         if (ax25_dev == NULL)
197                 return;
198
199         if (ax25_dev->dama.slave && !ax25_check_dama_slave(ax25_dev)) {
200                 ax25_kiss_cmd(ax25_dev, 5, 0);
201                 ax25_dev->dama.slave = 0;
202                 ax25_ds_del_timer(ax25_dev);
203         }
204 }
205
206 void ax25_dama_on(ax25_cb *ax25)
207 {
208         ax25_dev_dama_on(ax25->ax25_dev);
209         ax25->condition |= AX25_COND_DAMA_MODE;
210 }
211
212 void ax25_dama_off(ax25_cb *ax25)
213 {
214         ax25->condition &= ~AX25_COND_DAMA_MODE;
215         ax25_dev_dama_off(ax25->ax25_dev);
216 }
217