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