Import 2.3.30pre3
[davej-history.git] / net / rose / rose_link.c
blob896a372e9e6e0de41d81ee591ff42f16427f5ecb
1 /*
2 * ROSE release 003
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
12 * History
13 * ROSE 001 Jonathan(G4KLX) Cloned from rose_timer.c
14 * ROSE 003 Jonathan(G4KLX) New timer architecture.
17 #include <linux/config.h>
18 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
22 #include <linux/in.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <net/ax25.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <asm/segment.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
37 #include <linux/mm.h>
38 #include <linux/interrupt.h>
39 #include <linux/netfilter.h>
40 #include <net/rose.h>
42 static void rose_ftimer_expiry(unsigned long);
43 static void rose_t0timer_expiry(unsigned long);
45 void rose_start_ftimer(struct rose_neigh *neigh)
47 del_timer(&neigh->ftimer);
49 neigh->ftimer.data = (unsigned long)neigh;
50 neigh->ftimer.function = &rose_ftimer_expiry;
51 neigh->ftimer.expires = jiffies + sysctl_rose_link_fail_timeout;
53 add_timer(&neigh->ftimer);
56 void rose_start_t0timer(struct rose_neigh *neigh)
58 del_timer(&neigh->t0timer);
60 neigh->t0timer.data = (unsigned long)neigh;
61 neigh->t0timer.function = &rose_t0timer_expiry;
62 neigh->t0timer.expires = jiffies + sysctl_rose_restart_request_timeout;
64 add_timer(&neigh->t0timer);
67 void rose_stop_ftimer(struct rose_neigh *neigh)
69 del_timer(&neigh->ftimer);
72 void rose_stop_t0timer(struct rose_neigh *neigh)
74 del_timer(&neigh->t0timer);
77 int rose_ftimer_running(struct rose_neigh *neigh)
79 return (neigh->ftimer.prev != NULL || neigh->ftimer.next != NULL);
82 int rose_t0timer_running(struct rose_neigh *neigh)
84 return (neigh->t0timer.prev != NULL || neigh->t0timer.next != NULL);
87 static void rose_ftimer_expiry(unsigned long param)
91 static void rose_t0timer_expiry(unsigned long param)
93 struct rose_neigh *neigh = (struct rose_neigh *)param;
95 rose_transmit_restart_request(neigh);
97 neigh->dce_mode = 0;
99 rose_start_t0timer(neigh);
103 * Interface to ax25_send_frame. Changes my level 2 callsign depending
104 * on whether we have a global ROSE callsign or use the default port
105 * callsign.
107 static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
109 ax25_address *rose_call;
111 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
112 rose_call = (ax25_address *)neigh->dev->dev_addr;
113 else
114 rose_call = &rose_callsign;
116 neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
118 return (neigh->ax25 != NULL);
122 * Interface to ax25_link_up. Changes my level 2 callsign depending
123 * on whether we have a global ROSE callsign or use the default port
124 * callsign.
126 static int rose_link_up(struct rose_neigh *neigh)
128 ax25_address *rose_call;
130 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
131 rose_call = (ax25_address *)neigh->dev->dev_addr;
132 else
133 rose_call = &rose_callsign;
135 neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
137 return (neigh->ax25 != NULL);
141 * This handles all restart and diagnostic frames.
143 void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
145 struct sk_buff *skbn;
147 switch (frametype) {
148 case ROSE_RESTART_REQUEST:
149 rose_stop_t0timer(neigh);
150 neigh->restarted = 1;
151 neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
152 rose_transmit_restart_confirmation(neigh);
153 break;
155 case ROSE_RESTART_CONFIRMATION:
156 rose_stop_t0timer(neigh);
157 neigh->restarted = 1;
158 break;
160 case ROSE_DIAGNOSTIC:
161 printk(KERN_WARNING "ROSE: received diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
162 break;
164 default:
165 printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
166 break;
169 if (neigh->restarted) {
170 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
171 if (!rose_send_frame(skbn, neigh))
172 kfree_skb(skbn);
177 * This routine is called when a Restart Request is needed
179 void rose_transmit_restart_request(struct rose_neigh *neigh)
181 struct sk_buff *skb;
182 unsigned char *dptr;
183 int len;
185 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
187 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
188 return;
190 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
192 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
194 *dptr++ = AX25_P_ROSE;
195 *dptr++ = ROSE_GFI;
196 *dptr++ = 0x00;
197 *dptr++ = ROSE_RESTART_REQUEST;
198 *dptr++ = ROSE_DTE_ORIGINATED;
199 *dptr++ = 0;
201 if (!rose_send_frame(skb, neigh))
202 kfree_skb(skb);
206 * This routine is called when a Restart Confirmation is needed
208 void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
210 struct sk_buff *skb;
211 unsigned char *dptr;
212 int len;
214 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
216 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
217 return;
219 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
221 dptr = skb_put(skb, ROSE_MIN_LEN + 1);
223 *dptr++ = AX25_P_ROSE;
224 *dptr++ = ROSE_GFI;
225 *dptr++ = 0x00;
226 *dptr++ = ROSE_RESTART_CONFIRMATION;
228 if (!rose_send_frame(skb, neigh))
229 kfree_skb(skb);
233 * This routine is called when a Diagnostic is required.
235 void rose_transmit_diagnostic(struct rose_neigh *neigh, unsigned char diag)
237 struct sk_buff *skb;
238 unsigned char *dptr;
239 int len;
241 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 2;
243 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
244 return;
246 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
248 dptr = skb_put(skb, ROSE_MIN_LEN + 2);
250 *dptr++ = AX25_P_ROSE;
251 *dptr++ = ROSE_GFI;
252 *dptr++ = 0x00;
253 *dptr++ = ROSE_DIAGNOSTIC;
254 *dptr++ = diag;
256 if (!rose_send_frame(skb, neigh))
257 kfree_skb(skb);
261 * This routine is called when a Clear Request is needed outside of the context
262 * of a connected socket.
264 void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
266 struct sk_buff *skb;
267 unsigned char *dptr;
268 int len;
270 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
272 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
273 return;
275 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
277 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
279 *dptr++ = AX25_P_ROSE;
280 *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
281 *dptr++ = ((lci >> 0) & 0xFF);
282 *dptr++ = ROSE_CLEAR_REQUEST;
283 *dptr++ = cause;
284 *dptr++ = diagnostic;
286 if (!rose_send_frame(skb, neigh))
287 kfree_skb(skb);
290 void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
292 unsigned char *dptr;
294 #if 0
295 if (call_fw_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT) {
296 kfree_skb(skb);
297 return;
299 #endif
301 if (neigh->loopback) {
302 rose_loopback_queue(skb, neigh);
303 return;
306 if (!rose_link_up(neigh))
307 neigh->restarted = 0;
309 dptr = skb_push(skb, 1);
310 *dptr++ = AX25_P_ROSE;
312 if (neigh->restarted) {
313 if (!rose_send_frame(skb, neigh))
314 kfree_skb(skb);
315 } else {
316 skb_queue_tail(&neigh->queue, skb);
318 if (!rose_t0timer_running(neigh)) {
319 rose_transmit_restart_request(neigh);
320 neigh->dce_mode = 0;
321 rose_start_t0timer(neigh);
326 #endif