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.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.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 <linux/slab.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
25 #include <asm/system.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
29 #include <linux/netfilter.h>
32 static void rose_ftimer_expiry(unsigned long);
33 static void rose_t0timer_expiry(unsigned long);
35 static void rose_transmit_restart_confirmation(struct rose_neigh
*neigh
);
36 static void rose_transmit_restart_request(struct rose_neigh
*neigh
);
38 void rose_start_ftimer(struct rose_neigh
*neigh
)
40 del_timer(&neigh
->ftimer
);
42 neigh
->ftimer
.data
= (unsigned long)neigh
;
43 neigh
->ftimer
.function
= &rose_ftimer_expiry
;
44 neigh
->ftimer
.expires
=
45 jiffies
+ msecs_to_jiffies(sysctl_rose_link_fail_timeout
);
47 add_timer(&neigh
->ftimer
);
50 static void rose_start_t0timer(struct rose_neigh
*neigh
)
52 del_timer(&neigh
->t0timer
);
54 neigh
->t0timer
.data
= (unsigned long)neigh
;
55 neigh
->t0timer
.function
= &rose_t0timer_expiry
;
56 neigh
->t0timer
.expires
=
57 jiffies
+ msecs_to_jiffies(sysctl_rose_restart_request_timeout
);
59 add_timer(&neigh
->t0timer
);
62 void rose_stop_ftimer(struct rose_neigh
*neigh
)
64 del_timer(&neigh
->ftimer
);
67 void rose_stop_t0timer(struct rose_neigh
*neigh
)
69 del_timer(&neigh
->t0timer
);
72 int rose_ftimer_running(struct rose_neigh
*neigh
)
74 return timer_pending(&neigh
->ftimer
);
77 static int rose_t0timer_running(struct rose_neigh
*neigh
)
79 return timer_pending(&neigh
->t0timer
);
82 static void rose_ftimer_expiry(unsigned long param
)
86 static void rose_t0timer_expiry(unsigned long param
)
88 struct rose_neigh
*neigh
= (struct rose_neigh
*)param
;
90 rose_transmit_restart_request(neigh
);
94 rose_start_t0timer(neigh
);
98 * Interface to ax25_send_frame. Changes my level 2 callsign depending
99 * on whether we have a global ROSE callsign or use the default port
102 static int rose_send_frame(struct sk_buff
*skb
, struct rose_neigh
*neigh
)
104 ax25_address
*rose_call
;
107 if (ax25cmp(&rose_callsign
, &null_ax25_address
) == 0)
108 rose_call
= (ax25_address
*)neigh
->dev
->dev_addr
;
110 rose_call
= &rose_callsign
;
113 neigh
->ax25
= ax25_send_frame(skb
, 260, rose_call
, &neigh
->callsign
, neigh
->digipeat
, neigh
->dev
);
117 return (neigh
->ax25
!= NULL
);
121 * Interface to ax25_link_up. Changes my level 2 callsign depending
122 * on whether we have a global ROSE callsign or use the default port
125 static int rose_link_up(struct rose_neigh
*neigh
)
127 ax25_address
*rose_call
;
130 if (ax25cmp(&rose_callsign
, &null_ax25_address
) == 0)
131 rose_call
= (ax25_address
*)neigh
->dev
->dev_addr
;
133 rose_call
= &rose_callsign
;
136 neigh
->ax25
= ax25_find_cb(rose_call
, &neigh
->callsign
, neigh
->digipeat
, neigh
->dev
);
140 return (neigh
->ax25
!= NULL
);
144 * This handles all restart and diagnostic frames.
146 void rose_link_rx_restart(struct sk_buff
*skb
, struct rose_neigh
*neigh
, unsigned short frametype
)
148 struct sk_buff
*skbn
;
151 case ROSE_RESTART_REQUEST
:
152 rose_stop_t0timer(neigh
);
153 neigh
->restarted
= 1;
154 neigh
->dce_mode
= (skb
->data
[3] == ROSE_DTE_ORIGINATED
);
155 rose_transmit_restart_confirmation(neigh
);
158 case ROSE_RESTART_CONFIRMATION
:
159 rose_stop_t0timer(neigh
);
160 neigh
->restarted
= 1;
163 case ROSE_DIAGNOSTIC
:
164 printk(KERN_WARNING
"ROSE: received diagnostic #%d - %02X %02X %02X\n", skb
->data
[3], skb
->data
[4], skb
->data
[5], skb
->data
[6]);
168 printk(KERN_WARNING
"ROSE: received unknown %02X with LCI 000\n", frametype
);
172 if (neigh
->restarted
) {
173 while ((skbn
= skb_dequeue(&neigh
->queue
)) != NULL
)
174 if (!rose_send_frame(skbn
, neigh
))
180 * This routine is called when a Restart Request is needed
182 static void rose_transmit_restart_request(struct rose_neigh
*neigh
)
188 len
= AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
+ 3;
190 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
193 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
);
195 dptr
= skb_put(skb
, ROSE_MIN_LEN
+ 3);
197 *dptr
++ = AX25_P_ROSE
;
200 *dptr
++ = ROSE_RESTART_REQUEST
;
201 *dptr
++ = ROSE_DTE_ORIGINATED
;
204 if (!rose_send_frame(skb
, neigh
))
209 * This routine is called when a Restart Confirmation is needed
211 static void rose_transmit_restart_confirmation(struct rose_neigh
*neigh
)
217 len
= AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
+ 1;
219 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
222 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
);
224 dptr
= skb_put(skb
, ROSE_MIN_LEN
+ 1);
226 *dptr
++ = AX25_P_ROSE
;
229 *dptr
++ = ROSE_RESTART_CONFIRMATION
;
231 if (!rose_send_frame(skb
, neigh
))
236 * This routine is called when a Clear Request is needed outside of the context
237 * of a connected socket.
239 void rose_transmit_clear_request(struct rose_neigh
*neigh
, unsigned int lci
, unsigned char cause
, unsigned char diagnostic
)
245 len
= AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
+ 3;
247 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
250 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
);
252 dptr
= skb_put(skb
, ROSE_MIN_LEN
+ 3);
254 *dptr
++ = AX25_P_ROSE
;
255 *dptr
++ = ((lci
>> 8) & 0x0F) | ROSE_GFI
;
256 *dptr
++ = ((lci
>> 0) & 0xFF);
257 *dptr
++ = ROSE_CLEAR_REQUEST
;
259 *dptr
++ = diagnostic
;
261 if (!rose_send_frame(skb
, neigh
))
265 void rose_transmit_link(struct sk_buff
*skb
, struct rose_neigh
*neigh
)
270 if (call_fw_firewall(PF_ROSE
, skb
->dev
, skb
->data
, NULL
, &skb
) != FW_ACCEPT
) {
276 if (neigh
->loopback
) {
277 rose_loopback_queue(skb
, neigh
);
281 if (!rose_link_up(neigh
))
282 neigh
->restarted
= 0;
284 dptr
= skb_push(skb
, 1);
285 *dptr
++ = AX25_P_ROSE
;
287 if (neigh
->restarted
) {
288 if (!rose_send_frame(skb
, neigh
))
291 skb_queue_tail(&neigh
->queue
, skb
);
293 if (!rose_t0timer_running(neigh
)) {
294 rose_transmit_restart_request(neigh
);
296 rose_start_t0timer(neigh
);