import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / net / rose / rose_link.c
1 /*
2  *      ROSE release 003
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  *      History
13  *      ROSE 001        Jonathan(G4KLX) Cloned from rose_timer.c
14  *      ROSE 003        Jonathan(G4KLX) New timer architecture.
15  */
16
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/in.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <net/ax25.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <net/sock.h>
32 #include <asm/segment.h>
33 #include <asm/system.h>
34 #include <linux/fcntl.h>
35 #include <linux/mm.h>
36 #include <linux/interrupt.h>
37 #include <linux/netfilter.h>
38 #include <net/rose.h>
39
40 static void rose_ftimer_expiry(unsigned long);
41 static void rose_t0timer_expiry(unsigned long);
42
43 void rose_start_ftimer(struct rose_neigh *neigh)
44 {
45         del_timer(&neigh->ftimer);
46
47         neigh->ftimer.data     = (unsigned long)neigh;
48         neigh->ftimer.function = &rose_ftimer_expiry;
49         neigh->ftimer.expires  = jiffies + sysctl_rose_link_fail_timeout;
50
51         add_timer(&neigh->ftimer);
52 }
53
54 void rose_start_t0timer(struct rose_neigh *neigh)
55 {
56         del_timer(&neigh->t0timer);
57
58         neigh->t0timer.data     = (unsigned long)neigh;
59         neigh->t0timer.function = &rose_t0timer_expiry;
60         neigh->t0timer.expires  = jiffies + sysctl_rose_restart_request_timeout;
61
62         add_timer(&neigh->t0timer);
63 }
64
65 void rose_stop_ftimer(struct rose_neigh *neigh)
66 {
67         del_timer(&neigh->ftimer);
68 }
69
70 void rose_stop_t0timer(struct rose_neigh *neigh)
71 {
72         del_timer(&neigh->t0timer);
73 }
74
75 int rose_ftimer_running(struct rose_neigh *neigh)
76 {
77         return timer_pending(&neigh->ftimer);
78 }
79
80 int rose_t0timer_running(struct rose_neigh *neigh)
81 {
82         return timer_pending(&neigh->t0timer);
83 }
84
85 static void rose_ftimer_expiry(unsigned long param)
86 {
87 }
88
89 static void rose_t0timer_expiry(unsigned long param)
90 {
91         struct rose_neigh *neigh = (struct rose_neigh *)param;
92
93         rose_transmit_restart_request(neigh);
94
95         neigh->dce_mode = 0;
96
97         rose_start_t0timer(neigh);
98 }
99
100 /*
101  *      Interface to ax25_send_frame. Changes my level 2 callsign depending
102  *      on whether we have a global ROSE callsign or use the default port
103  *      callsign.
104  */
105 static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
106 {
107         ax25_address *rose_call;
108
109         if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
110                 rose_call = (ax25_address *)neigh->dev->dev_addr;
111         else
112                 rose_call = &rose_callsign;
113
114         neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
115
116         return (neigh->ax25 != NULL);
117 }
118
119 /*
120  *      Interface to ax25_link_up. Changes my level 2 callsign depending
121  *      on whether we have a global ROSE callsign or use the default port
122  *      callsign.
123  */
124 static int rose_link_up(struct rose_neigh *neigh)
125 {
126         ax25_address *rose_call;
127
128         if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
129                 rose_call = (ax25_address *)neigh->dev->dev_addr;
130         else
131                 rose_call = &rose_callsign;
132
133         neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
134
135         return (neigh->ax25 != NULL);
136 }
137
138 /*
139  *      This handles all restart and diagnostic frames.
140  */
141 void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
142 {
143         struct sk_buff *skbn;
144
145         switch (frametype) {
146                 case ROSE_RESTART_REQUEST:
147                         rose_stop_t0timer(neigh);
148                         neigh->restarted = 1;
149                         neigh->dce_mode  = (skb->data[3] == ROSE_DTE_ORIGINATED);
150                         rose_transmit_restart_confirmation(neigh);
151                         break;
152
153                 case ROSE_RESTART_CONFIRMATION:
154                         rose_stop_t0timer(neigh);
155                         neigh->restarted = 1;
156                         break;
157
158                 case ROSE_DIAGNOSTIC:
159                         printk(KERN_WARNING "ROSE: received diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
160                         break;
161
162                 default:
163                         printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
164                         break;
165         }
166
167         if (neigh->restarted) {
168                 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
169                         if (!rose_send_frame(skbn, neigh))
170                                 kfree_skb(skbn);
171         }
172 }
173
174 /*
175  *      This routine is called when a Restart Request is needed
176  */
177 void rose_transmit_restart_request(struct rose_neigh *neigh)
178 {
179         struct sk_buff *skb;
180         unsigned char *dptr;
181         int len;
182
183         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
184
185         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
186                 return;
187
188         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
189
190         dptr = skb_put(skb, ROSE_MIN_LEN + 3);
191
192         *dptr++ = AX25_P_ROSE;
193         *dptr++ = ROSE_GFI;
194         *dptr++ = 0x00;
195         *dptr++ = ROSE_RESTART_REQUEST;
196         *dptr++ = ROSE_DTE_ORIGINATED;
197         *dptr++ = 0;
198
199         if (!rose_send_frame(skb, neigh))
200                 kfree_skb(skb);
201 }
202
203 /*
204  * This routine is called when a Restart Confirmation is needed
205  */
206 void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
207 {
208         struct sk_buff *skb;
209         unsigned char *dptr;
210         int len;
211
212         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
213
214         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
215                 return;
216
217         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
218
219         dptr = skb_put(skb, ROSE_MIN_LEN + 1);
220
221         *dptr++ = AX25_P_ROSE;
222         *dptr++ = ROSE_GFI;
223         *dptr++ = 0x00;
224         *dptr++ = ROSE_RESTART_CONFIRMATION;
225
226         if (!rose_send_frame(skb, neigh))
227                 kfree_skb(skb);
228 }
229
230 /*
231  * This routine is called when a Diagnostic is required.
232  */
233 void rose_transmit_diagnostic(struct rose_neigh *neigh, unsigned char diag)
234 {
235         struct sk_buff *skb;
236         unsigned char *dptr;
237         int len;
238
239         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 2;
240
241         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
242                 return;
243
244         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
245
246         dptr = skb_put(skb, ROSE_MIN_LEN + 2);
247
248         *dptr++ = AX25_P_ROSE;
249         *dptr++ = ROSE_GFI;
250         *dptr++ = 0x00;
251         *dptr++ = ROSE_DIAGNOSTIC;
252         *dptr++ = diag;
253
254         if (!rose_send_frame(skb, neigh))
255                 kfree_skb(skb);
256 }
257
258 /*
259  * This routine is called when a Clear Request is needed outside of the context
260  * of a connected socket.
261  */
262 void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
263 {
264         struct sk_buff *skb;
265         unsigned char *dptr;
266         int len;
267         struct net_device *first;
268         int faclen = 0;
269
270         len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
271
272         first = rose_dev_first();
273         if (first)
274                 faclen = 6 + AX25_ADDR_LEN + 3 + ROSE_ADDR_LEN;
275         
276         if ((skb = alloc_skb(len + faclen, GFP_ATOMIC)) == NULL)
277                 return;
278
279         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
280
281         dptr = skb_put(skb, ROSE_MIN_LEN + 3 + faclen);
282
283         *dptr++ = AX25_P_ROSE;
284         *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
285         *dptr++ = ((lci >> 0) & 0xFF);
286         *dptr++ = ROSE_CLEAR_REQUEST;
287         *dptr++ = cause;
288         *dptr++ = diagnostic;
289
290         if (first) {    
291                 *dptr++ = 0x00;         /* Address length */
292                 *dptr++ = 4 + AX25_ADDR_LEN + 3 + ROSE_ADDR_LEN; /* Facilities length */
293                 *dptr++ = 0;
294                 *dptr++ = FAC_NATIONAL;
295                 *dptr++ = FAC_NATIONAL_FAIL_CALL;
296                 *dptr++ = AX25_ADDR_LEN;
297                 memcpy(dptr, &rose_callsign, AX25_ADDR_LEN);
298                 dptr += AX25_ADDR_LEN;
299                 *dptr++ = FAC_NATIONAL_FAIL_ADD;
300                 *dptr++ = ROSE_ADDR_LEN + 1;
301                 *dptr++ = ROSE_ADDR_LEN * 2;
302                 memcpy(dptr, first->dev_addr, ROSE_ADDR_LEN);
303         }
304
305         if (!rose_send_frame(skb, neigh))
306                 kfree_skb(skb);
307 }
308
309 void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
310 {
311         unsigned char *dptr;
312
313 #if 0
314         if (call_fw_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT) {
315                 kfree_skb(skb);
316                 return;
317         }
318 #endif
319
320         if (neigh->loopback) {
321                 rose_loopback_queue(skb, neigh);
322                 return;
323         }
324
325         if (!rose_link_up(neigh))
326                 neigh->restarted = 0;
327
328         dptr = skb_push(skb, 1);
329         *dptr++ = AX25_P_ROSE;
330
331         if (neigh->restarted) {
332                 if (!rose_send_frame(skb, neigh))
333                         kfree_skb(skb);
334         } else {
335                 skb_queue_tail(&neigh->queue, skb);
336
337                 if (!rose_t0timer_running(neigh)) {
338                         rose_transmit_restart_request(neigh);
339                         neigh->dce_mode = 0;
340                         rose_start_t0timer(neigh);
341                 }
342         }
343 }